The Nvidia CUDA toolkit is an extension of the GPU parallel computing platform and programming model. The Nvidia CUDA installation consists of inclusion of the official Nvidia CUDA repository followed by the installation of relevant meta package and configuring path the the executable CUDA binaries.

In this tutorial you will learn:
  • How to install CUDA toolkit from Ubuntu Repository
  • How to install CUDA toolkit from CUDA repository
  • How to compile example CUDA C code and execute program
  • How to Check CUDA version
CUDA on Ubuntu 20.04 Focal Fossa Linux
CUDA on Ubuntu 20.04 Focal Fossa Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software CUDA
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

How to install CUDA on Ubuntu 20.04 step by step instructions


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


How to install CUDA toolkit from Ubuntu Repository

  1. Although you might not end up witht he latest CUDA toolkit version, the easiest way to install CUDA on Ubuntu 20.04 is to perform the installation from Ubuntu's standard repositories.

    To install CUDA execute the following commands:
    $ sudo apt update
    $ sudo apt install nvidia-cuda-toolkit
    
  2. All should be ready now. Check your CUDA version:
    $ nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2019 NVIDIA Corporation
    Built on Sun_Jul_28_19:07:16_PDT_2019
    Cuda compilation tools, release 10.1, V10.1.243
    
    NOTE
    To install latest and more up to date CUDA version see below How to install CUDA toolkit from CUDA repository section.
  3. Confirm the CUDA toolkit installation by sample CUDA C code compilation. See the bellow Compile a Sample CUDA code section.

How to install CUDA toolkit from CUDA repository



  1. In case you have not done so yet, make sure that you have installed Nvdia driver fro you VGA. To do so follow our guide on How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux.
  2. Setup Nvida CUDA repository.

    NOTE
    At the time of writing the Ubuntu 20.04 Cuda driver version is not yet available. From this reason we will resort to the the latest stable version which made for Ubuntu 18.04.
    Execute the following commands to enable CUDA repository.
    $ wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
    $ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
    $ sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
    
  3. At this stage all should be ready to install CUDA. Execute the following apt command:
    $ sudo apt install cuda
    
  4. Once ready, set your path to point to CUDA binaries:
    $ echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
    
  5. Check CUDA version to confirm the installation:
    $ nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2019 NVIDIA Corporation
    Built on Wed_Oct_23_19:24:38_PDT_2019
    Cuda compilation tools, release 10.2, V10.2.89
    

Compile a Sample CUDA code

Confirm the installation by compiling an example CUDA C code. Save the following code into a file named eg. hello.cu:
#include <stdio.h>

__global__
void saxpy(int n, float a, float *x, float *y)
{
  int i = blockIdx.x*blockDim.x + threadIdx.x;
  if (i < n) y[i] = a*x[i] + y[i];
}

int main(void)
{
  int N = 1<<20;
  float *x, *y, *d_x, *d_y;
  x = (float*)malloc(N*sizeof(float));
  y = (float*)malloc(N*sizeof(float));

  cudaMalloc(&d_x, N*sizeof(float)); 
  cudaMalloc(&d_y, N*sizeof(float));

  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;
  }

  cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
  cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);

  // Perform SAXPY on 1M elements
  saxpy<<<(N+255)/256, 256>>>(N, 2.0f, d_x, d_y);

  cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);

  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = max(maxError, abs(y[i]-4.0f));
  printf("Max error: %f\n", maxError);

  cudaFree(d_x);
  cudaFree(d_y);
  free(x);
  free(y);
}
Next, use nvcc the Nvidia CUDA compiler to compile the code and run the newly compiled binary:
$ nvcc -o hello hello.cu 
$ ./hello 
Max error: 0.000000

Troubleshooting

At the moment CUDA does not support GCC compiler higher then version 8 when installed from CUDA Ubuntu 18.04 sources. As a result upon the code compilation with the Nvidia CUDA compiler you might receive the following error:
In file included from /usr/local/cuda-10.2/bin/../targets/x86_64-linux/include/cuda_runtime.h:83,
                 from :
/usr/local/cuda-10.2/bin/../targets/x86_64-linux/include/crt/host_config.h:138:2: error: #error -- unsupported GNU version! gcc versions later than 8 are not supported!
  138 | #error -- unsupported GNU version! gcc versions later than 8 are not supported!
      |  ^~~~~
To comply with the CUDA compiler requirements switch your default GCC compiler to version 8 or lower.
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.