ubuntu安装CUDA
因为深度学习需要用到CUDA,所以写篇博客,记录下自己安装CUDA 的过程。
1 安装前的检查
安装CUDA之前,首先要做一些事情,检查你的机器是否可以安装CUDA。
1.1 检查你的gpu是否是可以安装CUDA 的
运行如下命令:
$ lspci | grep -i nvidia
这个是我的机器的返回结果:
01:00.0 VGA compatible controller: NVIDIA Corporation GM107 [GeForce GTX 750 Ti] (rev a2) 01:00.1 Audio device: NVIDIA Corporation Device 0fbc (rev a1)
1.2 检查你的linux版本是否支持CUDA
运行如下命令:
uname -m && cat /etc/*release
我的机器返回结果如下:
x86_64 DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS" NAME="Ubuntu" VERSION="14.04.2 LTS,Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.2 LTS" VERSION_ID="14.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" DISTRIB_ID=Ubuntu Kylin DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu Kylin 14.04"
x86_64代表你的机器是64位的,剩下的是解释的linux发行版信息。
如果是红帽,可能是这样的信息:
x86_64 Red Hat Enterprise Linux Workstation release 6.0 (Santiago)
CUDA只支持一些特定的linux发行版,有Fedora,OpenSuSE,RHEL,CentOS,SLES,Ubuntu.
1.3 验证操作系统是否安装了gcc
在使用CUDA Tookit 开发的时候,gcc是需要的,但是运行CUDA程序的时候不需要。
gcc -v
我的结果是:
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-12ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-12ubuntu1)
1.4 验证linux内核是否有正确的系统头文件
输入:
uname -r
结果为:
3.16.0-53-generic
如果没有出现结果,就需要如下命令进行安装:
sudo apt-get install linux-headers-$(uname -r)
2 安装CUDA-Toolkit
点击官网链接:CUDA-Toolkit ,在Select Target Platform里,点击linux,86_64,Ubuntu,14.04,deb[network],之后网页会自动弹出来安装指令:
Installation Instructions: `sudo dpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb ` `sudo apt-get update` `sudo apt-get install cuda`
deb[network]和deb[local]的区别就是,local是把完整的安装文件一次下载下来后安装,而network是在线下载。依次运行这三个命令,可以将CUDA安装成功。
我在执行第一步的时候,出现了这个错误:
ws@ws-Lenovo:/media/ws/000F9A5700006688/Downloads$ sudo dpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb 1404-8-0-local-ga2_8.0.61-1_amd64.deb (Reading database ... 280787 files and directories currently installed.) Preparing to unpack cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb ... Unpacking cuda-repo-ubuntu1404-8-0-local-ga2 (8.0.61-1) over (8.0.61-1) ... Setting up cuda-repo-ubuntu1404-8-0-local-ga2 (8.0.61-1) ... run-parts: failed to stat component /etc/apt/trusted.gpg.d/wps-office-archive-keyring.gpg: No such file or directory OK
之后我把wps-office给卸载了就没有问题了,应该是wps软连接的问题吧。以下是成功的信息:
ws@ws-Lenovo:/media/ws/000F9A5700006688/Downloads$ sudo dpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb (Reading database ... 279398 files and directories currently installed.) Preparing to unpack cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb ... Unpacking cuda-repo-ubuntu1404-8-0-local-ga2 (8.0.61-1) over (8.0.61-1) ... Setting up cuda-repo-ubuntu1404-8-0-local-ga2 (8.0.61-1) ... OK
3 安装之后要做的事
在安装之后,我们还需要做一些工作,才能真正完成CUDA的安装。
3.1 必须要做的事
添加CUDA的bin目录到PATH环境变量:
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
之后在控制台输入nvcc –version,可以得到如下信息:
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2016 NVIDIA Corporation Built on Tue_Jan_10_13:22:03_CST_2017 Cuda compilation tools,release 8.0,V8.0.61
3.2 建议要做的事
之后我们可以安装一些官方的CUDA例子,来检验我们是否安装成功了。
进入CUDA目录/usr/local/cuda-8.0/bin,会发现在这个目录下,有一个名为cuda-install-samples-8.0.sh的文件,在控制台使用命令:
sudo sh cuda-install-samples-8.0.sh "例子被创建的目录"
我使用的是/home目录。在我的/home目录下,有一个NVIDIA_CUDA-8.0_Samples 文件夹,里面就是官方的例子,进入这个目录,输入make进行编译。
sudo make
需要相当长一段时间才能编译完成。我在编译第三个sample的时候,遇到了一个错误
/usr/bin/ld: cannot find -lnvcuvid
刚开始以为是安装出错了,因为之前安装失败过一次,又手动把CUDA给卸载了。结果发现,是英伟达显卡驱动版本不同导致的.在NVIDIA_CUDA-7.0_Samples/3_Imaging/cudaDecodeGL/findgllib.mk文件中,
UBUNTU_PKG_NAME = "nvidia-367"
而我的英伟达驱动是375,于是只要把这行代码改成
UBUNTU_PKG_NAME = "nvidia-375"
就可以了,然后所有的例子都顺利的编译通过了。在编译完所有例子以后,会提示:
Finished building CUDA samples
之后运行一些例子,编译好的二进制文件,保存在~/NVIDIA_CUDA-8.0_Samples/NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release 中,进入这个目录,输入ls,看到很多编译好的二进制文件。先运行deviceQuery。输入
sudo ./deviceQuery
可以看到如下运行结果:
./deviceQuery Starting... CUDA Device Query (Runtime API) version (CUDART static linking) Detected 1 CUDA Capable device(s) Device 0: "GeForce GTX 750 Ti" CUDA Driver Version / Runtime Version 8.0 / 8.0 CUDA Capability Major/Minor version number: 5.0 Total amount of global memory: 2000 MBytes (2096824320 bytes) ( 5) Multiprocessors,(128) CUDA Cores/MP: 640 CUDA Cores GPU Max Clock rate: 1189 MHz (1.19 GHz) Memory Clock rate: 2700 Mhz Memory Bus Width: 128-bit L2 Cache Size: 2097152 bytes Maximum Texture Dimension Size (x,y,z) 1D=(65536),2D=(65536,65536),3D=(4096,4096,4096) Maximum Layered 1D Texture Size,(num) layers 1D=(16384),2048 layers Maximum Layered 2D Texture Size,(num) layers 2D=(16384,16384),2048 layers Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 49152 bytes Total number of registers available per block: 65536 Warp size: 32 Maximum number of threads per multiprocessor: 2048 Maximum number of threads per block: 1024 Max dimension size of a thread block (x,z): (1024,1024,64) Max dimension size of a grid size (x,z): (2147483647,65535,65535) Maximum memory pitch: 2147483647 bytes Texture alignment: 512 bytes Concurrent copy and kernel execution: Yes with 1 copy engine(s) Run time limit on kernels: Yes Integrated GPU sharing Host Memory: No Support host page-locked memory mapping: Yes Alignment requirement for Surfaces: Yes Device has ECC support: Disabled Device supports Unified Addressing (UVA): Yes Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0 Compute Mode: < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) > deviceQuery,CUDA Driver = CUDART,CUDA Driver Version = 8.0,CUDA Runtime Version = 8.0,NumDevs = 1,Device0 = GeForce GTX 750 Ti Result = PASS
在运行bandwidthTest
sudo ./bandwidthTest