Ubuntu14.04下 940MX+CUDA8.0 + CuDNN5.1的安装

国内的thinkpad t系列一般会配一块N卡,想着虽然卡不咋地,但是做一些简单的实验还是可以的,于是决定在这台本子上面装CUDA和caffe来练习一下。下面是步骤,前方有坑,要仔细读:

  • 安装NVIDIA的驱动,使用ubuntu自己的driver安装:
    settings -> software & updates -> Additional Drivers -> 选择安装NVIDIA驱动
    虽然有手动安装的方法,但是我觉得这样最保险,一定不会装错。

  • 去NVIDIA官网下载CUDA8.0,注意用.run文件,而不是.deb,据说这样坑比较少一些。。。

  • log out出去,然后ctrl + alt + f1进入命令行模式:
    首先,禁止X server桌面,不然无法继续安装下去。

sudo service lightdm stop

然后,按照英伟达官网的指令:

sudo sh cuda.***.run (看你下的哪个版本)

我第一次装的时候没有指明tmpdir也装成了,但是因为下面有一步出错了卸载了重新装,没有加tmpdir的话会提示 disk space check has failed. Installation cannot continue. 这种情况就要自己指定一个解压的地方来存放临时安装文件了。后面可以写 --tmpdir=...(根据自己的情况)不过我后来很囧的发现我出现这个问题是因为后面自己在/目录下面拷了一个有点大的数据集。。。rm之后就好了,不用指明tmpdir,就直接在/下就可以。<br />
接下来,安装的时候很重要的一点是别盲目选y!!!第一个选项问你要不要装一个Driver,要选no,要选no,要选no,重要的话说三遍。因为这个就是第一步干的事,不能用它这里的driver,不然很可能不匹配,reboot之后就会出现很蛋疼的问题,什么low graphics blabla的...桌面都出不来了,只能卸掉nvidia的驱动重新来。

  • 安装完成后reboot,检验一下是不是装好了。
lspci | grep -i nvidia

如果能检测到的话说明驱动没问题。再看一下现在的主显卡:

prime-select query

test CUDA:

nvidia-smi

如果可以显示GPU当前信息的话,那么CUDA就装好了。
还可以把sample都编译一下,进入/home/username/NVIDIA...

sudo make

一堆warning...哎...<br >这里要设置一下环境变量,

sudo gedit /etc/profile

文件末尾添加PATH=/usr/local/cuda/bin:$PATHexport PATH <br >保存完成后,执行如下命令使环境变量立即生效:

source /etc/profile

然后还需要添加lib的路径:

sudo gedit /etc/ld.so.conf.d/cuda.conf

在文件中写入如下内容然后保存:
/usr/local/cuda/lib64<br >之后执行如下命令使之生效:

sudo ldconfig

ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib.so),进而创建出动态装入程序(ld.so)所需的连接和缓存文件。缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表。我们之前修改过cuda.conf,为了让链接可以找到这里来,才需要这个ldconfig的命令。
或者这样也可以:

echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrcsource ~/.bashrc
  • 再把cuDNN装了:
    依然取去官网下,我下的是CuDNN Library for Linux v5.1,下载需要注册一下...CuDNN的作用是对CUDA进行一些修改和优化,使得更适合于对神经网络进行计算。
    <br />下载后解压,然后进入该目录,把所有的lib复制到/usr/local/cuda/lib64/下,把头文件 cudnn.h复制到 /usr/local/cuda/include/下。
sudo cp lib* /usr/local/cuda/lib64/
sudo cp cudnn.h /usr/local/cuda/include/

然后更新软连接:

cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.5
sudo ln -s libcudnn.so.5.1.5 libcudnn.so.5
sudo ln -s libcudnn.so.5 libcudnn.so

我安装的是5.1.5的,大家要看下自己cudnn解压出来的lib下面的版本号,不可以盲目复制...

  • 安装一些依赖项
sudo apt-get install freeglut3-dev libx11-dev libxmu-dev libxi-dev libglu1-mesa-dev
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
  • 安装python的必备
sudo apt-get install python-dev python-pip
cd python
for req in $(cat requirements.txt); do sudo pip install $req; done

我的pip一直出些问题,后来我就用sudo easy-install pip安装了pip。这个apt-get安装出来的很蛋疼不知道为什么...

  • 安装caffe
git clone https://github.com/BVLC/caffe.git

要安装BLAS作为caffe的matrix运算支持。可以选择安装Atlas。

sudo apt-get install libatlas-base-dev
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc

编译之前修改Makefile.config
由于需要CuDNN,所以把 # USE_CUDNN := 1的注释去掉。还有Anaconda2的路径修改等。我根据自己的安装修改后的内容如下:

