How do I install NVIDIA and CUDA drivers into Ubuntu?

Ubuntu 18.04, CUDA 10.1, libcudnn 7.5.1 and NVIDIA 418.67 drivers


Notes

  • 2021-01-07: Please use the 20.04 installation below moving forward as the steps are the same for both 18.04 and 20.04.

  • 2019-06-23: Recent updates with either the CUDA 10.0 or 10.1 versions the NVIDIA 418.67 driver, that installs with it, no longer has the 32bit libraries included and this will cause Steam and most games to no longer work. The version of libnvidia-gl-418:i386 only installs the 418.56 version which will not work with the 418.67 driver. Hopefully NVIDIA will release an update for that soon. I have added the info at the bottom of this answer in the .run file install part of how to download just the run file for the CUDA installer then you can use whatever driver you want. The run file is 2.3GB in size, so it might take a bit to download.

  • CUDA 9.x is not available through NVIDIA's ubuntu1804 repo. I did however write an answer for CUDA 9.2 at https://askubuntu.com/a/1086993/231142


Installing CUDA through the repository (instead of the .deb installation)

The following lines you can copy and paste to a terminal window. Press Ctrl+Alt+T to open a terminal window.

Remove and update

Remove any CUDA PPAs that may be setup and also remove the nvidia-cuda-toolkit if installed:

sudo rm /etc/apt/sources.list.d/cuda*
sudo apt remove --autoremove nvidia-cuda-toolkit

Recommended to also remove all NVIDIA drivers before installing new drivers:

sudo apt remove --autoremove nvidia-*

Then update the system:

sudo apt update

Add and install

Recently, I just found out that the CUDA installation works with the graphics-drivers ppa so if you don't have it added, add it now:

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

The install the NVIDIA driver. For this we are going to use the 440 driver

sudo apt install nvidia-driver-440

Now, install the key:

sudo apt-key adv --fetch-keys  http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

Add the repos:

sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'

sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda_learn.list'

Update the system again:

sudo apt update

Install CUDA 10.1:

sudo apt install cuda-10-1

It should be installing the NVIDIA 418.40 drivers with it as those are what are listed in the repo. See: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/

Install libcudnn7 7.5.1:

sudo apt install libcudnn7

Tune environment and reboot

Add the following lines to your ~/.profile file for CUDA 10.1

# set PATH for cuda 10.1 installation
if [ -d "/usr/local/cuda-10.1/bin/" ]; then
    export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
fi

Reboot your computer.

Check your settings

Check NVIDIA Cuda Compiler with nvcc --version:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Apr_24_19:10:27_PDT_2019
Cuda compilation tools, release 10.1, V10.1.168

Check libcudnn version /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn:

terrance@terrance-ubuntu:~$ /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn
    libcudnn.so.7 -> libcudnn.so.7.5.1

Check NVIDIA driver with nvidia-smi:

terrance@terrance-ubuntu:~$ nvidia-smi
Wed Jan 29 12:41:02 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.48.02    Driver Version: 440.48.02    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 750 Ti  Off  | 00000000:02:00.0  On |                  N/A |
| 40%   34C    P0     1W /  38W |    163MiB /  2000MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1993      G   /usr/lib/xorg/Xorg                           158MiB |
|    0      2502      G   compton                                        1MiB |
+-----------------------------------------------------------------------------+

.run file install

Install driver

By using the sudo add-apt-repository ppa:graphics-drivers/ppa you can install the 430.26 newest driver or any that suit your fancy.

Install libcudnn7

Add the Repo:

sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda_learn.list'

Install the key:

sudo apt-key adv --fetch-keys  http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

Update the system:

sudo apt update

Install libcudnn7.5.1:

sudo apt install libcudnn7

Download the .run file

Now download the cuda_10.1.105_418.39_linux.run from https://developer.nvidia.com/cuda-10.1-download-archive-base?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=runfilelocal

If you want CUDA 10.2 the download instructions are here: https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=runfilelocal Then follow through with the same steps as below but make sure to update to 10.2 instead of 10.1.

Then run the installer:

sudo sh cuda_10.1.105_418.39_linux.run

Type in accept and press enter on this screen:

