5.7 KiB
How to Install the Latest Version of NVIDIA CUDA on Ubuntu 22.04 LTS
If you’re looking to unlock the power of your NVIDIA GPU for scientific computing, machine learning, or other parallel workloads, CUDA is essential. Follow this step-by-step guide to install the latest CUDA release on Ubuntu 22.04 LTS.
Prerequisites
Before proceeding with installing CUDA, ensure your system meets the following requirements:
- Ubuntu 22.04 LTS – This version is highly recommended for stability and compatibility.
- NVIDIA GPU + Drivers – CUDA requires having an NVIDIA GPU along with proprietary Nvidia drivers installed.
To check for an NVIDIA GPU, open a terminal and run:
lspci | grep -i NVIDIA
If an NVIDIA GPU is present, it will be listed. If not, consult NVIDIA’s documentation on installing the latest display drivers.
Step 1: Install Latest NVIDIA Drivers
Install the latest NVIDIA drivers matched to your GPU model and CUDA version using Ubuntu’s built-in Additional Drivers utility:
- Open Settings -> Software & Updates -> Additional Drivers
- Select the recommended driver under the NVIDIA heading
- Click Apply Changes and Reboot
Verify the driver installation by running:
nvidia-smi
This should print details on your NVIDIA GPU and driver version.
Step 2: Add the CUDA Repository
Add NVIDIA’s official repository to your system to install CUDA:
- Visit NVIDIA’s CUDA Download Page and select "Linux", "x86_64", "Ubuntu", "22.04", "deb(network)"
- Copy the repository installation commands for Ubuntu 22.04:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
Run these commands to download repository metadata and add the apt source.
Step 3: Install CUDA Toolkit
Install CUDA using apt:
sudo apt-get -y install cuda
Press Y to proceed and allow the latest supported version of the CUDA toolkit to install.
Step 4: Configure Environment Variables
Update environment variables to recognize the CUDA compiler, tools, and libraries:
Open /etc/profile.d/cuda.sh and add the following configuration:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Save changes and refresh environment variables:
source /etc/profile.d/cuda.sh
Alternatively, reboot to load the updated environment variables.
Step 5: Verify Installation
Validate the installation:
-
Check the
nvcccompiler version:nvcc --versionThis should display details on the CUDA compile driver, including the installed version.
-
Verify GPU details with NVIDIA SMI:
nvidia-smi
Optional: Setting Up cuDNN with CUDA: A Comprehensive Guide
This guide will walk you through downloading cuDNN from NVIDIA's official site, extracting it, copying the necessary files to the CUDA directory, and setting up environment variables for CUDA.
Step 1: Download cuDNN
-
Visit the NVIDIA cuDNN Archive: Navigate to the NVIDIA cuDNN Archive.
-
Select the Version: Choose the appropriate version of cuDNN compatible with your CUDA version. For this guide, we'll assume you are downloading
cudnn-linux-x86_64-8.9.7.29_cuda12-archive. -
Download the Archive: Download the
tar.xzfile to your local machine.
Step 2: Extract cuDNN
-
Navigate to the Download Directory: Open a terminal and navigate to the directory where the archive was downloaded.
cd ~/Downloads -
Extract the Archive: Use the
tarcommand to extract the contents of the archive.tar -xvf cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xzThis will create a directory named
cudnn-linux-x86_64-8.9.7.29_cuda12-archive.
Step 3: Copy cuDNN Files to CUDA Directory
-
Navigate to the Extracted Directory: Move into the directory containing the extracted cuDNN files.
cd cudnn-linux-x86_64-8.9.7.29_cuda12-archive -
Copy Header Files: Copy the header files to the CUDA include directory.
sudo cp include/cudnn*.h /usr/local/cuda-12.5/include/ -
Copy Library Files: Copy the library files to the CUDA lib64 directory.
sudo cp lib/libcudnn* /usr/local/cuda-12.5/lib64/ -
Set Correct Permissions: Ensure the copied files have the appropriate permissions.
sudo chmod a+r /usr/local/cuda-12.5/include/cudnn*.h /usr/local/cuda-12.5/lib64/libcudnn*
Step 4: Set Up Environment Variables
-
Open Your Shell Profile: Open your
.bashrcor.bash_profilefile in a text editor.nano ~/.bashrc -
Add CUDA to PATH and LD_LIBRARY_PATH: Add the following lines to set the environment variables for CUDA. This example assumes CUDA 12.5.
export PATH=/usr/local/cuda-12.5/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-12.5/lib64:$LD_LIBRARY_PATH -
Apply the Changes: Source the file to apply the changes immediately.
source ~/.bashrc
Verification
-
Check CUDA Installation: Verify that CUDA is correctly set up by running:
nvcc --version -
Check cuDNN Installation: Optionally, you can compile and run a sample program to ensure cuDNN is working correctly.
By following these steps, you will have downloaded and installed cuDNN, integrated it into your CUDA setup, and configured your environment variables for smooth operation. This ensures that applications requiring both CUDA and cuDNN can run without issues.