Ubuntu

How to Install CUDA on Ubuntu 20.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 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:

$ lsmod | grep nvidia

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-smi

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:

$ sudo apt update

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:

$ sudo apt install build-essential

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

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.

$ gcc --version

$ 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:

$ sudo apt install nvidia-cuda-toolkit

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

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:

$ nvcc --version

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

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:

$ 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

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

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:

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

The official NVIDIA CUDA package repository should be added.

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

$ sudo apt update

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:

$ sudo apt full-upgrade

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

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:

$ sudo apt install cuda

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

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:

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

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

export CUDA_HOME="/usr/local/cuda"

export PATH="$PATH:$CUDA_HOME/bin"

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the cuda.sh script.

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

$ sudo reboot

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

$ nvcc --version

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

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 <Ctrl> + X followed by Y and <Enter> to save the /etc/sudoers file.

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 <stdio.h>

__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.

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

$ cd ~/codes

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

$ nvcc hello.cu -o hello

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:

$ ./hello

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)

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.