Skip to content

Instantly share code, notes, and snippets.

@ksopyla
Last active July 14, 2024 16:06
Show Gist options
  • Save ksopyla/ee744bf013c83e4aa3fc525634d893c9 to your computer and use it in GitHub Desktop.
Save ksopyla/ee744bf013c83e4aa3fc525634d893c9 to your computer and use it in GitHub Desktop.
How to install CUDA toolkit 12 on ubuntu 22.04, step by step guide

Step by step instruction how to install CUDA 12 Ubuntu 22.04

Clean all previous nvidia packages [optional step]

sudo apt purge nvidia*
sudo apt remove nvidia-*
sudo rm /etc/apt/sources.list.d/cuda*
sudo apt autoremove && sudo apt autoclean
sudo rm -rf /usr/local/cuda*

Restart the machine

sudo shutdonw -r now

Tools, libraries and device checks

# to verify your gpu is cuda enable check
lspci | grep -i nvidia
# gcc compiler is required for development using the cuda toolkit. to verify the version of gcc install enter
gcc --version

Install the NVidia drivers [optional]

Add the ppa:graphics-drivers/ppa repository into your system

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

Install drivers (tip: type sudo apt install nvidia-driver- and press tab to see all posiible driver versions)

sudo apt install nvidia-driver-550
sudo shutdown -r now

After the reboot check driver version

nvidia-smi

You should see something like this

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.100                Driver Version: 550.100        CUDA Version: 12.4     |
|-----------------------------------------+------------------------+----------------------+
| 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 RTX 3090        Off |   00000000:03:00.0 Off |                  N/A |
|  0%   24C    P8              6W /  370W |       2MiB /  24576MiB |      0%      Default |
|                                         |                        |                  N/A |

NVidia Ubuntu 22.04 repository for CUDA 12

If you need CUDA Tolkit 12 with nvcc, other tools and libraries you can install it from NVIDIA Ubunutu 22.04 repository.

Add Ubuntu 22.04 repository

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600

Add public keys

sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub

sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"

Install cuda toolkit

Update and install cuda toolkit, you will gain access to many version of cuda and cuda toolkit.

Write apt install cuda-toolkit and press tab it will show list of all available versions

sudo apt update
sudo apt install cuda-toolkit-12-1

Install cudnn libraries

having a NVidia repositories added you can type

sudo apt install cudnn9-cuda-12

Add CUDA_HOME to PATH environmet

Edit /home/$USER/.bashrc file

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
export PATH="/usr/local/cuda/bin:$PATH"

Disable Sleep on Ubuntu Server

New drivers automatically install some xorg server packages and it activates power management options. Probably on your server you want to disable power management (sleep, hibernate).

sudo systemctl mask sleep.target suspend.target hibernate.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment