第一部分 写在前面
首先,http://blog.csdn.net/buyaoxx/article/details/77870885 很有参考价值,如博客中描述的步骤基本问题不大。
第二部分 安装步骤
第一步、安装显卡驱动
以我的显卡 GeForce 940M 为例
第一步、禁用nouveau驱动,这步很重要,不然nvidia的驱动是装不好的,安装显卡驱动后如果没有 nvidia-smi 基本上就是这个原因。
具体的操作如下:
1)、 使用命令查看Noueau驱动是否被加载
$ lsmod | grep nouveau
# 如果打印出一些信息,说明nouveau被加载了
2)、创建文件 /etc/modprobe.d/blacklist-nouveau.conf,文件内容如下:
blacklist nouveau
options nouveau modeset=0
然后重启系统
第二步、确认nvidia驱动版本,并安装
首先去nvidia官网上查看适合你电脑GPU的最新驱动:http://www.nvidia.com/Download/index.aspx?lang=en-us
我的显卡对应的驱动版本为:384.111,则使用如下命令进行安装:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-384(384是你查到的版本号,如果查到的版本号含有小数只要整数部分)
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
安装的过程中如果你在BIOS里面开启了Security Boot,则会提示是否要关闭,一定要关闭,否则安装后也不会出现 nvidia-smi
安装完之后重启,运行nvidia-smi ,我的打印如下:
Thu Jan 25 23:36:50 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.111 Driver Version: 384.111 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce 940M Off | 00000000:01:00.0 Off | N/A |
| N/A 38C P5 N/A / N/A | 488MiB / 2002MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1051 G /usr/lib/xorg/Xorg 224MiB |
| 0 1801 G compiz 137MiB |
| 0 1999 G fcitx-qimpanel 7MiB |
| 0 2286 G ...-token=EE71FBA4AEB74D004829B87B175899F6 117MiB |
+-----------------------------------------------------------------------------+
这个时候已经找到GPU了,证明驱动安装OK了。
第三步、安装CUDA
我下载的CUDA版本为:,安装时没有安装driver和sample,driver是一定不能装,sample是没必要。
安装命令为:
sudo ./sudo sh cuda_8.0.27_linux.run --tmpdir=/tmp
安装完成之后配置环境变量
$ vim ~/.bashrc # 编辑
在脚本中增加如下内容:
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:/usr/local/cuda-8.0/extras/CUPTI/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda-8.0/bin:$PATH
使变量立即生效
$source ~/.bashrc #配置立即生效
第四步、验证CUDA
验证CUDA使要先降低GCC和G++的版本,降到gcc-5版本,操作方法如下:
sudo apt-get install gcc-5 g++-5
rm -rf /usr/bin/gcc
rm -rf /usr/bin/g++
sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc -f
sudo ln -s /usr/bin/g++-5 /usr/bin/g++ -f
然后通过使用CUDA中的sample进行编译运行,具体如下:
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
sudo ./deviceQuery
注意:一定要用管理员权限执行,不然会报错,错误信息如下:
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
cudaGetDeviceCount returned 30
-> unknown error
Result = FAIL
我在搭建环境的过程中被这个错误坑了很久。
执行后看到如下打印信息,证明CUDA安装OK
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce 940M"
CUDA Driver Version / Runtime Version 9.0 / 8.0
CUDA Capability Major/Minor version number: 5.0
Total amount of global memory: 2003 MBytes (2100232192 bytes)
( 3) Multiprocessors, (128) CUDA Cores/MP: 384 CUDA Cores
GPU Max Clock rate: 1176 MHz (1.18 GHz)
Memory Clock rate: 900 Mhz
Memory Bus Width: 64-bit
L2 Cache Size: 1048576 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,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,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 = 9.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce 940M
Result = PASS
第五步 安装cuDNN
第六步 安装tensorflow-gpu
conda create -n tensorflow-gpu -p /media/kapoo/DATA1/ubuntu-app/tensorflow-gpu
第七步 切换环境
source activate tensorflow-gpu
附录:
1、参考链接
http://blog.csdn.net/ZWX2445205419/article/details/69429518
http://blog.csdn.net/huuuuusy/article/details/78008195?locationNum=1&fps=1