How to Install NVIDIA CUDA and cuDNN on Ubuntu: Complete Setup Guide for GPU Acceleration

1. Introduction

CUDA (Compute Unified Device Architecture) is a parallel computing platform and API provided by NVIDIA that enables high-speed processing using GPUs.
It is widely used in fields such as machine learning, deep learning, and scientific computation.
This article explains the step-by-step procedure for installing CUDA in an Ubuntu environment.

2. Prerequisites

2.1 How to Check for a Supported GPU

First, verify whether the NVIDIA GPU installed in your system is compatible with CUDA.
Run the following command in the terminal:

lspci | grep -i nvidia

If an NVIDIA device appears in the output, your GPU is recognized.
You can check the full list of supported GPUs on NVIDIA’s official website.

2.2 Check the Ubuntu Version

CUDA supports specific Ubuntu versions.
Use the following command to check your current Ubuntu version:

lsb_release -a

In general, Ubuntu LTS (Long Term Support) releases are recommended.
Refer to NVIDIA’s official documentation for the latest support information.

2.3 Check Whether gcc Is Installed

The gcc compiler is required to install CUDA.
Check the installation status with the following command:

gcc --version

If gcc is not installed, run this command to install it:

sudo apt install build-essential

3. Installing the NVIDIA Driver

3.1 Remove Existing Drivers

If older NVIDIA drivers are installed, remove them to avoid conflicts.
Run the following commands:

sudo apt-get --purge remove '*nvidia*'
sudo apt-get autoremove

3.2 Select and Install the Appropriate Driver

Check NVIDIA’s official website to find the correct driver for your GPU, then install it using the steps below.

  1. Add the repository — Run the following commands to add the NVIDIA driver repository:
   sudo add-apt-repository ppa:graphics-drivers/ppa
   sudo apt-get update
  1. Check recommended drivers — Use this command to find the recommended driver:
   ubuntu-drivers devices

Install the driver labeled as “recommended”.

  1. Install the driver — Specify the recommended version when installing:
   sudo apt install nvidia-driver-<recommended-version>
  1. Reboot the system — After installation, restart Ubuntu:
   sudo reboot

4. Installing the CUDA Toolkit

4.1 Selecting the CUDA Version

On the official CUDA download page, check which CUDA version is compatible with your GPU and Ubuntu release.
If you choose the latest version, ensure compatibility with your software and libraries.

4.2 Add Repository and Install

Follow the steps below to install the CUDA Toolkit.

  1. Add the repository — The following example uses Ubuntu 20.04:
   wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
   sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
  1. Add the repository key — Retrieve and install the repository key:
   sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
  1. Install the CUDA package — Install the CUDA Toolkit:
   sudo apt update
   sudo apt install cuda
  1. Verify installation — Confirm that CUDA is installed:
   nvcc --version

5. Setting Environment Variables

5.1 Configure PATH and LD_LIBRARY_PATH

To use CUDA, you must configure the environment variables properly. Follow these steps:

  1. Edit the .bashrc file
   nano ~/.bashrc
  1. Add the following lines
   export PATH=/usr/local/cuda/bin:$PATH
   export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
  1. Apply the changes — Save and reload the terminal:
   source ~/.bashrc

6. Installing cuDNN

6.1 What Is cuDNN?

cuDNN (CUDA Deep Neural Network library) is a GPU-accelerated library optimized for deep learning workloads.

6.2 Download cuDNN

Download the cuDNN version compatible with your installed CUDA from the official NVIDIA website.
An NVIDIA account is required to download.

6.3 Installation Procedure

  1. Extract the archive — Decompress the downloaded cuDNN archive:
   tar -xzvf cudnn-<version>.tgz
  1. Copy the files — Copy the necessary files into the CUDA directory:
   sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
   sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
   sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
  1. Verify installation — Check the cuDNN version with this command:
   cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

7. Verify the Installation

7.1 Check CUDA Operation

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

nvcc --version

7.2 Run Sample Programs

Execute CUDA sample programs to test functionality.

  1. Set up the samples
   cuda-install-samples-<version>.run
   cd ~/NVIDIA_CUDA-<version>_Samples/1_Utilities/deviceQuery
   make
  1. Run the program
   ./deviceQuery

If the output displays “PASS”, the installation was successful.

8. Troubleshooting

8.1 Common Issues and Solutions

  • Issue: CUDA is not recognized
    Solution: Recheck your environment variables and reboot the system.
  • Issue: GPU is not being used
    Solution: Try reinstalling the NVIDIA driver.
  • Issue: Incompatibility between CUDA and your software
    Solution: Check which CUDA version your software supports and install the corresponding version.

9. Conclusion

This article provided a detailed walkthrough for installing CUDA and cuDNN in an Ubuntu environment.
By following these steps accurately, you can build a high-performance GPU computing environment.
If you plan to use deep learning or scientific computation, consider setting up TensorFlow or PyTorch as your next step.

年収訴求