Setting up CUDA & cuDNN for Machine Learning can be overwhelming. In this guide, I will walk you through the steps to install CUDA and cuDNN on your system, ensuring your machine is correctly set up for deep learning tasks.
System Configuration:
- Operating System: Ubuntu 22.04
- GPU: GeForce RTX 3090
- ML Framework: PyTorch
sudo apt update && sudo apt upgradeUninstall the previous NVIDIA and CUDA installation to avoid messing up the new installation.
sudo apt-get remove --purge -y '*nvidia*' '*cuda*' 'libcudnn*' 'libnccl*' '*cudnn*' '*nccl*'
sudo apt-get autoremove --purge -y
sudo apt-get clean
Check if there are any remaining packages or files.
dpkg -l | grep -E 'nvidia|cuda|cudnn|nccl'
ubuntu-drivers devices
We will install the NVIDIA driver tagged recommended, which indicates which drivers are recommended for each piece of hardware based on compatibility and performance.
sudo ubuntu-drivers autoinstall
My recommended version is nvidia-driver-595-open. If ubuntu-drivers devices recommends a different driver on your machine, replace nvidia-driver-595-open with that package name.
sudo apt install nvidia-driver-595-open
Reboot the system for these changes to take effect.
reboot
After reboot, verify that the following command works:
nvidia-smi
Again, check for updates on your OS.
sudo apt update && sudo apt upgradeAt the time of writing, the latest CUDA version supported by PyTorch is 13.2.
You can find older versions in the CUDA Toolkit Archive. In my case, I will be continuing with CUDA Toolkit 13.2.0 (March 2026).
You will need to select your operating system (Linux in my case). Afterwards, you will be prompted to select the Architecture. If you are not sure what the Architecture of your PC you can use the command below (in my case, the Architecture is x86_64).
uname -m
Next, we need to select the distribution and version of our operating system, in my case, Ubuntu 22.04. Lastly, I am using the deb (local) installer type.
These are the commands for installing CUDA Toolkit 13.2:
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/13.2.0/local_installers/cuda-repo-ubuntu2204-13-2-local_13.2.0-595.45.04-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-13-2-local_13.2.0-595.45.04-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-13-2-local/cuda-*-keyring.gpg /usr/share/keyrings/sudo apt-get update
sudo apt-get -y install cuda-toolkit-13-2
nvcc --version
If you are not getting the CUDA version as output, do the following:
- Ensure that CUDA 13.2 is installed in the correct directory, typically
/usr/local/cuda-13.2. - Create a symlink to the CUDA directory to make it easier to reference.
sudo ln -s /usr/local/cuda-13.2 /usr/local/cuda
- Add the CUDA paths to your
.bashrcfile to ensure they are set up every time you open a terminal.echo 'export PATH=/usr/local/cuda-13.2/bin:$PATH' >> ~/.bashrc
- Apply the changes made to the
.bashrcsource ~/.bashrc
Firstly, to match appropriate cuDNN with our CUDA and Driver versions, we can use the table provided by NVIDIA. In my case, the newest 9.22.0 cuDNN version is appropriate.
You can download the cuDNN file here, for which you will need an Nvidia account. Same as for CUDA, you will have to select OS, architecture, distribution, version, and installer type.
In my case, to download the installer for Linux Ubuntu 22.04 x86_64, the instructions are as follows:
wget https://developer.download.nvidia.com/compute/cudnn/9.22.0/local_installers/cudnn-local-repo-ubuntu2204-9.22.0_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2204-9.22.0_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2204-9.22.0/cudnn-*-keyring.gpg /usr/share/keyrings/sudo apt-get update
sudo apt-get -y install cudnn9-cuda-13
I assume that you already have Python installed on your machine, so this tutorial will not cover that part.
mkdir test_cuda
cd test_cudapython -m venv .venv
source .venv/bin/activatepip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu132
import torch # Check PyTorch version print(f"PyTorch version: {torch.__version__}") # Check if CUDA device is recognised print(f"CUDA device is available: {torch.cuda.is_available()}") assert torch.cuda.is_available(), ValueError("CUDA device is not recognised.") device = "cuda" a = torch.rand(3, 1).to(device) b = torch.rand(1, 3).to(device) c = a @ b print(f"Variable shape: {c.shape}") print(f"Variable device: {c.device}")
If you carefully followed these instructions, you have successfully installed CUDA and cuDNN on your Ubuntu 22.04 system. Your NVIDIA GPU is now ready for deep learning tasks with PyTorch.
You can also view this guide on Medium. If this guide helped you, give it a 👏, share it, and give it a ⭐️ on GitHub.