How to Install CUDA on Ubuntu [Step-by-Step]


Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

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:

  1. Identification of the GPU model (e.g., NVIDIA Corporation TU117M [GeForce GTX 1650 Ti Mobile]).
  2. A list of compatible drivers, with one marked as the recommended driver (e.g., nvidia-driver-440 - distro non-free recommended).
  3. 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:

  1. 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.
  2. Compile the Program: Use the nvcc compiler to compile your CUDA code. The command will look like this: nvcc -o vector_add vector_add.cu. This compiles vector_add.cu and generates an executable named vector_add.
  3. 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.

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment