Ubuntu

How to Install the Latest Version of NVIDIA CUDA on Ubuntu 22.04 LTS

The full form of CUDA is Compute Unified Device Architecture. CUDA is a parallel computing platform and programming model that is developed by NVIDIA. It is used to run the programs on NVIDIA Graphics Processing Units (GPUs) to speed up the computing applications dramatically.

In this article, we will show you how to install the latest version of CUDA on Ubuntu 22.04 LTS. We will also show you how to write, compile, and run your very first CUDA program on Ubuntu 22.04 LTS.

Topic of Contents:

  1. Prerequisites
  2. Installing the Latest NVIDIA Drivers on Ubuntu
  3. Updating the APT Package Repository Cache
  4. Installing GCC and Other Build Tools
  5. Checking If the Installed NVIDIA Drivers Support the Latest Version of CUDA
  6. Adding the Official NVIDIA CUDA Repository on Ubuntu
  7. Installing the Latest Version of CUDA on Ubuntu
  8. Adding CUDA and CUDA Libraries to the Path.
  9. Allowing the CUDA Binaries to Run with Superuser Privileges
  10. Testing If the Latest Version of CUDA Is Installed on Ubuntu
  11. Writing, Compiling, and Running a Simple CUDA Program
  12. Conclusion
  13. References

Prerequisites:

For you to install the latest version of CUDA, compile the CUDA programs, and run the CUDA programs on Ubuntu 22.04 LTS operating system, you need the following:

i) Installed NVIDIA GPU on your computer.

ii) The latest version of NVIDIA GPU drivers installed on your Ubuntu operating system.

Installing the Latest NVIDIA Drivers on Ubuntu

You must have the latest version of NVIDIA GPU drivers installed on your Ubuntu operating system for the latest version of CUDA to work. If you haven’t yet installed the NVIDIA GPU drivers on your Ubuntu machine and if you need any assistance in that, read the article on how to install the NVIDIA Drivers on Ubuntu 22.04 LTS.

If you already have the NVIDIA drivers installed on your Ubuntu 22.04 LTS machine, make sure it’s up to date. If you need any assistance in updating the NVIDIA drivers on your Ubuntu 22.04 LTS operating system, read the article on How to Update the NVIDIA Drivers on Ubuntu 22.04 LTS.

Updating the APT Package Repository Cache

Once you install the latest version of NVIDIA drivers on Ubuntu, update the APT package repository cache with the following command:

$ sudo apt update

Text Description automatically generated

The APT package repository cache of Ubuntu should be updated.

Text Description automatically generated

Installing GCC and Other Build Tools

To compile the CUDA programs, you need to have GCC, Linux kernel headers, and some other build tools installed on your Ubuntu machine.

To install the GCC compiler, Linux kernel headers, and the required build tools on Ubuntu, run the following command:

$ sudo apt install build-essential linux-headers-$(uname -r)

To confirm the installation, press Y and then press <Enter>.

Text Description automatically generated

GCC, Linux kernel headers, and the required packages are being downloaded. It takes a while to complete.

Text Description automatically generated

GCC, Linux kernel heaers, and the required packages are being installed. It takes a while to complete.

Text Description automatically generated

GCC, Linux kernel headers, and the required build tools for CUDA to work should be installed at this point.

Text Description automatically generated

To check whether you can access the GCC C and C++ compilers, run the following commands:

$ gcc --version

$ g++ --version

Text Description automatically generated

Checking If the Installed NVIDIA Drivers Support the Latest Version of CUDA

To check the maximum CUDA version that the installed NVIDIA GPU drivers support, run the following command:

$ nvidia-smi

Text Description automatically generated

As you can see, the NVIDIA GPU drivers version 530.41.03[1] supports the CUDA version 12.1 or earlier[2]. At the time of this writing, CUDA 12.1 is the latest version of CUDA. So, the installed NVIDIA GPU drivers should support it.

NOTE: At the time you’re reading this article, new versions of CUDA may be released. To check if a newer version of CUDA is released, check the official CUDA downloads page.

Text Description automatically generated

Adding the Official NVIDIA CUDA Repository on Ubuntu

In this section, we will show you how to add the official NVIDIA CUDA repository on Ubuntu 22.04 LTS.

First, open a Terminal app and navigate to the ~/Downloads directory (or any other directory of your choice) as follows:

$ cd ~/Downloads

Text Description automatically generated

To download the official NVIDIA CUDA repository installer, run the following command:

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb

The official NVIDIA CUDA repository installer should be downloaded.

Text Description automatically generated

The official NVIDIA CUDA repository installer is a DEB package file as you can see in the following screenshot:

Graphical user interface, text, application Description automatically generated

To install the official NVIDIA CUDA repository package, run the following command:

$ sudo apt install ./cuda-keyring_1.0-1_all.deb

The official NVIDIA CUDA repository package should be installed and the official NVIDIA CUDA repository should be enabled.

Text Description automatically generated

