ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN

ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN

  • 1 安装显卡驱动
    • 1.1 禁止secure boot
    • 1.2 安装闭源驱动
    • 1.3 重启查看驱动
  • 2 安装CUDA
    • 2.1 下载
    • 2.2 安装CUDA
    • 2.3 配置环境
    • 2.4 查看CUDA
  • 3 安装cuDNN
    • 3.1 下载
    • 3.2 安装cuDNN
    • 3.3 查看cuDNN
  • 4 安装gpu版Pytorch
  • 5 版本冲突解决方法
  • 6 自动安装驱动的方法(简便)

1 安装显卡驱动

1.1 禁止secure boot

这一步很重要,如果bios中为enable,驱动将安装失败。

windows每次开机有可能自动开启secure boot,所以安装前要确认为禁用状态

1.2 安装闭源驱动

  • 图形模式安装
ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN_第1张图片
  • 命令行模式安装

    zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ ubuntu-drivers devices
    
    == /sys/devices/pci0000:00/0000:00:1c.0/0000:01:00.0 ==
    modalias : pci:v000010DEd00001D12sv0000103Csd0000834Cbc03sc02i00
    vendor   : NVIDIA Corporation
    model    : GP108M [GeForce MX150]
    driver   : nvidia-driver-390 - distro non-free
    driver   : nvidia-driver-418-server - distro non-free
    driver   : nvidia-driver-450-server - distro non-free
    driver   : nvidia-driver-450 - distro non-free
    driver   : nvidia-driver-455 - distro non-free recommended
    driver   : nvidia-driver-440-server - distro non-free
    driver   : xserver-xorg-video-nouveau - distro free builtin
    

    这里选择 recommended,即:

    sudo apt install nvidia-driver-455
    

1.3 重启查看驱动

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/桌面$ nvidia-smi 
Sun Nov 29 16:12:30 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.38       Driver Version: 455.38       CUDA Version: 11.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce MX150       Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   58C    P0    N/A /  N/A |    263MiB /  2002MiB |      3%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1337      G   /usr/lib/xorg/Xorg                125MiB |
|    0   N/A  N/A      1508      G   /usr/bin/gnome-shell              137MiB |
+-----------------------------------------------------------------------------+

如上图所示,显卡安装成功。

如果不放心可以用下面的指令查看:

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/桌面$ neofetch 

            .-/+oossssoo+/-.               zjy@zjy-HP-ENVY-Laptop-13-ad0xx 
        `:+ssssssssssssssssss+:`           ------------------------------- 
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 20.04.1 LTS x86_64 
    .ossssssssssssssssssdMMMNysssso.       Host: HP ENVY Laptop 13-ad0xx 
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Kernel: 5.4.0-54-generic 
  +ssssssssshmydMMMMMMMNddddyssssssss+     Uptime: 1 min 
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Packages: 1741 (dpkg), 6 (snap) 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Shell: bash 5.0.17 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Resolution: 2560x1440 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   DE: GNOME 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: Mutter 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   WM Theme: Adwaita 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Theme: Yaru [GTK2/3] 
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Icons: Yaru [GTK2/3] 
  +sssssssssdmydMMMMMMMMddddyssssssss+     Terminal: gnome-terminal 
   /ssssssssssshdmNNNNmyNMMMMhssssss/      CPU: Intel i5-7200U (4) @ 3.100GHz 
    .ossssssssssssssssssdMMMNysssso.       GPU: Intel HD Graphics 620 
      -+sssssssssssssssssyyyssss+-         GPU: NVIDIA GeForce MX150 
        `:+ssssssssssssssssss+:`           Memory: 840MiB / 7715MiB 
            .-/+oossssoo+/-.

正确识别到了核显和独显。

2 安装CUDA

2.1 下载

下载地址:https://developer.nvidia.cn/cuda-downloads
安装指南:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN_第2张图片

2.2 安装CUDA

执行:

