In this article, I will show you how to install CUDA on Ubuntu 20.04 LTS from the official package repository of Ubuntu. I will also show you how to install the latest version of CUDA on Ubuntu 20.04 LTS from the official NVIDIA package repository. I will show you how to write, compile, and run your very first CUDA program as well. So, let’s get started.
Table of Contents
- Pre-requisites
- Updating Package Repository Cache
- Installing GCC and Other Build Tools
- Installing CUDA from the Official Ubuntu Package Repository
- Installing the Latest Version of CUDA from the Official NVIDIA Package Repository
- Writing a Hello World Program with CUDA
- Conclusion
- References
Pre-requisites
Before you get started,
i) You must have an NVIDIA GPU installed on your computer.
ii) You must have the NVIDIA GPU drivers installed on your computer.
To confirm whether the NVIDIA GPU drivers are working, run the following command:
If the NVIDIA driver kernel modules are working, you should see the following output.
If the NVIDIA drivers are working correctly, NVIDIA command-line tools should work as expected.
NVIDIA graphical tools like the NVIDIA X Server Settings app should also work.
Updating Package Repository Cache:
Once you have fulfilled all the requirements, update the APT package repository cache with the following command:
The APT package repository cache should be updated.
Installing GCC and Other Build Tools:
For CUDA to work, you must have GCC and other build tools installed on your computer.
You can install GCC and all the required build tools from the official package repository of Ubuntu with the following command:
To confirm the installation, press Y and then press
All the required packages are being downloaded. It will take a while to complete.
Once the packages are downloaded, APT will install them one by one. It will take a while to complete.
At this point, GCC and all the required build tools should be installed.
As you can see, the GCC compiler is working just fine.
$ g++ –version
Installing CUDA from the Official Ubuntu Package Repository
CUDA version 10 is available in the official package repository of Ubuntu 20.04 LTS.
To install CUDA v10 from the official package repository of Ubuntu 20.04 LTS, run the following command:
To confirm the installation, press Y and then press
All the required packages will be downloaded from the internet. It will take a while to complete.
Once the required packages are downloaded, they will be installed one by one. It will take a while to complete.
At this point, CUDA and all the required dependencies should be installed.
To confirm whether CUDA is working, run the following command:
Installing the Latest Version of CUDA from the Official NVIDIA Package Repository
At the time of this writing, CUDA 11 is the latest version of CUDA. You can install the latest version of CUDA from the official package repository of NVIDIA.
First, run the following command to make sure that the required Linux headers are installed.
The Linux kernel headers should be installed. In my case, they are already installed.
Now, download the CUDA repository Pin file from the official website of NVIDIA with the following command:
To add the GPG key of the official NVIDIA package repository, run the following command:
The GPG key of the official NVIDIA package repository should be added to the APT package manager.
To add the official NVIDIA CUDA package repository, run the following command:
The official NVIDIA CUDA package repository should be added.
Now, update the APT package repository cache with the following command:
Before you install the latest version of CUDA from the official NVIDIA package repository, it’s a good idea to update all the existing packages of your Ubuntu 20.04 LTS machine.
To update all the existing packages of your Ubuntu 20.04 LTS machine, run the following command:
To confirm the update, press Y and then press
All the required updates are being downloaded from the internet. It will take a while to complete.
Once the updates are downloaded, the APT package manager will install them one by one. It will take a while to complete.
At this point, all the updates should be installed.
Now, you should be able to install the latest version of CUDA from the official package repository of NVIDIA as follows:
To confirm the installation, press Y and then press
The latest version of CUDA and all the required dependency packages should be downloaded and installed. It will take a while to complete.
At this point, the latest version of CUDA and all the required dependency packages should be installed.
To add CUDA to the path, create a new script cuda.sh in the /etc/profile.d/ directory as follows:
Type in the following lines in the cuda.sh script.
export PATH=“$PATH:$CUDA_HOME/bin”
Once you’re done, press
For the changes to take effect, reboot your computer as follows:
You should be able to access CUDA tools as you can see in the screenshot below.
For running some of the CUDA tools, you may need superuser privileges. To allow sudo to run binary files from the /usr/local/cuda/bin directory (where the latest version of CUDA is installed from the official NVIDIA package repository) with superuser privileges, you will have to configure the /etc/sudoers file.
Open the /etc/sudoers configuration file for editing with the following command:
Find the marked line on the /etc/sudoers file.
At the end of the secure_path, append :/usr/local/cuda/bin as marked in the screenshot below.
Once you’re done, press
From now on, if needed, you should be able to run CUDA tools with superuser privileges with sudo.
Writing a Hello World Program with CUDA
In this section, I am going to show you how to write your very first CUDA program. The program will simply print a message from the GPU and a message from the CPU. If this program runs successfully, you can confirm that CUDA is working on your computer.
Create a new file hello.cu and type in the following lines of codes:
NOTE: CUDA source files end with the extension .cu
Once you’re done, save the hello.cu file in the ~/codes directory or any other directory of your choice.
Navigate to the ~/codes directory or the directory where you have saved the hello.cu file.
To compile the hello.cu CUDA source file with the CUDA compiler nvcc, run the following command:
The CUDA source file hello.cu should be compiled without any errors and a new executable/binary file hello should be created as you can see in the screenshot below.
You can run the compiled CUDA program hello as follows:
If you see the following output, then CUDA is working just fine on your computer. You should have no problems compiling and running CUDA programs.
Conclusion
In this article, I have shown you how to install CUDA on Ubuntu 20.04 LTS from the official package repository of Ubuntu. I have also shown you how to install the latest version of CUDA on Ubuntu 20.04 LTS from the official NVIDIA package repository. I have shown you how to write your first CUDA program, compile it, and run it as well.
References
[1] CUDA Zone | NVIDIA Developer
[2] Installation Guide Linux :: CUDA Toolkit Documentation (nvidia.com)
[3] Tutorial 01: Say Hello to CUDA – CUDA Tutorial (cuda-tutorial.readthedocs.io)
[4] Your First CUDA C Program – YouTube
[5] cuda Tutorial => Let’s launch a single CUDA thread to say hello (riptutorial.com)
Discover more from Ubuntu-Server.com
Subscribe to get the latest posts sent to your email.