┌──────────────────────────────────────────────────────────────────────────────┐
│  End User License Agreement                                                  │
│  --------------------------                                                  │
│                                                                              │
│                                                                              │
│  Preface                                                                     │
│  -------                                                                     │
│                                                                              │
│  The Software License Agreement in Chapter 1 and the Supplement              │
│  in Chapter 2 contain license terms and conditions that govern               │
│  the use of NVIDIA software. By accepting this agreement, you                │
│  agree to comply with all the terms and conditions applicable                │
│  to the product(s) included herein.                                          │
│                                                                              │
│                                                                              │
│  NVIDIA Driver                                                               │
│                                                                              │
│                                                                              │
│  Description                                                                 │
│                                                                              │
│  This package contains the operating system driver and                       │
│──────────────────────────────────────────────────────────────────────────────│
│ Do you accept the above EULA? (accept/decline/quit):                         │
│ accept                                                                       

Unselect the driver and then choose Install by using the arrow keys and space bar to move and select or unselect:

┌──────────────────────────────────────────────────────────────────────────────┐
│ CUDA Installer                                                               │
│ - [ ] Driver                                                                 │
│      [ ] 418.39                                                              │
│ + [X] CUDA Toolkit 10.1                                                      │
│   [X] CUDA Samples 10.1                                                      │
│   [X] CUDA Demo Suite 10.1                                                   │
│   [X] CUDA Documentation 10.1                                                │
│   Install                                                                    │
│   Options                                                                    │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│ Up/Down: Move | Left/Right: Expand | 'Enter': Select | 'A': Advanced options │

Wait for the install to finish, it might say errors during, but not to worry.

Tune environment and reboot

Add the following lines to your ~/.profile file for CUDA 10.1

# set PATH for cuda 10.1 installation
if [ -d "/usr/local/cuda-10.1/bin/" ]; then
    export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
fi

Reboot the system for the changes to take effect.


Ubuntu 20.04 LTS, CUDA 11.5.0, NVIDIA 495 and libcudnn 8.0.4


I don't recommend installing the NVIDIA drivers that come with CUDA as they do not contain the dkms drivers that carry over into new kernel upgrades.

The Ubuntu repositories now contain the same drivers as the graphics-drivers PPA. So feel free to install the 495.44 drivers.

sudo apt install nvidia-driver-495

Reboot the system so the new driver takes effect.

Now, download the CUDA 11.5.0 .run file from NVIDIA:

wget https://developer.download.nvidia.com/compute/cuda/11.5.0/local_installers/cuda_11.5.0_495.29.05_linux.run

Run the .run file as sudo:

sudo sh ./cuda_11.5.0_495.29.05_linux.run

If you get the following, just choose Continue:

┌──────────────────────────────────────────────────────────────────────────────┐
│ Existing package manager installation of the driver found. It is strongly    │
│ recommended that you remove this before continuing.                          │
│ Abort                                                                        │
│ Continue                                                                     │
│                                                                             

Accept the EULA:

┌──────────────────────────────────────────────────────────────────────────────┐
│  End User License Agreement                                                  │
│  --------------------------                                                  │
│                                                                              │
│  NVIDIA Software License Agreement and CUDA Supplement to                    │
│  Software License Agreement. Last updated: October 8, 2021                   │
│                                                                              │
│  The CUDA Toolkit End User License Agreement applies to the                  │
│  NVIDIA CUDA Toolkit, the NVIDIA CUDA Samples, the NVIDIA                    │
│  Display Driver, NVIDIA Nsight tools (Visual Studio Edition),                │
│  and the associated documentation on CUDA APIs, programming                  │
│  model and development tools. If you do not agree with the                   │
│  terms and conditions of the license agreement, then do not                  │
│  download or use the software.                                               │
│                                                                              │
│  Last updated: October 8, 2021.                                              │
│                                                                              │
│                                                                              │
│  Preface                                                                     │
│  -------                                                                     │
│                                                                              │
│──────────────────────────────────────────────────────────────────────────────│
│ Do you accept the above EULA? (accept/decline/quit):                         │
│ accept                                                                       

Unselect the video driver by pressing the spacebar while [X] Driver is highlighted:

