Install CUDA for Ubuntu in 5 mins

Andrew Zhu (Shudong Zhu)
2 min readMay 14, 2023

One tweet from https://twitter.com/finbarrtimbers says:

Source: https://twitter.com/finbarrtimbers/status/1657154970213572608

I enjoyed the CUDA driver installation for 2 days plus another day to reinstall the whole system around 2 years ago.

Now in 2023, with the correct steps, you can only enjoy CUDA drivers installation for just 5 mins in Ubuntu, here are the steps:

Step 1. If you have Nvidia’s driver already installed, you’d better uninstall all Nvidia drivers first to ensure minimum errors.

sudo apt-get purge 'nvidia*'
sudo apt-get autoremove

Then reboot your system:

sudo reboot

You may want to disable the UEFI secure boot from the BIOS, if not doing it, you will be asked to input a password and then enroll the key, it will work, but require additional steps and may lead to driver installation failure.

Step 2. Install GCC if you don’t have one

sudo apt install gcc

Step 3. Install NVIDIA GPU driver

sudo apt update
sudo apt upgrade

sudo apt install build-essential dkms

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

# list all avaliable drivers
sudo ubuntu-drivers devices

# auto install
sudo ubuntu-drivers autoinstall
# or with a version
# sudo apt install nvidia-driver-525

Note that driver above 535 could lead to fail suspend, in other words, can’t go sleep, if you are using Ubuntu as your work desktop.

Then reboot

sudo reboot

Step 4. Select the right CUDA version for your system on this CUDA download page:https://developer.nvidia.com/cuda-downloads

and you will get the bash commands that do everything of installation:

Here is a version of my choice:

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
wget https://developer.download.nvidia.com/compute/cuda/12.1.1/local_installers/cuda-repo-ubuntu2204-12-1-local_12.1.1-530.30.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-12-1-local_12.1.1-530.30.02-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

After this, you should be able to see the CUDA version by

nvidia-smi

or

nvcc --version

If the nvcc is not found, just add this into your terminal rc file (~/.bashrc or ~/.zshrc)

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Step 5. Reboot and stop enjoying CUDA Driver installation.

Sorry~

--

--