This tutorial guides you through detailed steps to install CUDA on Ubuntu, covering driver installation, toolkit setup, verifying installation, compiling a sample program, system compatibility checks, and troubleshooting common issues.
CUDA, which stands for Compute Unified Device Architecture, is a parallel computing platform and application programming interface (API) developed by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU). Originally designed for rendering graphics, GPUs have evolved into highly efficient computing devices with a massive number of small, efficient cores. CUDA leverages this architecture for general-purpose computing. It allows developers to use ‘C’, ‘C++’, and other supported languages to write programs that can execute on the GPU.
System Compatibility Checks for CUDA Toolkit Versions
When checking system compatibility for different CUDA Toolkit versions, it’s essential to consider the compatibility between the CUDA version, the GPU model, and the NVIDIA driver version. Each CUDA version supports specific driver versions and is designed to work with certain GPU architectures. Using incompatible versions can lead to performance issues or failure to utilize the GPU effectively.
To check compatibility:
- CUDA Toolkit and Driver Version: Refer to the NVIDIA CUDA Toolkit Release Notes, which provide details on the supported driver versions for each CUDA release. For instance, CUDA Toolkit 11.0 might be compatible with NVIDIA driver version 450.xx or later and may support GPUs with the Turing architecture or newer.
- GPU Architecture: Ensure that your GPU architecture is supported by the CUDA Toolkit version you plan to use. NVIDIA documentation lists supported GPUs for each CUDA version. CUDA Toolkit versions are designed for specific GPU architectures. For example, if you have a Tesla V100 GPU (Volta architecture), you would need a CUDA version that supports Volta, like CUDA 9.0 or newer.
- Operating System and Compiler Compatibility: CUDA Toolkits are also specific to operating systems and compilers. Make sure your system’s OS and compiler versions are compatible with the CUDA Toolkit version. Each CUDA version supports specific OS and compiler versions. For instance, CUDA Toolkit 10.1 might be compatible with Ubuntu 18.04 and GCC 7.3.
Steps to install CUDA on Ubuntu
1. Update and Upgrade Ubuntu
Before starting, it’s essential to update your Ubuntu system. You must execute this command either as root user or using a normal user with sudo privilege. Open a terminal and execute:
sudo apt update
2. Install NVIDIA Driver
To install the recommended NVIDIA driver, first identify the best driver for your system:
ubuntu-drivers devices
The output of the ubuntu-drivers devices command can vary depending on
the specific hardware and software configuration of your Ubuntu system.
For a system with a GeForce GTX 1650 Ti Mobile GPU, the output might
look something like this:
- Identification of the GPU model (e.g., NVIDIA Corporation TU117M [GeForce GTX 1650 Ti Mobile]).
- A list of compatible drivers, with one marked as the recommended
driver (e.g.,
nvidia-driver-440 - distro non-free recommended). - Alternative drivers might also be listed, including open-source
options (e.g.,
xserver-xorg-video-nouveau - distro free builtin).
Here is a sample output:
== /sys/devices/pci0000:00/0000:00:02.0 ==
modalias : pci:v000010DEd00001D10sv00001028sd000007D1bc03sc02i00
vendor : NVIDIA Corporation
model : GP108M [GeForce MX150]
driver : nvidia-driver-525 - third-party free recommended
driver : nvidia-driver-515-server - distro non-free
driver : nvidia-driver-470 - distro non-free
driver : nvidia-driver-470-server - distro non-free
driver : nvidia-driver-515 - distro non-free
driver : nvidia-driver-450-server - distro non-free
Install the driver using apt package manager:
sudo apt install nvidia-driver-525
After installation, reboot your system:
sudo reboot
3. Verify NVIDIA Driver Installation
After rebooting, check if the NVIDIA driver is installed correctly with:
nvidia-smi
Sample Output:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.37.12 Driver Version: 525.37.12 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 GeForce RTX 3080 Off | 00000000:01:00.0 Off | N/A |
| 30% 55C P2 70W / 320W | 5000MiB / 10000MiB | 60% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1234 G /usr/lib/xorg/Xorg 183MiB |
| 0 N/A N/A 2345 C ./your_cuda_app 4817MiB |
+-----------------------------------------------------------------------------+
4. Install CUDA Toolkit
Add the CUDA repository and GPG key:
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
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
sudo apt-get update
Now, install CUDA:
sudo apt install cuda
Next reboot the system
sudo reboot
5. Set Environment Variables
After installation, add CUDA to your PATH. In your .bashrc file, add:
echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
source ~/.bashrc
You can also use .bash_profile based on your requirement, to
understand the difference you can also read
.bashrc vs .bash_profile [Which one to
use?]
6. Verify CUDA Installation
Check the CUDA version to confirm the installation:
nvcc --version
The output will display the CUDA version, similar to:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jul_11_02:20:44_PDT_2023
Cuda compilation tools, release 12.0, V12.0.140
Compiling and Running a Sample CUDA Program
To compile and run a basic CUDA program, such as vector addition, follow these steps:
- Write the Program: Create a CUDA file, e.g.,
vector_add.cu, and write your CUDA code for vector addition in it. The program typically includes kernel definition for the operation and host code to allocate memory, transfer data between the host and device, and launch the kernel. - Compile the Program: Use the
nvcccompiler to compile your CUDA code. The command will look like this:nvcc -o vector_add vector_add.cu. This compilesvector_add.cuand generates an executable namedvector_add. - Run the Compiled Program: After successful compilation, run the
program by typing:
./vector_add
This process will execute your CUDA program and perform vector addition on the GPU. Remember to handle memory allocation and deallocation, both on the host and device, and to check for errors in kernel execution.
Summary
Installing the NVIDIA CUDA Toolkit on Ubuntu involves a series of steps
to ensure proper setup and compatibility. First, update and upgrade your
Ubuntu system. Next, identify and install the appropriate NVIDIA driver,
typically using the ubuntu-drivers devices command. Once the driver is
installed, proceed to install the CUDA Toolkit by adding the CUDA
repository and using the package manager. After installation, update
your system’s PATH to include the CUDA binaries. Finally, verify the
installation by checking the CUDA version. Throughout this process,
compatibility between the CUDA version, the GPU, and the driver version
is crucial for optimal performance.

![How to Install CUDA on Ubuntu [Step-by-Step]](/install-cuda-on-ubuntu/install-cuda-ubuntu.jpg)
