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 16.04, CUDA 9.2 and NVIDIA 396 drivers:

NOTE: NVIDIA's repo here has decided to push the 410 drivers. I will do some testing to see if I can get it to set for the driver you want installed.

These instructions are for 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 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 nvidia-cuda-toolkit

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

sudo apt remove nvidia-*

Then update the system:

sudo apt update

Install the key:

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

Add the repo:

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

Update for the new repo being added:

sudo apt update

Install CUDA 9.2:

sudo apt install cuda-9-2

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

Verify that CUDA 9.2 was installed:

~$ ls /usr/local/cuda-9.2/
bin     include    libnvvp           nvml    samples  targets
doc     lib64      LICENSE           nvvm    share    tools
extras  libnsight  nsightee_plugins  README  src      version.txt

Now, add the following to your ~/.profile for the PATH and LD_LIBRARY. You can use the command gedit ~/.profile for editing:

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

Reboot your system.

sudo reboot

Once the system is up, you can verify the installation by typing in the following:

nvcc -V

You should see something similar the following:

~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85

And you should see the 396.xx drivers installed:

~$ nvidia-smi
Thu May 17 07:38:54 2018        
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.44                 Driver Version: 396.44                    |
|-------------------------------+----------------------+----------------------+
| 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 760     Off  | 00000000:02:00.0 N/A |                  N/A |
| 49%   53C    P0    N/A /  N/A |    187MiB /  1999MiB |     N/A      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0                    Not Supported                                       |
+-----------------------------------------------------------------------------+


Ubuntu 20.04 LTS, CUDA 11.2.0, NVIDIA 455 and libcudnn 8.0.4


Note: The NVIDIA driver that comes with CUDA 11.2.0 is the 460 driver which does not come with the 32bit drivers which can make apps like Steam fail due to missing 32bit libraries. This installation bypasses the installation of the proprietary NVIDIA 460 drivers and uses the graphics-drivers PPA instead. This also allows for the driver to stay installed after a kernel update.

If you don't have the graphics-drivers PPA already setup, add it now to your system and remove any previous NVIDIA drivers.

sudo apt-add-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-455

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

wget https://developer.download.nvidia.com/compute/cuda/11.2.0/local_installers/cuda_11.2.0_460.27.04_linux.run

I like to make it executable:

chmod +x cuda_11.2.0_460.27.04_linux.run

Now install CUDA:

sudo ./cuda_11.2.0_460.27.04_linux.run 

Accept the EULA:

┌──────────────────────────────────────────────────────────────────────────────┐
│  End User License Agreement                                                  │
│  --------------------------                                                  │
│                                                                              │
│  NVIDIA Software License Agreement and CUDA Supplement to                    │
│  Software 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                                                               │
│                                                                              │
│                                                                              │
│──────────────────────────────────────────────────────────────────────────────│
│ Do you accept the above EULA? (accept/decline/quit):                         │
│ accept                                                                            

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

┌──────────────────────────────────────────────────────────────────────────────┐
│ CUDA Installer                                                               │
│ - [ ] Driver                                                                 │
│      [ ] 460.27.04                                                           │
│ + [X] CUDA Toolkit 11.2                                                      │
│   [X] CUDA Samples 11.2                                                      │
│   [X] CUDA Demo Suite 11.2                                                   │
│   [X] CUDA Documentation 11.2                                             │
│   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.2 installation
if [ -d "/usr/local/cuda-11.2/bin/" ]; then
    export PATH=/usr/local/cuda-11.2/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda-11.2/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 Jan  7 08:05:43 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.45.01    Driver Version: 455.45.01    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 GTX 750 Ti  On   | 00000000:01:00.0  On |                  N/A |
| 40%   45C    P0     2W /  38W |    212MiB /  2000MiB |      2%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      3096      G   /usr/lib/xorg/Xorg                189MiB |
|    0   N/A  N/A      5979      G   compton                             1MiB |
|    0   N/A  N/A     15230      G   ...AAAAAAAA== --shared-files       15MiB |
+-----------------------------------------------------------------------------+

~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Nov_30_19:08:53_PST_2020
Cuda compilation tools, release 11.2, V11.2.67
Build cuda_11.2.r11.2/compiler.29373293


~$ /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

Tags:

Cuda

Nvidia