how to get the version of cuda installed on linux

How to Get the Version of CUDA Installed on Linux

In this article, we’ll cover how to get the version of CUDA installed on Linux. In addition, we’ll also look at how to install CUDA on Linux. So, let’s get started!

CUDA Overview

CUDA is a parallel computing platform created by Nvidia. In addition, it provides a GPU-based programming model. Furthermore, the toolkit consists of the official CUDA repository and relevant meta-packages. It also enables users to configure the path of the executable CUDA binaries. 

Prerequisites

To learn how to get the version of CUDA installed on Linux, you’ll need a Linux machine with sudo access. In addition, you must have CUDA installed on your system to get its version. 

How to Get the Version of CUDA Installed on Linux

In this section, we’ll first cover how to get the version of CUDA installed on Linux. Additionally, we’ll cover how to install CUDA on Linux machines. Continue reading the article to learn more.

Get the Version of CUDA Installed on Linux

There are multiple ways to get the CUDA version installed on Linux. Let’s look at each method in detail.

Get the Most Current Version

To get the most current installed version of CUDA on Linux, open the terminal by pressing “Ctrl + Alt + T” and then type:

nvidia-smi

You should get a similar output:

get the version of cuda

From the output, you can see the latest version Number of the NVIDIA driver installed on the Linux machine. In addition, you can also see the maximum version of CUDA that the NVIDIA driver supports. 

Get the Version Number of CUDA

To get the version of CUDA installed on Linux, open the terminal by pressing “Ctrl + Alt + T”. after that, execute the command given below:

nvcc --version

You should see the version number of CUDA installed on Linux as shown below:

get the version of cuda

Install CUDA on Linux

You’ve seen how to get the version of CUDA on Linux. In this section, we’ll cover how to install CUDA on Linux. You can install CUDA Toolkit using either the distribution-specific packages (RPM and Deb packages), or a distribution-independent package (runfile packages). 

The distribution-independent package works on a wider set of Linux distributions. However, it does not update the native Linux package management system. 

In this section, we’ll first cover the prerequisite configurations required to install CUDA on Linux. Next, we’ll look at the installation process. Follow the steps given below to set up CUDA on your machine.

Verify the CUDA Capable GPU

Before starting the installation, verify that the GPU in your system can support CUDA by using the command given below:

lspci | grep -i nvidia

You’ll get an NVIDIA GPU that is present on the system.

check gpu

Next, head over to the NVIDIA site to check the GPU compatibility. 

Check Linux version

After that, the next task is to check the Linux version. Out of several Linux versions, CUDA supports only a few distributions. To find out which distribution you are running, execute the following command:

uname -m && cat /etc/*release

The output should look like this:

check linux kernel

Verify the GCC Installation

Most Linux systems have a pre-installed gcc compiler. To check if it’s available on your system, use the gcc –version command. For example, type:

gcc --version

If it’s not available, then execute the commands given below to install the prerequisite libraries:

sudo apt-get install manpages-dev
sudo apt-get update
sudo apt install build-essential
sudo apt-get install manpages-dev

Verify correct Kernel headers

CUDA supports specific kernel headers and development packages. If your system is running kernel version 3.17.4–301, you must install the 3.17.4–301 kernel headers and development packages.

To check Linux kernel headers, type:

uname -r

Output:

verify correct kernel header

For this step, use the install command with the -r option. This option gives the current Kernel version of the system. Specifically, type:

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

Update the System

Once you are done with these configurations, it is time to install CUDA on Linux. The first step is to update the Linux machine. For this step, use the update and upgrade command. Specifically, type:

sudo apt update && sudo apt upgrade

You should get a similar output:

update the system

Install NVIDIA CUDA on Linux Using the APT Package

After that, install the NVIDIA CUDA toolkit using the APT package manager. For instance, type:

sudo apt install nvidia-cuda-toolkit

The output looks something like this:

install nvidia cuda toolkit

Wait for the installation to complete. Once it is done, check the NVIDIA version as shown below:

nvcc --version

Output:

check nvidia version

Install NVIDIA CUDA on Linux Using the Latest Repository

Alternatively, you can also install NVIDIA CUDA on Linux from the CUDA toolkit. This installation will provide you with the latest CUDA version. For this step, setup the NVIDIA CUDA repository as shown below:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.105-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.1.105-1_amd64.deb

After that, add the APT key:

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

Now, add the repository in the APT package manager. For this step, type:

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

Now, you are ready to install CUDA on Linux. Execute the commands given below:

sudo apt update
sudo apt install cuda

You should get a similar output:

install cuda toolkit

This will install the latest CUDA version. If you want a specific version, let’s say cuda 10.1, then execute this command:

sudo apt-get install cuda-10-1

Wait for the installation to complete. Once ready, set up the CUDA binaries on a Linux machine. In other words, update the bashrc. For this step, use the export command to set up the PATH variable. Specifically, type:

echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
export PATH="/usr/local/cuda-10.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-10.1/lib64:$LD_LIBRARY_PATH"

After that, execute:

source .bashrc

Once done, check the CUDA version as shown below:

nvcc --version

The output looks something like this:

check the cuda version

Alternatively, you can also try the nvidia-smi command. In case it doesn’t work, reboot the system, and it should work.

Compile a CUDA Code

To check if the NVIDIA CUDA is working, we’ll compile a sample CUDA code using C language. For this step, first, create a new C file as shown below:

# nano editor
nano hello.cu
# vim editor
vim hello.cu

Next, add the sample code in the source file:

#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);
}

Hit “Ctrl + S” to save and “Ctrl + X” to exit the text editor. After that, compile the CUDA code by using the nvcc compiler. Specifically, type:

nvcc -o hello hello.cu 

Now to run the file, simply type the filename without extension:

./hello 

Bonus: Troubleshooting

Since the CUDA does not support GCC compiler higher than version 8, you might receive the following error as a result of compilation:

In the 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!

      | ^~~~~

For this error, switch the default GCC compiler version to 8 or lower. 

Conclusion

In this guide, you’ve learned how to get the version of CUDA installed on Linux. You’ve also covered how to install CUDA toolkits in two different methods. To get the latest version, you’ll have to install it using the PPA repository.

Apart from this, you can also learn how to clean install NVIDIA drivers on Ubuntu and Debian to explore NVIDIA toolkit.

If this guide helped you, please share it

Related Posts