For the changes to take effect, update the APT package repository cache with the following command:

$ sudo apt update

Text Description automatically generated

Installing the Latest Version of CUDA on Ubuntu

To install the latest version of CUDA on Ubuntu 22.04 LTS, run the following command:

$ sudo apt install cuda

Text Description automatically generated

To confirm the installation, press Y and then press <Enter>.

Text Description automatically generated

The NVIDIA CUDA and the required dependency packages/libraries are being downloaded. It takes a while to complete.

Text Description automatically generated

The NVIDIA CUDA and the required dependency packages/libraries are being installed. It takes a while to complete.

Text Description automatically generated

At this point, the NVIDIA CUDA should be installed.

Text Description automatically generated

Adding CUDA and CUDA Libraries to the Path

Once you installed the latest version of CUDA on Ubuntu 22.04 LTS, you have to add the CUDA binaries and libraries to the path of your Ubuntu 22.04 LTS operating system.

To do that, create a new file /etc/profile.d/cuda.sh and open it with the nano text editor as follows:

$ sudo nano /etc/profile.d/cuda.sh

Text Description automatically generated

Type in the following lines in the /etc/profile.d/cuda.sh file.

export CUDA_HOME="/usr/local/cuda"

export PATH="${CUDA_HOME}/bin${PATH:+:${PATH}}"

export LD_LIBRARY_PATH="${CUDA_HOME}/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/profile.d/cuda.sh file.

Text Description automatically generated

For the changes to take effect, restart your Ubuntu machine with the following command:

$ sudo reboot

A screenshot of a computer Description automatically generated with medium confidence

Once your Ubuntu machine boots, print the values of the PATH and LD_LIBRARY_PATH variables using the following commands to verify that the CUDA binaries and CUDA libraries are in the path of your Ubuntu machine:

$ echo $PATH

$ echo $LD_LIBRARY_PATH

Text Description automatically generated

Allowing the CUDA Binaries to Run with Superuser Privileges

At times, you may need to run some CUDA tools with superuser privileges. To run the CUDA tools with superuser privileges (via sudo), you must add the CUDA directory /usr/local/cuda/bin (where the latest version of CUDA is installed) to the /etc/sudoers file.

First, open the /etc/sudoers configuration file to edit with the following command:

$ sudo visudo -f /etc/sudoers

Text Description automatically generated

Add the text :/usr/local/cuda/bin at the end of the secure_path of the sudoers file as marked in the following screenshot.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the /etc/sudoers file.

A screenshot of a computer Description automatically generated

Testing If the Latest Version of CUDA Is Installed on Ubuntu

To check whether the latest version of CUDA is installed successfully on Ubuntu, run the following command:

$ nvcc --version

Text Description automatically generated

As you can see, CUDA version 12.1 (the latest version of CUDA at the time of this writing) is installed on our Ubuntu machine.

Text Description automatically generated

Writing, Compiling, and Running a Simple CUDA Program

Now that you installed the latest version of CUDA on your Ubuntu 22.04 LTS machine, we will show you how to write, compile, and run a very simple CUDA hello world program.

First, create a new file “hello.cu” (in the ~/codes directory if you want to follow along). Then, open it with a code editor of your choice and type in the following lines of codes:

NOTE: CUDA source files end with the “.cu” extension.

#include <stdio.h>

__global__ void sayHello() {

printf("Hello world from the GPU!\n");

}

int main() {

  printf("Hello world from the CPU!\n");

  sayHello<<<1,1>>>();

  cudaDeviceSynchronize();

 

  return 0;

}

Once you’re done, save the “hello.cu” file.

Graphical user interface, text, application Description automatically generated

To compile the “hello.cu” CUDA program, open a Terminal and navigate to the ~/codes directory (or the directory where you saved the “hello.cu” file).

$ cd ~/codes

Graphical user interface, text Description automatically generated

The “hello.cu” CUDA program should be in this directory.

$ ls -lh

Text Description automatically generated

To compile the “hello.cu” CUDA program with the nvcc CUDA compiler and create an executable hello, run the following command:

$ nvcc hello.cu -o hello

Text Description automatically generated

The “hello.cu”  CUDA program should be compiled without any errors and a new executable/binary hello file  should be created as you can see in the following screenshot:

$ ls -lh

Text Description automatically generated

You can run the compiled hello  CUDA program  as follows:

$ ./hello

Text Description automatically generated

If you see the following output, CUDA is working just fine on your Ubuntu machine. You should have no problems in compiling and running the CUDA programs.

Graphical user interface, text Description automatically generated

Conclusion

We showed you how to install the latest version of CUDA on Ubuntu 22.04 LTS from the official NVIDIA CUDA repository. We also showed you how to write a simple CUDA program, compile it with the latest version of CUDA, and run it on Ubuntu 22.04 LTS.

References:

  1. CUDA Toolkit Downloads | NVIDIA
  2. NVIDIA CUDA Installation Guide for Linux

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.