Skip to content

Instantly share code, notes, and snippets.

@denguir
Last active May 20, 2024 11:18
Show Gist options
  • Save denguir/b21aa66ae7fb1089655dd9de8351a202 to your computer and use it in GitHub Desktop.
Save denguir/b21aa66ae7fb1089655dd9de8351a202 to your computer and use it in GitHub Desktop.
Installation procedure for CUDA & cuDNN

How to install CUDA & cuDNN on Ubuntu 22.04

Install NVIDIA drivers

Update & upgrade

sudo apt update && sudo apt upgrade

Remove previous NVIDIA installation

sudo apt autoremove nvidia* --purge

Check Ubuntu devices

ubuntu-drivers devices

You will install the NVIDIA driver whose version is tagged with recommended

Install Ubuntu drivers

sudo ubuntu-drivers autoinstall

Install NVIDIA drivers

My recommended version is 525, adapt to yours

sudo apt install nvidia-driver-525

Reboot & Check

reboot

after restart verify that the following command works

nvidia-smi

Install CUDA drivers

Update & upgrade

sudo apt update && sudo apt upgrade

Install CUDA toolkit

sudo apt install nvidia-cuda-toolkit

Check CUDA install

nvcc --version

Install cuDNN

Download cuDNN .deb file

You can download cuDNN file here. You will need an Nvidia account. Select the cuDNN version for the appropriate CUDA version, which is the version that appears when you run:

nvcc --version

Install cuDNN

sudo apt install ./<filename.deb>
sudo cp /var/cudnn-<something>.gpg /usr/share/keyrings/

My cuDNN version is 8, adapt the following to your version:

sudo apt update
sudo apt install libcudnn8
sudo apt install libcudnn8-dev
sudo apt install libcudnn8-samples

Test CUDA on Pytorch

Create a virtualenv and activate it

sudo apt-get install python3-pip
sudo pip3 install virtualenv 
virtualenv -p py3.10 venv
source venv/bin/activate

Install pytorch

pip3 install torch torchvision torchaudio

Open Python and execute a test

import torch
print(torch.cuda.is_available()) # should be True

t = torch.rand(10, 10).cuda()
print(t.device) # should be CUDA
@LouisJULIEN
Copy link

Worked like a charm, thanks!

The only thing I did a bit differently was the gpg key copy. It's now is a subdirectory. Here the command I used:

sudo cp /var/cudnn-local-repo-ubuntu2204-8.9.6.50/cudnn-local-692B6C75-keyring.gpg  /usr/share/keyrings/

@LouisJULIEN
Copy link

Some tweaks are required if you're willing to use tensorflow with CUDA:

  • As of November 2023, you need cudNN v8.6 as stated by the (english) doc. Beware, the latest cudNN version is 8.9. You need to uninstall your previous cudNN to install the right version. Don't forget to uninstall then reinstall libcudnn8 libcudnn8-dev libcudnn8-samplesas well.
  • To install tensorflow with cuda, use pip install tensorflow[and-cuda]
  • Check the installation: python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))" . You should see 1 or higher if your setup is right.

@ilo-bst
Copy link

ilo-bst commented Dec 22, 2023

Hi, what does <something> stand for in sudo cp /var/cudnn-<something>.gpg /usr/share/keyrings/? My cuDNN file is cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb.

@ilo-bst
Copy link

ilo-bst commented Dec 22, 2023

Hi, what does <something> stand for in sudo cp /var/cudnn-<something>.gpg /usr/share/keyrings/? My cuDNN file is cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb.

I just went to the location /var/cudnn... in my file system and found the appropriate gpg file.

@misaelpedro
Copy link

When I was installing cuDNN and ran:

$ sudo apt install ./cudnn-local-repo-ubuntu2204-8.6.0.163_1.0-1_amd64.deb

It returned this at end of output:

The public cudnn-local-repo-ubuntu2204-8.6.0.163 GPG key does not appear to be installed. To install the key, run this command: sudo cp /var/cudnn-local-repo-ubuntu2204-8.6.0.163/<YOURS_KEY>-keyring.gpg /usr/share/keyrings/

Then I ran that command in the end of message and the others ones as it is listed above (libcudnn8, libcudnn8-dev, libcudnn8-samples)

A final tip to check if cuDNN is installed is run :
whereis cudnn.h
It should return : cudnn.h: /usr/include/cudnn.h

@aquinovo
Copy link

Thank you. This is very helpful

@MaisJamal
Copy link

Thanks, it's very helpful, worked perfectly.

@corticalstack
Copy link

@Atlas3DSS Having same issue as you reported. I've purged all nvidia (as I'm aware) and reinstalled nvidia driver, followed by cuda kit, but all I can seem to install is the 11.5 cuda kit version (on Ubuntu 22.04). Any tips to resolve you can share?

@tarreislam
Copy link

For anyone having issues on ubuntu 22.04: check this out

NVIDIA/nvidia-docker#1243 (comment)

I switched to bare metal because I thought Esxi was the issue, but after doing this, it worked

@flavves
Copy link

flavves commented Apr 8, 2024

Thanks!

@Adesojialu
Copy link

ubuntu-drivers devices
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.

@chococandy63
Copy link

chococandy63 commented May 14, 2024

@denguir

trinity@ukiyo:~$ ubuntu-drivers devices
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
== /sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0 ==
modalias : pci:v000010DEd00001F99sv00001025sd00001447bc03sc00i00
vendor   : NVIDIA Corporation
model    : TU117M [GeForce GTX 1650 Mobile / Max-Q]
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-525 - third-party non-free
driver   : nvidia-driver-550-open - third-party non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-515 - third-party non-free
driver   : nvidia-driver-470 - distro non-free
driver   : nvidia-driver-535-open - distro non-free
driver   : nvidia-driver-550 - third-party non-free recommended
driver   : nvidia-driver-535 - third-party non-free
driver   : nvidia-driver-520 - third-party non-free
driver   : nvidia-driver-535-server-open - distro non-free
driver   : nvidia-driver-545 - third-party non-free
driver   : xserver-xorg-video-nouveau - distro free builtin
trinity@ukiyo:~$ sudo ubuntu-drivers autoinstall
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 linux-modules-nvidia-550-generic-hwe-24.04 : Depends: linux-modules-nvidia-550-6.8.0-31-generic (= 6.8.0-31.31) but it is not going to be installed
                                              Depends: nvidia-kernel-common-550 (>= 550.67) but 550.54.15-0ubuntu1 is to be installed
E: Unable to correct problems, you have held broken packages.

after running sudo apt install nvidia-driver-550, the command ubuntu-drivers devices shows:

trinity@ukiyo:~$ ubuntu-drivers devices
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
udevadm hwdb is deprecated. Use systemd-hwdb instead.
== /sys/devices/pci0000:00/0000:00:01.1/0000:01:00.0 ==
modalias : pci:v000010DEd00001F99sv00001025sd00001447bc03sc00i00
vendor   : NVIDIA Corporation
model    : TU117M [GeForce GTX 1650 Mobile / Max-Q]
driver   : nvidia-driver-515 - third-party non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-535-open - distro non-free
driver   : nvidia-driver-550 - third-party non-free recommended
driver   : nvidia-driver-520 - third-party non-free
driver   : nvidia-driver-535-server-open - distro non-free
driver   : nvidia-driver-525 - third-party non-free
driver   : nvidia-driver-535 - third-party non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-550-open - third-party non-free
driver   : nvidia-driver-545 - third-party non-free
driver   : nvidia-driver-470 - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin

@shravanvallaban
Copy link

@chococandy63 I have the same issue, I recently install Ubuntu 24.04 and when I tried install cuda drivers I get the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment