Ubuntu – How to install NVIDIA and CUDA drivers into Ubuntu

cudanvidia

EDIT: I have added a new answer below for Ubuntu 20.04 LTS and CUDA 11.1.1. See below.


I have a system with a NVIDIA card that has a compute support of 3.5+ compared on https://developer.nvidia.com/cuda-gpus. How do I install CUDA and the NVIDIA drivers in Ubuntu without downloading the .deb files from NVIDIA?

Best Answer

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.

Related Question