Ubuntu 18.04 + Python3.6 + CUDA-10.0 + CUDNN-7.5.0 + tensorflow-gpu-1.13.1

转自:https://blog.csdn.net/ycfn97/article/details/100084641

Install nvidia driver

首先卸载原有驱动

sudo apt-get purge nvidia

添加图形驱动程序到源列表:

 sudo add-apt-repository ppa:graphics-drivers/ppa
 sudo apt update
 sudo apt upgrade

检查将安装什么驱动程序:

ubuntu-drivers devices
root@boss-To-be-filled-by-O-E-M:/home/boss# ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001E04sv00007377sd00001730bc03sc00i00
vendor   : NVIDIA Corporation
driver   : nvidia-driver-430 - third-party free recommended
driver   : nvidia-driver-410 - third-party free
driver   : nvidia-driver-415 - third-party free
driver   : xserver-xorg-video-nouveau - distro free builtin

注意!!!虚拟机无法安装NVIDIA显卡驱动程序,虚拟机中只有虚拟显卡没有NVIDIA显卡,不能安装NVIDIA驱动,无论是在NVIDIA官网下载NVIDIA-Linux-x86_64-430.40.run还是ppa下载都是没用的,也无法安装TensorFlow-gpu,虚拟机只能用cpu版本的TensorFlow。如果是考虑“虚拟一个系统”来使用显卡的话,考虑容器化技术——Docker大家可以参考这位大佬的文章虚拟机上无法安装tensorflow-GPU版本

虚拟机驱动程序是VM,,,

== /sys/devices/pci0000:00/0000:00:0f.0 ==
modalias : pci:v000015ADd00000405sv000015ADsd00000405bc03sc00i00
vendor   : VMware
model    : SVGA II Adapter
driver   : open-vm-tools-desktop - distro free

自动安装最新的驱动程序

sudo ubuntu-drivers autoinstall

显示

This system doesn\'t support Secure Boot
Secure Boot not enabled on this system.
Done.

nvidia:
Running module version sanity check.

- Original module
  - No original module exists within this kernel
- Installation
  - Installing to /lib/modules/5.0.0-25-generic/updates/dkms/

nvidia-modeset.ko:
Running module version sanity check.

- Original module
  - No original module exists within this kernel
- Installation
  - Installing to /lib/modules/5.0.0-25-generic/updates/dkms/

nvidia-drm.ko:
Running module version sanity check.

- Original module
  - No original module exists within this kernel
- Installation
  - Installing to /lib/modules/5.0.0-25-generic/updates/dkms/

nvidia-uvm.ko:
Running module version sanity check.

- Original module
  - No original module exists within this kernel
- Installation
  - Installing to /lib/modules/5.0.0-25-generic/updates/dkms/

depmod...

DKMS: install completed.

表示安装成功

然后重启机器:

sudo reboot

检查正确的安装驱动程序:

nvidia-smi

显示 说明NVIDIA驱动安装成功

root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/bandwidthTest# nvidia-smi
Tue Aug 27 20:03:00 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.40       Driver Version: 430.40       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| 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 RTX 208...  Off  | 00000000:01:00.0  On |                  N/A |
| 26%   32C    P8     9W / 250W |    302MiB / 11016MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1135      G   /usr/lib/xorg/Xorg                            16MiB |
|    0      1186      G   /usr/bin/gnome-shell                          58MiB |
|    0      1422      G   /usr/lib/xorg/Xorg                            98MiB |
|    0      1566      G   /usr/bin/gnome-shell                         127MiB |
+-----------------------------------------------------------------------------+

这里也可以下载并安装 Cuda 和 Nvidia 驱动一起执行,分开安装的筒子们请略过下面的部分直接跳到 install cuda&cudnn部分

卸载所有原驱动

$ sudo apt-get purge nvidia*

禁用nouveau

新建-blacklist-nouveau.conf 输⼊指令:

$ sudo vim /etc/modprobe.d/blacklist-nouveau.conf

往 blacklist-nouveau.conf 文件中写⼊:

blacklist nouveau
options nouveau modeset=0

禁 Ubuntu 自带开源驱动nouveau,写入后保存重启

$ sudo reboot 

重启后在终端执行行命令:

$ lsmod | grep nouveau

查看nouveau模块是否被加载,若无输出,则执行下一步

禁⽤X服务

$ sudo service lightdm stop

注意!!!在Ubuntu禁用X进入tty1终端后登陆用户输入密码时不能用小键盘,系统不识别,别问我怎么知道的 )逃…

确定下载的 cuda 版本后执行指令

$ sudo chmod 777 cuda_9.0.176_384.81_linux.run
$ sudo ./cuda_9.0.176_384.81_linux.run --no-opengl-libs

进入 CUDA 安装中同意安装 Nvidia 驱动

[accept] #同意安装
[y] #安装Driver,将自动安装CUDA版本相匹配的Nvidia驱动
[y] #安装CUDA Toolkit install
#安装到默认目录
[y] #创建安装目录的软链接
[n] #不复制Samples,因为在安装目录下有/samples

安装完成后会显示 CUDA 和 Nvidia 驱动成功安装

后面vim 打开.bashrc 在末行加⼊命令详见install cuda&cudnn部分

Install cuda&cudnn

各个版本cuda下载地址

cd 下载目录
sudo sh cuda_10.0.130_410.48_linux.run --override --silent --toolkit

