How to Check the CUDA Version on Ubuntu [Easy Command Guide]

目次

1. Introduction

CUDA (Compute Unified Device Architecture) is a parallel computing platform developed by NVIDIA that utilizes GPUs. It is widely used for machine learning, deep learning, 3D rendering, and various other computational tasks.

When using CUDA in an Ubuntu environment, it is crucial to check the CUDA version for the following reasons:

Driver Compatibility

CUDA requires a specific version of the NVIDIA driver to function correctly. If the versions are incompatible, CUDA may not work properly.

Library Compatibility

Libraries like TensorFlow and PyTorch require specific CUDA and cuDNN versions. It is essential to ensure that you have installed the appropriate version.

Preventing System Confusion

If multiple CUDA versions are installed on the system, it is necessary to identify which version is active and switch between versions as needed.

In this article, we will provide a clear explanation of how to check the CUDA version on Ubuntu.

2. How to Check the CUDA Version on Ubuntu

In an Ubuntu environment, you can check the CUDA version using the following methods:

Method 1: Check with the nvidia-smi Command (Easiest Method)

The NVIDIA driver includes a tool called nvidia-smi (NVIDIA System Management Interface) that allows you to check the status of your GPU.

Execution Command

nvidia-smi

Example Output

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 530.41.03    Driver Version: 530.41.03    CUDA Version: 12.1     |
+-----------------------------------------------------------------------------+

Key Points

  • The CUDA Version: 12.1 displayed here represents the maximum CUDA version supported by the NVIDIA driver.
  • This may not always match the installed CUDA toolkit version, so it’s recommended to check using additional methods.

Method 2: Check with the nvcc -V Command (For Developers)

If CUDA is installed correctly, you can check the version of nvcc (the CUDA compiler).

Execution Command

nvcc -V

Example Output

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Sun_Jul_30_19:09:40_PDT_2023
Cuda compilation tools, release 12.1, V12.1.105

Key Points

  • The part that says release 12.1, V12.1.105 indicates the installed CUDA toolkit version.
  • This may differ from the version displayed by nvidia-smi, so be cautious.

Method 3: Check the version.txt File (Manual Verification)

If CUDA is installed in /usr/local/cuda, the version information is recorded in the version.txt file.

Execution Command

cat /usr/local/cuda/version.txt

Example Output

CUDA Version 12.1.105

Key Points

  • This method is useful if the nvcc -V command is unavailable.
  • Ensure that /usr/local/cuda is correctly linked to the desired CUDA version.
侍エンジニア塾

3. How to Check the cuDNN Version

cuDNN (CUDA Deep Neural Network) is a library designed for deep learning and is used in combination with CUDA.
Along with checking the CUDA version, it is also important to verify the cuDNN version.

Method 1: Check the cudnn_version.h File

The cuDNN version is recorded in the header file cudnn_version.h.

Execution Command

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

Example Output

#define CUDNN_MAJOR 8
#define CUDNN_MINOR 9
#define CUDNN_PATCHLEVEL 1

Key Points

  • This output confirms that cuDNN 8.9.1 is installed.
  • Using the grep command allows you to easily retrieve the cuDNN version information.
  • Since cuDNN must be compatible with CUDA, it is important to verify the correct combination of versions.

Method 2: Check with the dpkg Command (For Debian-based Linux)

On Ubuntu and other Debian-based Linux distributions, you can check the installed cuDNN version using the dpkg command.

Execution Command

dpkg -l | grep libcudnn

Example Output

ii  libcudnn8    8.9.1-1+cuda12.1    amd64    NVIDIA cuDNN Library

Key Points

  • The libcudnn8 8.9.1-1+cuda12.1 part confirms the installed cuDNN version (8.9.1).
  • The cuda12.1 part indicates the compatible CUDA version (12.1).

By using these methods, you can ensure that your CUDA environment is properly configured.


4. How to Handle Multiple Installed CUDA Versions

In an Ubuntu environment, multiple CUDA versions can be installed. However, this can sometimes lead to confusion regarding which version is currently active.
In such cases, you need to switch to the appropriate version.

