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:
- Prerequisites
- Installing the NVIDIA Drivers on Ubuntu
- Updating the APT Package Repository Cache
- Installing GCC and Other Build Tools
- Installing CUDA on Ubuntu from the Official Ubuntu Package Repository
- Testing If CUDA Is Installed Successfully on Ubuntu
- Writing, Compiling, and Running a Simple CUDA Program
- 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:
- An installed NVIDIA GPU on your computer.
- 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:
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:
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:
$ 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:
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:
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.
__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).
The “hello.cu” CUDA program should be in this directory.
To compile the “hello.cu” CUDA program with the “nvcc” CUDA compiler and create an executable “hello”, run the following command:
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:
You can run the compiled “hello” CUDA program as follows:
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.