┌──────────────────────────────────────────────────────────────────────────────┐
│ CUDA Installer                                                               │
│ - [ ] Driver                                                                 │
│      [ ] 495.29.05                                                           │
│ + [X] CUDA Toolkit 11.5                                                      │
│   [X] CUDA Samples 11.5                                                      │
│   [X] CUDA Demo Suite 11.5                                                   │
│   [X] CUDA Documentation 11.5                                                │
│   Options                                                                    │
│   Install                                                                    │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│ Up/Down: Move | Left/Right: Expand | 'Enter': Select | 'A': Advanced options │

Then press the down arrow to Install. Press Enter then wait for installation to complete.

After the installation is complete add the following to the bottom of your ~/.profile or add it to the /etc/profile.d/cuda.sh file which you might have to create for all users (global):

# set PATH for cuda 11.5 installation
if [ -d "/usr/local/cuda-11.5/bin/" ]; then
    export PATH=/usr/local/cuda-11.5/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-11.5/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
fi

Install libcudnn8

Add the Repo:

NOTE: The 20.04 repo from NVIDIA does not supply libcudnn but the 18.04 repo does and installs just fine into 20.04.

echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda_learn.list

Install the key:

sudo apt-key adv --fetch-keys  http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

Update the system:

sudo apt update

Install libcudnn 8.0.4:

sudo apt install libcudnn8

I recommend now to reboot the system for the changes to take effect.

After it reboots check the installations:

~$ nvidia-smi
Thu Nov 18 07:31:31 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 495.44       Driver Version: 495.44       CUDA Version: 11.5     |
|-------------------------------+----------------------+----------------------+
| 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  NVIDIA GeForce ...  Off  | 00000000:01:00.0  On |                  N/A |
| 40%   38C    P8     1W /  38W |    310MiB /  2000MiB |      4%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      2091      G   /usr/lib/xorg/Xorg                 46MiB |
|    0   N/A  N/A      2680      G   /usr/lib/xorg/Xorg                163MiB |
|    0   N/A  N/A      2906      G   compton                             1MiB |
|    0   N/A  N/A      3262      G   /opt/waterfox/waterfox             85MiB |
+-----------------------------------------------------------------------------+

and check CUDA install:

~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Mon_Sep_13_19:13:29_PDT_2021
Cuda compilation tools, release 11.5, V11.5.50
Build cuda_11.5.r11.5/compiler.30411180_0

and check libcudnn install:

~$ /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn
    libcudnn_cnn_infer.so.8 -> libcudnn_cnn_infer.so.8.0.4
    libcudnn.so.8 -> libcudnn.so.8.0.4
    libcudnn_adv_train.so.8 -> libcudnn_adv_train.so.8.0.4
    libcudnn_ops_infer.so.8 -> libcudnn_ops_infer.so.8.0.4
    libcudnn_cnn_train.so.8 -> libcudnn_cnn_train.so.8.0.4
    libcudnn_adv_infer.so.8 -> libcudnn_adv_infer.so.8.0.4
    libcudnn_ops_train.so.8 -> libcudnn_ops_train.so.8.0.4

I just want to add an alternative solution that ends up being much easier and modular, IMHO: use Nvidia's CUDA docker images!

This does require a working (proprietary) driver on the host machine, which can be installed from Ubuntu repos (nvidia-driver-470 is the final supported driver version for CUDA compute 3.5).

  1. sudo apt install nvidia-driver-470
  2. Install Docker and nvidia-docker2. See this guide from Nvidia; if you're running Ubuntu 21+ you'll want to replace $distribution with ubuntu2004 in this step.
  3. Find the CUDA docker image you want on Nvidia's DockerHub page; for example, if you want CUDA 11.4.2 and cuDNN 8 you could run
    $ docker pull nvidia/cuda:11.4.2-cudnn8-devel-ubuntu20.04
    
  4. Now run a container from that image, attaching your GPUs:
    $ docker run -it --rm --gpus all nvidia/cuda:11.4.2-cudnn8-devel-ubuntu20.04
    
    You should verify the container can see your GPU by running nvidia-smi, which will show the same output you get from running nvidia-smi NOT inside of Docker.