Ubuntu18.04下安装(1080Ti)NVIDIA驱动、CUDA9.2、cuDNN7.3进行GPU加速

1. 查看相关工具版本的方法

查看NVIDIA驱动版本

nvidia-smi

查看CUDA版本

cat /usr/local/cuda/version.txt
或者 nvcc --version(设置并更新环境变量之后才可用)

查看cuDNN版本

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

信息更新(20190521)

(base) famir@ubuntu:~$ nvidia-smi
Tue May 21 19:29:29 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.56       Driver Version: 418.56       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 GTX 108...  Off  | 00000000:01:00.0  On |                  N/A |
|  0%   42C    P8    19W / 250W |    468MiB / 11178MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1894      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1960      G   /usr/bin/gnome-shell                          70MiB |
|    0      4407      G   /usr/lib/xorg/Xorg                           245MiB |
|    0      4602      G   /opt/teamviewer/tv_bin/TeamViewer              8MiB |
|    0      6832      G   ...uest-channel-token=16521326733675790024   122MiB |
+-----------------------------------------------------------------------------+
(base) famir@ubuntu:~$ cat /usr/local/cuda/version.txt
CUDA Version 10.0.130
(base) famir@ubuntu:~$ nvcc --version
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
(base) famir@ubuntu:~$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
#define CUDNN_MAJOR 7
#define CUDNN_MINOR 5
#define CUDNN_PATCHLEVEL 0
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

2. 安裝或更新NVIDIA驱动

1.安装之前先卸载已经存在的驱动版本:

sudo apt-get purge nvidia*
或者 sudo apt-get remove --purge nvidia*

2.需要禁用 nouveau,只有在禁用掉 nouveau 后才能顺利安装 NVIDIA 显卡驱动,禁用方法就是在 /etc/modprobe.d/blacklist-nouveau.conf 文件中添加一条禁用命令,首先需要打开该文件,通过以下命令打开:

sudo gedit /etc/modprobe.d/blacklist.conf

在最后一行加上: blacklist nouveau ,将Ubuntu自带的显卡驱动加入黑名单。Ctrl +S保存后注意此时还需在终端执行以下命令使禁用 nouveau 真正生效 : sudo update-initramfs -u

3.重启電腦后,输入命令sudo service lightdm stop关闭桌面服务,然后就可以安装驱动了。

4.在Ubuntu 18.04上安装NVIDIA有三种方法:

  • 使用标准Ubuntu仓库进行自动化安装
  • 使用PPA仓库进行自动化安装
  • 使用官方的NVIDIA驱动进行手动安装

在這裏使用第一種方法:即使用标准Ubuntu仓库进行自动化安装
这种方法几乎是所有的示例中最简单的方法,也是该教程最为推荐的方法。首先,检测你的NVIDIA显卡型号和推荐的驱动程序的模型。在命令行中输入如下命令:ubuntu-drivers devices,顯示如下:

== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001B06sv00001458sd00003752bc03sc00i00
vendor   : NVIDIA Corporation
model    : GP102 [GeForce GTX 1080 Ti]
driver   : nvidia-driver-396 - third-party free recommended
driver   : nvidia-driver-390 - third-party free
driver   : xserver-xorg-video-nouveau - distro free builtin

从输出结果可以看到,目前系统已连接Nvidia GeForce GTX 1080 Ti显卡,建议安装驱动程序是 nvidia-driver-396版本的驱动。如果您同意该建议,请再次使用Ubuntu驱动程序命令来安装所有推荐的驱动程序。
输入以下命令:sudo ubuntu-drivers autoinstall,安装结束,重新启动系统。

注:本次安装过程中,提示老版本驱动有一些包未删除干净,可使用命令sudo apt autoremove删除。

重启后输入以下指令进行验证: nvidia-smi ,若列出了GPU的信息列表则表示驱动安装成功。如下:

Fri Sep 28 11:01:49 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.54                 Driver Version: 396.54                    |
|-------------------------------+----------------------+----------------------+
| 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 GTX 108...  Off  | 00000000:01:00.0  On |                  N/A |
|  0%   44C    P0    64W / 250W |    309MiB / 11175MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1212      G   /usr/lib/xorg/Xorg                            18MiB |
|    0      1254      G   /usr/bin/gnome-shell                          49MiB |
|    0      1570      G   /usr/lib/xorg/Xorg                           104MiB |
|    0      1749      G   /usr/bin/gnome-shell                          89MiB |
|    0      1796      G   /opt/teamviewer/tv_bin/TeamViewer              2MiB |
|    0      2371      G   /proc/self/exe                                40MiB |
+-----------------------------------------------------------------------------+

