Installing CUDA on Ubuntu 22.04 (RXT4080-laptop)

I tried to install nvcc on my Ubuntu 22.04 machine following the NVIDIA offical guides and broke my driver 3 times, with a hell lot of headache, and eventually found that this version works.

Installing CUDA Toolkit 12.2 on Ubuntu 22.04

This guide will walk you through the process of installing the CUDA Toolkit 12.2 on Ubuntu 22.04, including setting up the environment variables and verifying the installation.


1. Remove Previous CUDA Installations and Install the NVIDIA Driver

Ensure that there are no conflicting versions of CUDA or NVIDIA drivers installed, then install the NVIDIA driver version 535.

sudo apt-get --purge remove '*cublas*' 'cuda*' 'nsight*' 'nvidia*'
sudo apt-get autoremove
sudo apt-get install nvidia-driver-535
sudo reboot

2. Download and Install the CUDA Repository Pin

This step ensures that the CUDA repository takes precedence over other repositories.

sudo mv /etc/apt/preferences.d/cuda-repository-pin-600

3. Download the CUDA Repository Package

Download the CUDA 12.2 repository package for Ubuntu 22.04.


4. Install the CUDA Repository Package

Use dpkg to install the downloaded package.

sudo dpkg -i cuda-repo-ubuntu2204-12-2-local_12.2.0-535.54.03-1_amd64.deb

5. Install the GPG Key

Install the GPG key to complete the setup.

sudo cp /var/cuda-repo-ubuntu2204-12-2-local/cuda-216F19BD-keyring.gpg /usr/share/keyrings/

6. Update the Package Lists

Update the package lists to include the CUDA repository.

sudo apt-get update

7. Install CUDA Toolkit

Install the CUDA toolkit using apt-get.

sudo apt-get -y install cuda

8. Set Environment Variables

Ensure your environment variables are correctly set by adding the following lines to your .bashrc or .zshrc:

echo 'export PATH=/usr/local/cuda-12.2/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

9. Verify the Installation

Run the following commands to verify that nvidia-smi and nvcc work correctly.

nvcc --version


(us) rong@ubuntu:~$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jun_13_19:16:58_PDT_2023
Cuda compilation tools, release 12.2, V12.2.91
Build cuda_12.2.r12.2/compiler.32965470_0
(us) rong@ubuntu:~$ nvidia-smi
Wed May 15 09:20:33 2024       
| NVIDIA-SMI 535.171.04             Driver Version: 535.171.04   CUDA Version: 12.2     |
| 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 4080 ...    Off | 00000000:01:00.0  On |                  N/A |
| N/A   47C    P8               4W / 150W |     73MiB / 12282MiB |      0%      Default |
|                                         |                      |                  N/A |
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|    0   N/A  N/A      1332      G   /usr/lib/xorg/Xorg                           69MiB |

1 Like

Hi @trsjtu17, thank you for this write-up!

It might be better suited for the CUDA installation category, but we can leave it here.

CUDA installation can be a bit confusing. But in general, if you start with a clean system without any NVIDIA or CUDA installed, following the instructions in the CUDA documentation works nearly every time. The doc also includes how a fitting driver can be installed as part of the CUDA toolkit installation. It might not be the newest, but it will be a known working driver and fit right into the rest of the installation procedure.

In any case, great that you got through this ad have a running CUDA installation!