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.
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:
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.
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.
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
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:
To install CUDA from the official package repository of Ubuntu, run the following command:
To confirm the installation, press Y and then press
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.
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.
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:
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.
2024 was the GenAI year. With new and more performant LLMs and a higher number…
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…