新装操作系统Ubuntu18.04上安装NVIDIA驱动、CUDA、CUDNN

  • Post author:
  • Post category:其他




1. NVIDIA显卡驱动

过程不难,就是命令记不住。。。

  1. 驱动下载


官网

下载NVIDIA-Linux-x86_64-440.44.run

  1. 卸载nouveau

编辑配置文件

$ sudo vi /etc/modprobe.d/blacklist.conf  

添加一行

blacklist nouveau 

配置生效

sudo update-initramfs -u  

重启电脑检查禁用,若无输出则禁用成功

lsmod | grep nouveau
  1. 安装依赖
$ sudo apt-get install gcc make
  1. 禁用X服务
$ sudo /etc/init.d/lightdm stop
  1. 安装驱动
$ sudo chmod a+x NVIDIA-Linux-x86_64-440.44.run
$ sudo ./NVIDIA-Linux-x86_64-440.44.run
  1. 检查安装
$ nvidia-smi
Wed Aug 12 14:56:44 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.44       Driver Version: 440.44       CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| 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 1070    Off  | 00000000:01:00.0  On |                  N/A |
| 51%   36C    P8    10W / 180W |   1085MiB /  8118MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1755      G   /usr/lib/xorg/Xorg                           478MiB |
|    0      3312      G   /opt/teamviewer/tv_bin/TeamViewer              2MiB |
|    0      3398      G   /usr/bin/gnome-shell                         263MiB |
|    0      6194      G   ...AAAAAAAAAAAACAAAAAAAAAA= --shared-files   181MiB |
|    0      6834      G   ...quest-channel-token=3054035920459279912    78MiB |
|    0     10301      G   /usr/local/sunlogin/bin/sunloginclient         5MiB |
|    0     10536      G   /usr/local/sunlogin/bin/sunloginclient        68MiB |
+-----------------------------------------------------------------------------+



2. CUDA


  1. 下载

    安装包,如我的文件是

    cuda_10.2.89_440.33.01_linux.run

  2. cd到下载目录下执行run文件

$ sudo ./cuda_10.2.89_440.33.01_linux.run

下图输入

accept

执行下一步

选择安装项,Enter键是选择或取消,注意Driver不需要再安装,真正有用的只有第二个CUDA Toolkit。这里其他示例、文档也选择了安装。

出现下面内容表示安装成功

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

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-10.2/
Samples:  Installed in /home/dreamdeck/, but missing recommended libraries

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

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-10.2/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.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 440.00 is required for CUDA 10.2 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log
  1. 更改环境变量
$ sudo gedit ~/.bashrc

添加以下内容

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

保存文件后激活环境变量

$ source ~/.bashrc
  1. 更改软链接文件

一般情况下上面过程会报错下面内容

/sbin/ldconfig.real: /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link

首先检查该文件,下面的输出表明它是一个文件,它本应是一个软连接。

$ ls -lh /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7
-rwxr-xr-x 1 root root 439M 91 11:55 /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7

于是创建软接连:

sudo ln -sf /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5 /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7



3. CUDNN


  1. 下载

    与CUDA版本对应的cudnn文件,我的文件是

    cudnn-10.2-linux-x64-v7.6.5.32.tgz
  2. 解压缩安装
$ tar -zxvf cudnn-10.2-linux-x64-v7.6.5.32.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
  1. 查看cudnn版本
$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

上面输出表示我的CUDNN版本是7.6.5


【码字不易,点个赞再走呗!(^ – ^)】