Install nvidia graphics card driver/CUDA/cuDNN on ubuntu20.04

Install nvidia graphics driver/CUDA/cuDNN on ubuntu20.04

  • 1 Install the graphics card driver
    • 1.1 Disable secure boot
    • 1.2 Install closed source driver
    • 1.3 Restart to view the driver
  • 2 Install CUDA
    • 2.1 download
    • 2.2 Install CUDA
    • 2.3 Configuration environment
    • 2.4 View CUDA
  • 3 Install cuDNN
    • 3.1 download
    • 3.2 Install cuDNN
    • 3.3 View cuDNN
  • 4 Install gpu version of Pytorch
  • 5 version conflict resolution
  • 6 How to install the driver automatically (simple)

1 Install graphics driver

1.1 Disable secure boot

This step is very important, if it is enabled in the bios, the driver will fail to install.

Windows may automatically enable secure boot every time it is turned on, so be sure to disable it before installation.

1.2 Install closed source driver

  • Graphical mode installation

  • Command line mode installation

    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
    

    Select recommended here, namely:

    sudo apt install nvidia-driver-455
    

1.3 Restart to view the driver

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/desktop$ 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 |
 + ------------------------------------------------- ---------------------------- +

As shown in the figure above, the graphics card is installed successfully.

If you are worried, you can use the following command to check:

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

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

The core display and discrete display are correctly identified.

2 Install CUDA

2.1 Download

Download address: https://developer.nvidia.cn/cuda-downloads
Installation guide: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

2.2 Install CUDA

implement:

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):

Enter accept and press Enter.

│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

Press space on the corresponding option to uncheck Driver, then select Install, and press Enter.

After the installation is complete, it will display:

===========
=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 Configuration environment

After the installation is complete, you need to configure environment variables and edit the ~/.bashrc file. If other users need to use cuda, then follow the above steps to add environment variables and update them:

sudo gedit ~/.bashrc

Add at the end:

export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_HOME}/lib64
export PATH=${CUDA_HOME}/bin:${<!-- -->PATH}

Update environment variables after adding:

source ~/.bashrc

The cuda installation directory is /usr/local/cuda-xxx, xxx is the version number, at the same time, cuda will also create a /usr/local/cuda code> synchronization link, so you can directly add the path to the environment variable, and then change the cuda version without modifying the environment variable.

Reference: Ubuntu18.04 LTS uses CUDA11.1 to compile TensoFlow-GPU version

2.4 View CUDA

zjy@zjy-HP-ENVY-Laptop-13-ad0xx:~/download$ 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 Install cuDNN

3.1 Download

Download address: https://developer.nvidia.cn/rdp/cudnn-download

Here select cuDNN Library for Linux (x86_64)

Installation guide: https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html

3.2 Install 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 View 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 */

Successful installation.

4 Install gpu version of Pytorch

Select the corresponding version of pytorch: https://pytorch.org/

Install and test in a new environment:

(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 #success

5 version conflict resolution

Nvidia graphics card Failed to initialize NVML Driver/library version mismatch error solution

6 The method of automatic driver installation (simple)

#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

If you want to install recommended, execute directly:

$ sudo ubuntu-drivers autoinstall

otherwise:

$ sudo apt-get install nvidia-driver-xxx

Finally, remember to restart, and then check nvidia-smi to see the graphics card.