Method 1: Switch Using update-alternatives

On Ubuntu, you can use update-alternatives to switch CUDA versions.

Check Current Settings

update-alternatives --query cuda

Switch CUDA Version

sudo update-alternatives --config cuda

Example Output

There are 3 choices for the alternative cuda (providing /usr/local/cuda).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/local/cuda-11.8  100       auto mode
  1            /usr/local/cuda-10.2  50        manual mode
  2            /usr/local/cuda-11.8  100       manual mode
  3            /usr/local/cuda-12.1  110       manual mode

Press <enter> to keep the current choice[*], or type selection number:

Key Points

  • Executing update-alternatives --config cuda will display a list of available CUDA versions.
  • You can select the desired CUDA version by entering the corresponding number.
  • auto mode and manual mode are available; choose manual mode if you want to switch versions manually.

Method 2: Manually Set a Symbolic Link

You can also switch CUDA versions by modifying the symbolic link.

Check Existing Symbolic Link

ls -l /usr/local/cuda

Example Output

lrwxrwxrwx 1 root root 20 Feb  1 12:34 /usr/local/cuda -> /usr/local/cuda-11.8

Change CUDA Version

sudo rm /usr/local/cuda
sudo ln -s /usr/local/cuda-12.1 /usr/local/cuda

Verify Change

ls -l /usr/local/cuda

Key Points

  • /usr/local/cuda serves as the default CUDA path. Changing this link switches the CUDA version.
  • By using the ln -s command, you can easily change the CUDA version without modifying system-wide configurations.

With these methods, you can efficiently manage multiple CUDA versions and ensure you are using the correct version for your needs.

5. Frequently Asked Questions (FAQ)

Here are some common questions related to checking the CUDA version. If you encounter any issues, refer to these solutions.

Q1: nvcc -V Command Not Found!

If the nvcc command is not found, CUDA may not be installed correctly, or its path is not set.

Solution 1: Check If CUDA Is Installed

ls /usr/local/cuda/

Solution 2: Add nvcc to the Path

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

After running these commands, try executing nvcc -V again to check if the version is displayed correctly.

Q2: Why Is the CUDA Version Displayed by nvidia-smi Different?

The CUDA version displayed by nvidia-smi represents the maximum CUDA version supported by the NVIDIA driver, not necessarily the installed CUDA toolkit version.

How to Check:

nvidia-smi

Example Output:

CUDA Version: 12.1

To check the actual installed CUDA version, use nvcc -V or check the version.txt file.

Q3: How to Check CUDA and cuDNN Compatibility?

The best way to check compatibility between CUDA and cuDNN is to refer to NVIDIA’s official support matrix.

Official Documentation:

NVIDIA cuDNN Support Matrix

Additionally, you can check the installed versions using the following commands:

Check CUDA Version

nvcc -V

Check cuDNN Version

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

By managing your environment correctly, you can avoid CUDA and cuDNN compatibility issues.

6. Summary

In this article, we explained how to check the CUDA version in an Ubuntu environment.
Let’s review the key points.

Ways to Check the CUDA Version

MethodCommandDescription
nvidia-sminvidia-smiShows the CUDA version supported by the NVIDIA driver
nvcc -Vnvcc -VShows the actual installed CUDA toolkit version
version.txtcat /usr/local/cuda/version.txtManually check the CUDA version

Ways to Check the cuDNN Version

MethodCommandDescription
cudnn_version.hcat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2Check the version from the header file
dpkg Commanddpkg -l | grep libcudnnCheck the installed cuDNN version

How to Switch CUDA Versions

MethodCommandDescription
update-alternativessudo update-alternatives --config cudaSwitch between multiple CUDA versions
Symbolic Linksudo ln -s /usr/local/cuda-XX.X /usr/local/cudaManually change the CUDA version

Key Takeaways

  • It is important to correctly identify the CUDA version
  • Ensure compatibility between CUDA and cuDNN
  • If using multiple CUDA versions, understand how to switch between them

By managing your environment properly, you can maximize the benefits of CUDA.
We hope this article helps you check the CUDA version in your Ubuntu environment.

Related Articles