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 programs on the NVIDIA Graphics Processing Units (GPUs) to speed up computing applications dramatically.

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

  1. Pre-requisites
  2. Updating Package Repository Cache
  3. Installing GCC and Other Build Tools
  4. Installing CUDA from the Official Ubuntu Package Repository
  5. Installing the Latest Version of CUDA from the Official NVIDIA Package Repository
  6. Writing a Hello World Program with CUDA
  7. Conclusion
  8. 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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-798.png" data-lazy- height="88" src="data:image/svg xml,” width=”617″>

If the NVIDIA driver kernel modules are working, you should see the following output.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-801.png" data-lazy- height="276" src="data:image/svg xml,” width=”1087″>

If the NVIDIA drivers are working correctly, NVIDIA command-line tools should work as expected.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-802.png" data-lazy- height="705" src="data:image/svg xml,” width=”1086″>

NVIDIA graphical tools like the NVIDIA X Server Settings app should also work.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-805.png" data-lazy- height="531" src="data:image/svg xml,” width=”828″>

Updating Package Repository Cache:

Once you have fulfilled all the requirements, update the APT package repository cache with the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-809.png" data-lazy- height="92" src="data:image/svg xml,” width=”470″>

The APT package repository cache should be updated.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-811.png" data-lazy- height="331" src="data:image/svg xml,” width=”945″>

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:

$ sudo apt install build-essential

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-812.png" data-lazy- height="84" src="data:image/svg xml,” width=”772″>

To confirm the installation, press Y and then press .

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-815.png" data-lazy- height="686" src="data:image/svg xml,” width=”1076″>

All the required packages are being downloaded. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-817.png" data-lazy- height="691" src="data:image/svg xml,” width=”1073″>

Once the packages are downloaded, APT will install them one by one. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-820.png" data-lazy- height="694" src="data:image/svg xml,” width=”1074″>

At this point, GCC and all the required build tools should be installed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-821.png" data-lazy- height="689" src="data:image/svg xml,” width=”1075″>

As you can see, the GCC compiler is working just fine.

$ gcc –version

$ g –version

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-823.png" data-lazy- height="410" src="data:image/svg xml,” width=”1076″>

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:

$ sudo apt install nvidia-cuda-toolkit

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-824.png" data-lazy- height="82" src="data:image/svg xml,” width=”693″>

To confirm the installation, press Y and then press .

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-826.png" data-lazy- height="644" src="data:image/svg xml,” width=”1005″>

All the required packages will be downloaded from the internet. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-828.png" data-lazy- height="651" src="data:image/svg xml,” width=”1002″>

Once the required packages are downloaded, they will be installed one by one. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-830.png" data-lazy- height="644" src="data:image/svg xml,” width=”1004″>

At this point, CUDA and all the required dependencies should be installed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-833.png" data-lazy- height="646" src="data:image/svg xml,” width=”997″>

To confirm whether CUDA is working, run the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-835.png" data-lazy- height="210" src="data:image/svg xml,” width=”611″>

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.

$ sudo apt install linux-headers-$(uname -r) -y

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-837.png" data-lazy- height="90" src="data:image/svg xml,” width=”859″>

The Linux kernel headers should be installed. In my case, they are already installed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-839.png" data-lazy- height="273" src="data:image/svg xml,” width=”1078″>

Now, download the CUDA repository Pin file from the official website of NVIDIA with the following command:

$ sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-840.png" data-lazy- height="110" src="data:image/svg xml,” width=”1313″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-843.png" data-lazy- height="484" src="data:image/svg xml,” width=”1316″>

To add the GPG key of the official NVIDIA package repository, run the following command:

$ sudo apt-key adv –fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-844.png" data-lazy- height="111" src="data:image/svg xml,” width=”1310″>

The GPG key of the official NVIDIA package repository should be added to the APT package manager.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-847.png" data-lazy- height="323" src="data:image/svg xml,” width=”1305″>

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

