机器学习 | 联邦学习 | VPS | 摄影 | 日常

[Linux Guide] Step-by-Step Guide to Configuring CUDA and cuDNN Environment on Ubuntu

The article was first published in 若绾 [Linux Guide] Step-by-Step Guide to Configuring CUDA and cuDNN Environment on Ubuntu, please indicate the source when reposting.


As a core tool for deep learning and parallel computing, CUDA (Compute Unified Device Architecture) provides parallel computing capabilities for NVIDIA graphics cards. In this tutorial, we will provide a detailed guide on how to configure CUDA on the Ubuntu operating system. This tutorial uses CUDA 12.1 and Ubuntu 20.04 as examples, but the configuration process is similar for other versions of CUDA and Ubuntu.

Installing CUDA#

Step 1: Confirm System Compatibility#

Before configuring CUDA, make sure your system meets the installation requirements. Here are the items to check:

  1. Make sure your GPU is an NVIDIA GPU and supports CUDA compute capability. You can check the supported GPU list on the NVIDIA official website.
  2. Make sure your system is a 64-bit Ubuntu operating system.
  3. Install a Linux kernel version 4.4 or above.

Step 2: Update System Packages#

Open the terminal and enter the following commands to update the system packages:

sudo apt update
sudo apt upgrade

Step 3: Install NVIDIA Graphics Card Driver#

If your system does not have the NVIDIA graphics card driver installed, you can use the following command to install it:

sudo ubuntu-drivers autoinstall

After the installation is complete, restart your computer to ensure that the driver is loaded correctly.

Step 4: Download CUDA Toolkit#

Go to the CUDA Toolkit official website to download the corresponding version of the CUDA Toolkit installation package (using CUDA 12.1 as an example).

Step 5: Install CUDA Toolkit#

You can run the following commands to download and install the CUDA Toolkit:

sudo mv /etc/apt/preferences.d/cuda-repository-pin-600
sudo dpkg -i cuda-repo-ubuntu2004-12-1-local_12.1.0-530.30.02-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2004-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

Step 6: Configure Environment Variables#

To ensure that the system can correctly recognize the installation location of the CUDA Toolkit, you need to set environment variables. Open the ~/.bashrc file and add the following content:

# Open ~/.bashrc
sudo vim ~/.bashrc

# Add the following two lines at the end of the file and save/close it
export PATH=/usr/local/cuda-12.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Save the file and run the following command to make the environment variables take effect:

source ~/.bashrc

Step 7: Verify CUDA Installation#

Run the following command to verify if CUDA is installed correctly:

nvcc --version

If the installation is successful, you will see output similar to the following, which displays the version information of the CUDA compiler (nvcc):

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Tue_Sep_21_19:24:46_PDT_2021
Cuda compilation tools, release 12.1, VXX.X.X
Build cuda_12.1.r12.1/compiler.XX

Installing cuDNN Library (Optional)#

cuDNN is a GPU acceleration library for deep learning frameworks such as TensorFlow and PyTorch. To install cuDNN, follow these steps:

  1. Go to the NVIDIA official website, register and log in, then go to the cuDNN download page.
  2. Choose the cuDNN version compatible with the CUDA version you installed and download the corresponding compressed package.
  3. Extract the downloaded file. Assuming the file is extracted to the ~/Downloads/cudnn-local-repo-ubuntu2004- directory.
  4. Run the following commands in the terminal to install the cuDNN library. Before running the commands, you must replace X.Y and 8.x.x.x with your specific CUDA and cuDNN versions:
cd ~/Downloads/
sudo dpkg -i cudnn-local-repo-${OS}-8.x.x.x_1.0-1_amd64.deb

# Import GPG key
sudo cp /var/cudnn-local-repo-*/cudnn-local-*-keyring.gpg /usr/share/keyrings/

# Refresh apt sources
sudo apt-get update

# Install the library
sudo apt-get install libcudnn8=8.x.x.x-1+cudaX.Y
sudo apt-get install libcudnn8-dev=8.x.x.x-1+cudaX.Y
sudo apt-get install libcudnn8-samples=8.x.x.x-1+cudaX.Y


Congratulations! You have successfully configured the CUDA environment on Ubuntu. Now you can use the GPU for deep learning and high-performance computing. In your future work, you may also need to install and configure deep learning frameworks such as TensorFlow or PyTorch according to your specific needs.

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.