## Refer to http://caffe.berkeleyvision.org/installation.html
    # Contributions simplifying and improving our build system are welcome!

    # cuDNN acceleration switch (uncomment to build with cuDNN).
     USE_CUDNN := 1

    # CPU-only switch (uncomment to build without GPU support).
    # CPU_ONLY := 1

    # uncomment to disable IO dependencies and corresponding data layers
    # USE_OPENCV := 0
    # USE_LEVELDB := 0
    # USE_LMDB := 0

    # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
    #   You should not set this flag if you will be reading LMDBs with any
    #   possibility of simultaneous read and write
    # ALLOW_LMDB_NOLOCK := 1

    # Uncomment if you're using OpenCV 3
    # OPENCV_VERSION := 3

    # To customize your choice of compiler, uncomment and set the following.
    # N.B. the default for Linux is g++ and the default for OSX is clang++
    # CUSTOM_CXX := g++

    # CUDA directory contains bin/ and lib/ directories that we need.
    CUDA_DIR := /usr/local/cuda
    # On Ubuntu 14.04, if cuda tools are installed via
    # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
    # CUDA_DIR := /usr

    # CUDA architecture setting: going with all of them.
    # For CUDA < 6.0, comment the *_50 lines for compatibility.
    CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
            -gencode arch=compute_20,code=sm_21 \
            -gencode arch=compute_30,code=sm_30 \
            -gencode arch=compute_35,code=sm_35 \
            -gencode arch=compute_50,code=sm_50 \
            -gencode arch=compute_50,code=compute_50

    # BLAS choice:
    # atlas for ATLAS (default)
    # mkl for MKL
    # open for OpenBlas
    BLAS := atlas
    # Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
    # Leave commented to accept the defaults for your choice of BLAS
    # (which should work)!
    # BLAS_INCLUDE := /path/to/your/blas
    # BLAS_LIB := /path/to/your/blas

    # Homebrew puts openblas in a directory that is not on the standard search path
    # BLAS_INCLUDE := $(shell brew --prefix openblas)/include
    # BLAS_LIB := $(shell brew --prefix openblas)/lib

    # This is required only if you will compile the matlab interface.
    # MATLAB directory should contain the mex binary in /bin.
     MATLAB_DIR := /usr/local/matlab
    # MATLAB_DIR := /matlab

    # NOTE: this is required only if you will compile the python interface.
    # We need to be able to find Python.h and numpy/arrayobject.h.
    # PYTHON_INCLUDE := /usr/include/python2.7 \
            # /usr/lib/python2.7/dist-packages/numpy/core/include
    # Anaconda Python distribution is quite popular. Include path:
    # Verify anaconda location, sometimes it's in root.
    ANACONDA_HOME := $(HOME)/anaconda2
    PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
                      $(ANACONDA_HOME)/include/python2.7 \
            # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

    # Uncomment to use Python 3 (default is Python 2)
    # PYTHON_LIBRARIES := boost_python3 python3.5m
    # PYTHON_INCLUDE := /usr/include/python3.5m \
    #                 /usr/lib/python3.5/dist-packages/numpy/core/include

    # We need to be able to find libpythonX.X.so or .dylib.
    PYTHON_LIB := /usr/lib
    PYTHON_LIB := $(ANACONDA_HOME)/lib

    # Homebrew installs numpy in a non standard path (keg only)
    # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
    # PYTHON_LIB += $(shell brew --prefix numpy)/lib

    # Uncomment to support layers written in Python (will link against Python libs)
    # WITH_PYTHON_LAYER := 1

    # Whatever else you find you need goes here.
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 

    # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
    # INCLUDE_DIRS += $(shell brew --prefix)/include
    # LIBRARY_DIRS += $(shell brew --prefix)/lib

    # Uncomment to use `pkg-config` to specify OpenCV library paths.
    # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
    # USE_PKG_CONFIG := 1

    # N.B. both build and distribute dirs are cleared on `make clean`
    BUILD_DIR := build
    DISTRIBUTE_DIR := distribute

    # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
    # DEBUG := 1

    # The ID of the GPU that 'make runtest' will use to run unit tests.
    TEST_GPUID := 0

    # enable pretty build (comment to see full commands)
    Q ?= @

caffe目录下:

make all -j16
make test
make runtest

在进行make runtest的时候可能会报错说找不到libhdf5.so.10的错误。我解决这个的方式是去/usr/lib/下把libhdf5.so.7和libhdf5_hl.so.7该成了.10。。。。因为我发现我即使把那些依赖都装好了,还是.7,如果有更好的解决方案请告诉我,谢谢~<br />根据需求编译matcaffe和pycaffe

make matcaffe
make pycaffe

matcaffe要能找到mex文件,所以先把matlab装好然后把matlab的路径填到Makefile.config里面去。还有在make pycaffe的时候,因为我是用的anaconda2,Python.h就在anaconda2/include/python2.7/下面,所以这个路径要包含进去,在PYTHON_INCLUDE下面ANACONDA的第二行取消注释。

  • 运行mnist demo
sh data/mnist/get_mnist.sh
sh examples/mnist/create_mnist.sh
sh examples/mnist/train_lenet.sh
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,637评论 1 318
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,443评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,164评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,075评论 0 188
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,080评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,365评论 1 184
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,901评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,649评论 0 176
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,122评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,734评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,093评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,548评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,028评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,765评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,291评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,162评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,293评论 2 242

推荐阅读更多精彩内容