Ubuntu

How to Install 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 developed by NVIDIA. It is used to run the programs on NVIDIA Graphics Processing Units (GPUs) to dramatically speed up the computing applications.

In this article, we will show you how to install CUDA on Ubuntu 22.04 LTS from the official package repository of Ubuntu. We will also show you how to write, compile, and run your very first CUDA program on Ubuntu 22.04 LTS as well.

Topic of Contents:

  1. Prerequisites
  2. Installing the NVIDIA Drivers on Ubuntu
  3. Updating the APT Package Repository Cache
  4. Installing GCC and Other Build Tools
  5. Installing CUDA on Ubuntu from the Official Ubuntu Package Repository
  6. Testing If CUDA Is Installed Successfully on Ubuntu
  7. Writing, Compiling, and Running a Simple CUDA Program
  8. Conclusion

Prerequisites

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

  1. An installed NVIDIA GPU on your computer.
  2. Installed NVIDIA GPU drivers on your Ubuntu operating system.

Installing NVIDIA Drivers on Ubuntu

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

Updating the APT Package Repository Cache

Once you installed the NVIDIA drivers on Ubuntu, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache of Ubuntu should be updated.

Installing GCC and Other Build Tools

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

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

$ sudo apt install build-essential

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

GCC and the required packages are now being downloaded. It takes a while to complete.

GCC and the required packages are now being installed. It takes a while to complete.

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

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

$ gcc --version
$ g++ --version

Install CUDA on Ubuntu from the Official Ubuntu Package Repository

To install CUDA from the official package repository of Ubuntu, run the following command:

$ sudo apt install nvidia-cuda-toolkit nvidia-cuda-toolkit-gcc

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

CUDA and the required packages are now being downloaded. It takes a while to complete.

CUDA and the required packages are now being installed. It takes a while to complete.

CUDA should be installed at this point.

Testing If CUDA Is Installed Successfully on Ubuntu

To check if CUDA is installed successfully on Ubuntu, run the following command:

$ nvcc --version

As you can see, CUDA version 11.5 is installed on our Ubuntu machine.

Writing, Compiling, and Running a Simple CUDA Program

Now that you installed 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 “hello.cu” file (in the ~/codes directory if you want to follow along). 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.

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

$ cd ~/codes

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

$ ls -lh

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

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

You can run the compiled “hello” CUDA program as follows:

$ ./hello

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

Conclusion

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

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.