各个版本cudnn下载地址

cd 下载目录
tar -xzvf cudnn-10.0-linux-x64-v7.5.0.56.solitairetheme8
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

现在必须为/.bashrc添加一些路径:

gedit ~/.bashrc

在/.bashrc后面加上

export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda

现在重新加载你的终端配置:

source ~/.bashrc
 sudo ldconfig

检查路径是否正确安装:

echo $CUDA_HOME

测试版本号

查看 CUDA 版本:

cat /usr/local/cuda/version.txt

查看 CUDNN 版本:

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

也可以这样测试cuda安装情况

编译并测试设备 deviceQuery:

root@boss-To-be-filled-by-O-E-M:/home/boss/download# nvcc -V

输出如下

root@boss-To-be-filled-by-O-E-M:/home/boss/download# nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130

编译并测试带宽 bandwidthTest:

$ cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery
$ sudo make
$ ./deviceQuery

输出如下

root@boss-To-be-filled-by-O-E-M:/home/boss/download# cd /usr/local/cuda-10.0/samples/1_Utilities/deviceQuery
root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/deviceQuery# sudo make
"/usr/local/cuda-10.0"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o deviceQuery.o -c deviceQuery.cpp
"/usr/local/cuda-10.0"/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o deviceQuery deviceQuery.o 
mkdir -p ../../bin/x86_64/linux/release
root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/deviceQuery# ./deviceQuery
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce RTX 2080 Ti"
  CUDA Driver Version / Runtime Version          10.1 / 10.0
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 11017 MBytes (11552096256 bytes)
  (68) Multiprocessors, ( 64) CUDA Cores/MP:     4352 CUDA Cores
  GPU Max Clock rate:                            1545 MHz (1.54 GHz)
  Memory Clock rate:                             7000 Mhz
  Memory Bus Width:                              352-bit
  L2 Cache Size:                                 5767168 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 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:  1024
  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 3 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 supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      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 = 10.1, CUDA Runtime Version = 10.0, NumDevs = 1
Result = PASS

编译并测试带宽 bandwidthTest:

$ cd ../bandwidthTest
$ sudo make
$ ./bandwidthTest

输出如下

root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/deviceQuery# cd ../bandwidthTest
root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/bandwidthTest# sudo make
"/usr/local/cuda-10.0"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o bandwidthTest.o -c bandwidthTest.cu
"/usr/local/cuda-10.0"/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o bandwidthTest bandwidthTest.o 
mkdir -p ../../bin/x86_64/linux/release
cp bandwidthTest ../../bin/x86_64/linux/release
root@boss-To-be-filled-by-O-E-M:/usr/local/cuda-10.0/samples/1_Utilities/bandwidthTest# ./bandwidthTest
[CUDA Bandwidth Test] - Starting...
Running on...

 Device 0: GeForce RTX 2080 Ti
 Quick Mode

 Host to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12162.1

 Device to Host Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12451.7

 Device to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     513231.8

Result = PASS

如果两个测试的结果都是 Result = PASS CUDA ,说明安装成功

安装TensorFlow-gpu

这里我选择了清华大学的开源镜像以提高下载速度,提示出现pip not found ,不要直接复制提示安装pip,要根据Python版本下载你想要的pip,否则会下载Python2.7

root@sunqi-To-be-filled-by-O-E-M:/# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
Command \'pip\' not found, but can be installed with:
apt install python-pip
root@sunqi-To-be-filled-by-O-E-M:/# apt install python-pip

直接复制提示就会变成下面这样要修改Python默认版本,不要这样

root@sunqi-To-be-filled-by-O-E-M:/# pip -V
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

修改Python默认版本

详见我的另一篇博客linux修改Python默认版本

修改完Python默认版本如果还不改,那么依然会是Python2.7的pip

root@sunqi-To-be-filled-by-O-E-M:/# apt install python-pip
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
python-pip 已经是最新版 (9.0.1-2.3~ubuntu1.18.04.1)。

pip -V还是no module named pip

root@sunqi-To-be-filled-by-O-E-M:/# pip -V
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ModuleNotFoundError: No module named \'pip\'

必须这样安装

root@sunqi-To-be-filled-by-O-E-M:/# apt install python3-pip

然后再查看pip版本是否符合要求

pip -v

如果pip -v出现报错:bash: /usr/bin/pip: 没有那个文件或目录 可参考pip -v报错:bash: /usr/bin/pip: 没有那个文件或目录

更新pip

sudo pip install --upgrade pip

安装Tensorflow-gpu

pip install --user tensorflow-gpu

这里还有一种方法用bazel编译,需要下载一些依赖库,就不赘述了,感性趣的筒子们自行百度

在此,TensorFlow-gpu环境就已经安装完成,下面进行测试

import tensorflow as tf
hello=tf.constant("Hello,Tensorflow!")
sees=tf.Session()
print(sees.run(hello))

如果得到了输出hello,TensorFlow!那么恭喜你完成了DeepLearning基本环境的配置

如果出错,请检查NVIDIA驱动版本和cuda以及cudnn版本是否正确匹配

常见问题

 I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library \'libcudart.so.10.0\'; dlerror: libcudart.so.10.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64

详见cuda程序执行出错: libcudart.so.10.0: cannot open shared object file: No such file or directory

听说看完了会点赞的都是美女帅哥