$ sudo add-apt-repository “deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /”

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-848.png" data-lazy- height="112" src="data:image/svg xml,” width=”1306″>

The official NVIDIA CUDA package repository should be added.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-850.png" data-lazy- height="460" src="data:image/svg xml,” width=”1315″>

Now, update the APT package repository cache with the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-853.png" data-lazy- height="376" src="data:image/svg xml,” width=”1307″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-854.png" data-lazy- height="96" src="data:image/svg xml,” width=”550″>

To confirm the update, press Y and then press .

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-856.png" data-lazy- height="567" src="data:image/svg xml,” width=”1310″>

All the required updates are being downloaded from the internet. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-858.png" data-lazy- height="564" src="data:image/svg xml,” width=”1317″>

Once the updates are downloaded, the APT package manager will install them one by one. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-860.png" data-lazy- height="568" src="data:image/svg xml,” width=”1317″>

At this point, all the updates should be installed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-861.png" data-lazy- height="510" src="data:image/svg xml,” width=”1184″>

Now, you should be able to install the latest version of CUDA from the official package repository of NVIDIA as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-863.png" data-lazy- height="88" src="data:image/svg xml,” width=”555″>

To confirm the installation, press Y and then press .

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-865.png" data-lazy- height="689" src="data:image/svg xml,” width=”1071″>

The latest version of CUDA and all the required dependency packages should be downloaded and installed. It will take a while to complete.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-867.png" data-lazy- height="617" src="data:image/svg xml,” width=”1252″>

At this point, the latest version of CUDA and all the required dependency packages should be installed.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-869.png" data-lazy- height="555" src="data:image/svg xml,” width=”886″>

To add CUDA to the path, create a new script cuda.sh in the /etc/profile.d/ directory as follows:

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-870.png" data-lazy- height="89" src="data:image/svg xml,” width=”696″>

Type in the following lines in the cuda.sh script.

export CUDA_HOME=“https://linuxhint.com/usr/local/cuda”

export PATH=$PATH:$CUDA_HOME/bin”

Once you’re done, press X followed by Y and to save the cuda.sh script.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-871.png" data-lazy- height="437" src="data:image/svg xml,” width=”893″>

For the changes to take effect, reboot your computer as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-873.png" data-lazy- height="87" src="data:image/svg xml,” width=”423″>

You should be able to access CUDA tools as you can see in the screenshot below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-874.png" data-lazy- height="252" src="data:image/svg xml,” width=”640″>

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:

$ sudo visudo -f /etc/sudoers

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-875.png" data-lazy- height="81" src="data:image/svg xml,” width=”632″>

Find the marked line on the /etc/sudoers file.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-876.png" data-lazy- height="697" src="data:image/svg xml,” width=”1080″>

At the end of the secure_path, append :/usr/local/cuda/bin as marked in the screenshot below.

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-877.png" data-lazy- height="698" src="data:image/svg xml,” width=”1087″>

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

#include

__global__ void say_hello() {

printf(“Hello world from the GPU!n);

}

int main() {

printf(“Hello world from the CPU!n);

say_hello<>();


cudaDeviceSynchronize();

    return 0;

}

Once you’re done, save the hello.cu file in the ~/codes directory or any other directory of your choice.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-881.png" data-lazy- height="533" src="data:image/svg xml,” width=”804″>

Navigate to the ~/codes directory or the directory where you have saved the hello.cu file.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-883.png" data-lazy- height="114" src="data:image/svg xml,” width=”407″>

To compile the hello.cu CUDA source file with the CUDA compiler nvcc, run the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-884.png" data-lazy- height="88" src="data:image/svg xml,” width=”640″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-888.png" data-lazy- height="201" src="data:image/svg xml,” width=”840″>

You can run the compiled CUDA program hello as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-891.png" data-lazy- height="84" src="data:image/svg xml,” width=”443″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-894.png" data-lazy- height="172" src="data:image/svg xml,” width=”674″>

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)

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/photo2-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

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.