sudo sh cuda_11.1.0_455.23.05_linux.run
x  End User License Agreement                                                  x
x  -                                                                           x
x  NVIDIA Software License Agreement and CUDA Supplement to                    x
x  Software License Agreement.                                                 x
x                                                                              x
x  Preface                                                                     x
x  -                                                                           x
x  The Software License Agreement in Chapter 1 and the Supplement              x
x  in Chapter 2 contain license terms and conditions that govern               x
x  the use of NVIDIA software. By accepting this agreement, you                x
x  agree to comply with all the terms and conditions applicable                x
x  to the product(s) included herein.                                          x
x                                                                              x
x  NVIDIA Driver                                                               x
                                                                               x
x Do you accept the above EULA? (accept/decline/quit): 

输入accept,回车。

│ CUDA Installer
│ - [ ] Driver
│      [ ] 455.23.05
│ + [X] CUDA Toolkit 11.1[X] CUDA Samples 11.1[X] CUDA Demo Suite 11.1[X] CUDA Documentation 11.1
│   Options
│   Install

在对应选项上按空格(space)取消勾选Driver,再向下选中Install,回车。
ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN_第3张图片
安装完成后显示:

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

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

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

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.1/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least .00 is required for CUDA 11.1 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

2.3 配置环境

装完成后,需要配置环境变量,编辑~/.bashrc文件,如果其他用户需要使用cuda,再按照上述步骤添加环境变量并更新即可:

sudo gedit ~/.bashrc

在末尾添加:

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

添加后更新环境变量:

source ~/.bashrc

cuda安装目录为/usr/local/cuda-xxxxxx为版本号,同时,cuda还会建立一个/usr/local/cuda同步链接,所以可以直接将该路径添加至环境变量,之后更换cuda版本,也不需要修改环境变量。

参考:Ubuntu18.04 LTS 使用CUDA11.1编译TensoFlow-GPU版本

2.4 查看CUDA

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/下载$ nvcc -V

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Tue_Sep_15_19:10:02_PDT_2020
Cuda compilation tools, release 11.1, V11.1.74
Build cuda_11.1.TC455_06.29069683_0

3 安装cuDNN

3.1 下载

下载地址:https://developer.nvidia.cn/rdp/cudnn-download
ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN_第4张图片
这里选择cuDNN Library for Linux (x86_64)

安装指南:https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html

3.2 安装cuDNN

  1. Navigate to your directory containing the cuDNN tar file.

  2. Unzip the cuDNN package.

    tar -xvf cudnn-linux-x86_64-8.x.x.x_cudaX.Y-archive.tar.xz
    
  3. Copy the following files into the CUDA Toolkit directory.

    sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include 
    sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64 
    sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
    

3.3 查看cuDNN

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

#define CUDNN_MAJOR 8
#define CUDNN_MINOR 0
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#endif /* CUDNN_VERSION_H */

安装成功。

4 安装gpu版Pytorch

选择对应版本的pytorch:https://pytorch.org/
ubuntu20.04安装nvidia显卡驱动/CUDA/cuDNN_第5张图片
在新环境中安装,并测试:

(pytorch_gpu) zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~$ python
Python 3.7.9 (default, Aug 31 2020, 12:42:55) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import torch
>>> torch.cuda.is_available()
True #成功

5 版本冲突解决方法

Nvidia 显卡 Failed to initialize NVML Driver/library version mismatch 错误解决方案

6 自动安装驱动的方法(简便)

# zjy @ 3090-711 in ~ [12:30:32] 
$ sudo ubuntu-drivers devices    
 
== /sys/devices/pci0000:64/0000:64:00.0/0000:65:00.0/0000:66:10.0/0000:68:00.0 ==
modalias : pci:v000010DEd00002204sv000019DAsd00001625bc03sc00i00
vendor   : NVIDIA Corporation
driver   : nvidia-driver-470 - distro non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-510 - distro non-free recommended
driver   : xserver-xorg-video-nouveau - distro free builtin

如果是要安装recommended,则直接执行:

$ sudo ubuntu-drivers autoinstall

否则:

$ sudo apt-get install nvidia-driver-xxx

最后,记得重启,再查看nvidia-smi即可看到显卡。

你可能感兴趣的:(nvidia,ubuntu,cuda,cudnn)