How to install cuda on ubuntu 20. 04 lts

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.

Sponsored

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

How to install cuda on ubuntu 20. 04 lts 1

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

How to install cuda on ubuntu 20. 04 lts 2

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

$ nvidia-smi

How to install cuda on ubuntu 20. 04 lts 3

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

How to install cuda on ubuntu 20. 04 lts 4

Updating Package Repository Cache:

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

$ sudo apt update

How to install cuda on ubuntu 20. 04 lts 5

The APT package repository cache should be updated.

How to install cuda on ubuntu 20. 04 lts 6

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

How to install cuda on ubuntu 20. 04 lts 7

To confirm the installation, press Y and then press .

How to install cuda on ubuntu 20. 04 lts 8

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

How to install cuda on ubuntu 20. 04 lts 9

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

How to install cuda on ubuntu 20. 04 lts 10

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

See also  What is the telco core network?

How to install cuda on ubuntu 20. 04 lts 11

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

$ gcc –version

$ g++ –version

How to install cuda on ubuntu 20. 04 lts 12

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

How to install cuda on ubuntu 20. 04 lts 13

To confirm the installation, press Y and then press .

How to install cuda on ubuntu 20. 04 lts 14

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

How to install cuda on ubuntu 20. 04 lts 15

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

How to install cuda on ubuntu 20. 04 lts 16

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

How to install cuda on ubuntu 20. 04 lts 17

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

$ nvcc –version

How to install cuda on ubuntu 20. 04 lts 18

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

How to install cuda on ubuntu 20. 04 lts 19

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

How to install cuda on ubuntu 20. 04 lts 20

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

How to install cuda on ubuntu 20. 04 lts 21

How to install cuda on ubuntu 20. 04 lts 22

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

How to install cuda on ubuntu 20. 04 lts 23

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

How to install cuda on ubuntu 20. 04 lts 24

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/ /”

How to install cuda on ubuntu 20. 04 lts 25

The official NVIDIA CUDA package repository should be added.

How to install cuda on ubuntu 20. 04 lts 26

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

$ sudo apt update

How to install cuda on ubuntu 20. 04 lts 27

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.

Sponsored

To update all the existing packages of your Ubuntu 20.04 LTS machine, run the following command:

$ sudo apt full-upgrade

How to install cuda on ubuntu 20. 04 lts 28

To confirm the update, press Y and then press .

How to install cuda on ubuntu 20. 04 lts 29

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

How to install cuda on ubuntu 20. 04 lts 30

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

How to install cuda on ubuntu 20. 04 lts 31

At this point, all the updates should be installed.

How to install cuda on ubuntu 20. 04 lts 32

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

How to install cuda on ubuntu 20. 04 lts 33

To confirm the installation, press Y and then press .

How to install cuda on ubuntu 20. 04 lts 34

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

How to install cuda on ubuntu 20. 04 lts 35

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

How to install cuda on ubuntu 20. 04 lts 36

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

How to install cuda on ubuntu 20. 04 lts 37

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 + X followed by Y and to save the cuda.sh script.

How to install cuda on ubuntu 20. 04 lts 38

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

$ sudo reboot

How to install cuda on ubuntu 20. 04 lts 39

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

$ nvcc –version

How to install cuda on ubuntu 20. 04 lts 40

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

How to install cuda on ubuntu 20. 04 lts 41

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

How to install cuda on ubuntu 20. 04 lts 42

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.

How to install cuda on ubuntu 20. 04 lts 43

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.

How to install cuda on ubuntu 20. 04 lts 44

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

$ cd ~/codes

How to install cuda on ubuntu 20. 04 lts 45

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

$ nvcc hello.cu -o hello

How to install cuda on ubuntu 20. 04 lts 46

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.

How to install cuda on ubuntu 20. 04 lts 47

You can run the compiled CUDA program hello as follows:

$ ./hello

How to install cuda on ubuntu 20. 04 lts 48

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.

How to install cuda on ubuntu 20. 04 lts 49

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.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply