63

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?

Tim
  • 32,861
  • 27
  • 118
  • 178
Terrance
  • 41,612
  • 7
  • 124
  • 183

5 Answers5

95

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.

Terrance
  • 41,612
  • 7
  • 124
  • 183
  • 1
    Note that as of 2018-11 you need cuda 0.9 for tensorflow. – mathtick Nov 24 '18 at 12:10
  • 1
    installation of nvidia-driver-410 fails because it depends on xserver-xorg-video-nvidia-410 and it can't be installed because it depnds on a bunch of lbnvidia which are not available – fccoelho Jan 24 '19 at 15:50
  • 1
    @fccoelho I recently discovered that the installation actually works best with the graphics-drivers ppa, so I have updated my answer here to include that PPA, then it should find and install all the depends. – Terrance Mar 26 '19 at 22:22
  • 1
    You shoud do a "sudo apt autoremove" step after removing the previous toolkit & drivers. Otherwise the install fails due to dependencies that are not automatically updated. – Pieter-Jan Busschaert Jun 08 '19 at 07:03
  • @Pieter-JanBusschaert Added to the answer. – Terrance Jun 08 '19 at 12:48
  • 1
    Additional comment: the NVidia driver included in the 10.1 CUDA .deb package from NVidia does not include 32-bit libraries. Steam (and most games) will not work anymore after installing this way. See also: https://github.com/ValveSoftware/steam-for-linux/issues/5778 – Pieter-Jan Busschaert Jun 10 '19 at 08:53
  • @Pieter-JanBusschaert You are correct. I am going to add that as a note. It is the 418.67 driver where they did not include the 32bit libraries with either CUDA 10.0 or 10.1. Kind of frustrating but thank you for the heads up. – Terrance Jun 10 '19 at 13:40
  • @Terrance can you please check out this question: https://stackoverflow.com/questions/56720530/make-error-while-building-hello-zed-example – csg Jun 23 '19 at 14:56
  • @csg I left you a comment there on your question, but if you like you can try the run file installation I just added to the bottom of this answer. – Terrance Jun 23 '19 at 17:13
  • "Hopefully NVIDIA will release an update for that soon."

    Narrator: "They didn't." (Note that for CUDA 10.2 you need to use Nvidia driver version 440+.)

    – Joschua Jan 01 '20 at 11:19
  • 1
    nvidia-driver-440. Could not install cuda 10-0/10-1/10-2 and got this errors "Depends: cuda-demo-suite-10-2 (>= 10.2.89) but it is not going to be installed". To solve it just installed cuda via cuda $ sudo apt-get install cuda-toolkit-10-2. cuda-toolkit-10-1 didn't set well either, maybe someone helps – Ivan Shelonik Mar 16 '20 at 20:11
  • @IvanShelonik Had the same problem – justanewb Sep 21 '21 at 02:28
  • Where it says the following, did you want a newline? Check libcudnn version /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn: – EngrStudent Oct 29 '21 at 11:58
  • E: The repository 'http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2204/x86_64 Release' does not have a Release file. N: Updating from such a repository can't be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. – mLstudent33 Dec 01 '22 at 10:44
  • @mLstudent33 That repo you just tried to add doesn't exist. The highest right now it goes is 20.04, but even that repo doesn't have all the packages and is very incomplete. The highest one that is complete is 18.04, but the packages might be too outdated. See the next answer below. – Terrance Dec 01 '22 at 14:26
  • @Terrance, installing on WSL2 was so much easier and the Ubuntu version is 22.04 so I am going to use that. I may reinstall Ubuntu 20.04 on my dual boot but WSL2 seems pretty nice at the moment. – mLstudent33 Dec 02 '22 at 06:49
  • @Terrance I just saw that for WSL2 there is no unified memory, ie. cudaMallocManaged() so I'll try below. Also are you recommending that I dual boot 18.04 if I want to do parallel programming with Cuda? I also want to train some neural networks. – mLstudent33 Dec 02 '22 at 08:50
  • just a warning to anyone about to install nvcc - the installation will take up many many gigabytes... – Lost Crotchet Nov 15 '23 at 15:53
20

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
Terrance
  • 41,612
  • 7
  • 124
  • 183
  • 1
    11.3 now available: wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run – Brian D May 17 '21 at 18:02
  • 1
    Ran into an 'unmet dependencies' error when running sudo apt install like this: https://forums.developer.nvidia.com/t/nvidia-driver-installation-unmet-dependencies/169501 solution was to open "Software&Updates", "Other Software", remove/disable links to prior driver version installs. e.g., file://var/cuda-repo-10-2-local.../ and then the install works. – Brian D May 17 '21 at 18:42
  • 2
    This also works for Ubuntu 22.04, CUDA 11.8, and NVIDIA 515 drivers. – Cecilia Oct 24 '22 at 17:57
  • I have an option right after accepting Eula to put an X for Kernel Objects then indented one more level under that is nvidia-fs. This is for Ubuntu 22.04, Cuda 11.8, Nvidia 515 drivers per @Cecilia – mLstudent33 Dec 02 '22 at 09:23
  • Anyone else reading this @Cecilia is correct. Also leave the box for Kernel Objects without the X. It seemed to have worked for me too. – mLstudent33 Dec 02 '22 at 09:31
  • actually @Cecilia's answer needs adjustment, make sure to install Cuda 11.7 with that driver 515 not 11.8 or you get mismatch of driver and toolchain. – mLstudent33 Dec 04 '22 at 08:19
  • For the Ubuntu 22.04 and cuda-12-3 with sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub sudo apt update ``` sudo apt install cuda-12-3
    
    
    – Zheng Zhedong Jan 22 '24 at 05:14
3

22.04 LTS instructions that worked for me:

Install nvidia driver:

sudo apt install nvidia-utils-525  # change version number to the new one...
sudo apt install nvidia-driver-525
sudo shutdown -r now # restart 
sudo apt autoremove # just for good measure, clean up
nvidia-smi # check that the system can find the driver and list the gpus
nvidia-settings  # to check current usage, etc.

Install Cuda without uninstalling the nvidia driver you just chose:

Find the cuda version you need: developer.nvidia.com/cuda-downloads

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda

For me, this was the only way to get both the nvidia driver I wanted, and the version of cuda that i wanted while still being able to use nvidia-smi and nvidia-settings. If I installed nvidia-cuda-toolkit instead, it would uninstall the utils and driver that I selected and disable nvidia-smi.

Brian D
  • 358
  • 3
  • 7
1

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.
0

For anyone working with AWS deep learning base AMI:
The image comes with several NVIDIA CUDA environments built-in and by default works with one of them (in my case 10), but you can switch to any other, simply by changing the symlink:

$ sudo rm /usr/local/cuda
$ sudo ln -s /usr/local/cuda-10.2 /usr/local/cuda

More info:
https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-base.html

MikeL
  • 181