或查看设置>详细信息>图形:
Ubuntu18.04下安装(1080Ti)NVIDIA驱动、CUDA9.2、cuDNN7.3进行GPU加速_第1张图片
注:重装Ubuntu后,使用第一种方法系统推荐396版本:

➜  ~ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001B06sv00001458sd00003752bc03sc00i00
vendor   : NVIDIA Corporation
model    : GP102 [GeForce GTX 1080 Ti]
driver   : nvidia-driver-396 - third-party free recommended
driver   : nvidia-driver-390 - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin

于是使用第二种方法自己安装最新的410版本:

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

ubuntu-drivers devices  #查看自己的显卡及可以安装的驱动版本,显示如下:
	== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
	modalias : pci:v000010DEd00001B06sv00001458sd00003752bc03sc00i00
	vendor   : NVIDIA Corporation
	model    : GP102 [GeForce GTX 1080 Ti]
	driver   : nvidia-driver-390 - third-party free
	driver   : nvidia-driver-410 - third-party free recommended
	driver   : nvidia-driver-396 - third-party free
	driver   : xserver-xorg-video-nouveau - distro free builtin
	
sudo apt install nvidia-driver-410

完成后重启系统。

还可以使用软件和更新来安装或更新驱动:
Ubuntu18.04下安装(1080Ti)NVIDIA驱动、CUDA9.2、cuDNN7.3进行GPU加速_第2张图片

本部分参考:
Ubuntu 16.04 禁用 nouveau 安装 nvidia显卡驱动
Ubuntu 18.04 NVIDIA驱动安装总结

3. 安装CUDA9.2

安装之前先到NVIDIA官网下载对应版本cuda和cudnn工具文件
https://developer.nvidia.com/cuda-toolkit-archive
https://developer.nvidia.com/cudnn (需注册)
此处下载如下版本文件:

cuda_9.2.148_396.37_linux.run  # cuda文件(1.8G)
cuda_9.2.148.1_linux.run            # cuda补丁(85.6M)
cudnn-9.2-linux-x64-v7.3.1.20.tgz   # cudnn文件(368.1M)

安装依赖:

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev libfreeimage3 libfreeimage-dev
# 否则安装cuda后会出现提示:
	Missing recommended library: libGLU.so
	Missing recommended library: libX11.so
	Missing recommended library: libXi.so
	Missing recommended library: libXmu.so
	Missing recommended library: libGL.so

终端输入:sudo sh cuda_9.2.148_396.37_linux.run
空格到底或者输入q退出阅读EULA,除了提问是否安装显卡驱动时选择no,其他均选yes或默认路径。显示如下:

Installing the CUDA Toolkit in /usr/local/cuda-9.2 ...
Installing the CUDA Samples in /home/famir ...
Copying samples to /home/famir/NVIDIA_CUDA-9.2_Samples now...
Finished copying samples.

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-9.2
Samples:  Installed in /home/famir

Please make sure that
 -   PATH includes /usr/local/cuda-9.2/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-9.2/lib64, or, add /usr/local/cuda-9.2/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.2/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.2/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.2 functionality to work.
To install the driver using this installer, run the following command, replacing  with the name of this run file:
    sudo .run -silent -driver

Logfile is /tmp/cuda_install_20157.log

安装补丁:sudo sh cuda_9.2.148.1_linux.run
设置cuda环境变量(比较重要,不然samples跑不起来):

gedit ~/.bashrc(我的是gedit ~/.shrc,因为我更改了默认shell)
# 在最后加入两行:(按照安装提示修改成自己的路径)
	export PATH=/usr/local/cuda/bin:$PATH
	export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
# 保存退出之后,运行:
source ~/.bashrc(我的是source ~/.zshrc,或者重启终端)

cuda测试:

# 在目录/home/famir/NVIDIA_CUDA-9.2_Samples/1_Utilities/deviceQuery中打开终端
make
./deviceQuery

最后出现:Result = PASS 即可。
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.0, CUDA Runtime Version = 9.2, NumDevs = 1
Result = PASS

4. 安装cuDNN7.3

cuDNN 的安装,就是将 cuDNN 包内的文件,拷贝到cuda文件夹中即可。

# 进入文件cudnn-9.2-linux-x64-v7.3.1.20.tgz放置目录,在终端打开,依次执行:
tar -xzvf cudnn-9.2-linux-x64-v7.3.1.20.tgz
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*

部分参考:
NVIDIA官方教程
真实机下 ubuntu 18.04 安装GPU +CUDA+cuDNN 以及其版本选择(亲测非常实用)
Ubuntu18:装GPU TensorFlow

你可能感兴趣的:(Linux)