1. Introduction
CUDA (Compute Unified Device Architecture) is a parallel computing platform and API provided by NVIDIA, enabling high-speed computation using GPUs.
It is widely used in various fields, including machine learning, deep learning, and scientific computing.
This guide provides a detailed explanation of how to install CUDA on an Ubuntu system.
2. Prerequisites
2.1 Checking GPU Compatibility
First, check whether your NVIDIA GPU supports CUDA.
Run the following command in the terminal:
lspci | grep -i nvidia
If your system recognizes an NVIDIA device in the output, your GPU is detected.
For a complete list of supported GPUs, refer to NVIDIA’s official website.
2.2 Checking Your Ubuntu Version
CUDA is supported on specific Ubuntu versions.
Check your current Ubuntu version by running the following command:
lsb_release -a
In general, LTS (Long Term Support) versions of Ubuntu are recommended.
For the latest compatibility details, refer to NVIDIA’s official documentation.
2.3 Checking GCC Installation
The GCC compiler is required to install CUDA.
Verify whether it is installed using the following command:
gcc --version
If GCC is not installed, install it by running:
sudo apt install build-essential

3. Installing NVIDIA Drivers
3.1 Removing Existing Drivers
If old NVIDIA drivers are installed, remove them to avoid conflicts.
Run the following commands:
sudo apt-get --purge remove '*nvidia*'
sudo apt-get autoremove
3.2 Selecting and Installing the Appropriate Driver
Check the NVIDIA official website to find the correct driver for your GPU, then follow these steps for installation:
- Add the Repository: Run the following commands in the terminal to add the NVIDIA driver repository.
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
- Check Recommended Drivers: Use this command to see the recommended driver.
ubuntu-drivers devices
Look for the driver marked as “recommended” in the output.
- Install the Driver: Install the recommended driver by specifying its version.
sudo apt install nvidia-driver-<recommended version>
- Restart the System: After installation, restart your system.
sudo reboot
4. Installing CUDA Toolkit
4.1 Choosing the CUDA Version
Visit the official CUDA download page to find the compatible CUDA version for your GPU and Ubuntu version.
If using the latest version, ensure compatibility with your software and libraries.
4.2 Adding the Repository and Installing CUDA
Follow these steps to install the CUDA Toolkit.
- Add the Repository: Add the NVIDIA repository (example for Ubuntu 20.04).
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
- Add the Repository Key: Retrieve and install the repository key.
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
- Install CUDA Packages: Install the CUDA Toolkit.
sudo apt update
sudo apt install cuda
- Verify Installation: Check if CUDA is installed correctly.
nvcc --version

5. Installing cuDNN
5.1 Downloading cuDNN
cuDNN (CUDA Deep Neural Network library) is an NVIDIA GPU-accelerated library for deep learning.
To install cuDNN, follow these steps:
- Go to the NVIDIA cuDNN download page and log in.
- Select the appropriate cuDNN version compatible with your CUDA version.
- Download the cuDNN package for Ubuntu.
5.2 Installing cuDNN
After downloading, install cuDNN using the following commands:
tar -xvf cudnn-*.tar.xz
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
After installation, verify that cuDNN is correctly installed by checking its version:
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
6. Setting Up Environment Variables
After installing CUDA and cuDNN, update the environment variables to ensure they are correctly recognized by the system.
6.1 Updating .bashrc
Edit the .bashrc
file to add the CUDA paths:
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
6.2 Verifying the CUDA Installation
To confirm that CUDA is installed and configured correctly, run the following command:
nvcc --version
This should output the CUDA version installed on your system.
7. Running a Test Program
To check if CUDA is working properly, compile and run a simple test program.
#include <stdio.h>
int main() {
printf("CUDA setup is complete!\n");
return 0;
}
Compile and execute it using:
gcc test.c -o test
./test
8. Troubleshooting
8.1 Common Issues and Fixes
- CUDA not recognized: Ensure the environment variables are correctly set by running
echo $PATH
andecho $LD_LIBRARY_PATH
. - Driver issues: If NVIDIA drivers are not working, try reinstalling them using the steps in section 3.
- cuDNN version mismatch: Check that your cuDNN version is compatible with your installed CUDA version.
9. Conclusion
By following this guide, you should now have a fully functional CUDA environment set up on Ubuntu.
With CUDA and cuDNN installed, you can start utilizing GPU acceleration for deep learning, scientific computing, and other high-performance applications.
If you encounter any issues, refer to NVIDIA’s official documentation or community forums for additional support.
