How to Configure Ubuntu 22.04 for CUDA Programming: A Comprehensive Guide

June 2024.

How to Configure Ubuntu 22.04 for CUDA Programming and OpenCV: A Comprehensive Guide

Introduction

Setting up a deep learning environment on Ubuntu can be challenging. Navigating through graphics drivers, multiple libraries, and ensuring compatibility can present numerous obstacles. This guide seeks to simplify the setup process by offering clear, step-by-step instructions for installing Nvidia drivers, the CUDA toolkit, cuDNN, GCC, and OpenCV with CUDA and cuDNN support on Ubuntu 22.04. Whether you're working on a personal project or configuring a professional workstation, these instructions will help you establish a reliable environment for your deep learning tasks.

Prerequisites

  • A fresh installation of Ubuntu 22.04 LTS.
  • A Nvidia GPU (I have an RTX 4060).
  • Internet connection.

Step 1: Prepare Your System

Blacklist Nouveau Driver

Nouveau, the open-source graphics driver for Nvidia cards, needs to be disabled to install the official Nvidia driver.

sudo bash -c "echo blacklist nouveau > /etc/modprobe.d/blacklist-nvidia-nouveau.conf"
sudo bash -c "echo options nouveau modeset=0 >> /etc/modprobe.d/blacklist-nvidia-nouveau.conf"
sudo update-initramfs -u
reboot

 After executing these commands, reboot your system to apply the changes.

Step 2: Install the Nvidia Driver

Download and Install

Install the Nvidia driver from the Ubuntu repository:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-555

Reboot your system to load the new driver.

To verify if the driver installed properly, run the below command, it should display the driver info:

nvidia-smi


Step 3: Install CUDA Toolkit 12.5 and cuDNN 8.5.1

Install CUDA Toolkit

Follow the instruction on Nvidia developer official site for installing cuda tool kit 12.5.

use this command:  sudo apt install nvidia-cuda-toolkit

To verify if the cuda toolkit installed properly, run the below command, it should display the info:

nvcc --version

Install cuDNN

Download cuDNN 9.2.0 from Nvidia Developer (account required).

Use the commands below for Local Installer for Ubuntu22.04 x86_64 (Deb). It will automatically add the latest cuDNN Debian package which in this case is: cudnn9-cuda-12   to your local repository:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.debsudo dpkg -i cuda-keyring_1.1-1_all.debsudo apt-get updatesudo apt-get -y install cudnn

Ensure that cuDNN is correctly installed by checking the installed versions. You can do this by looking at the versions of the cuDNN libraries installed in your system.

ls /usr/lib/x86_64-linux-gnu/libcudnn*


This command will list all the cuDNN related libraries, and you should see files related to cuDNN version 9.2.0 if the installation was successful.

To ensure all tools and libraries recognize and use Cuda, cuDNN, you need to set the LD_LIBRARY_PATH environment variable. This helps the linker find Cuda, cuDNN libraries at runtime.

echo 'export PATH=/usr/local/cuda-12.5/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.5/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
gcc --version
g++ --version

The results are as below for my computer gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Comments

Popular posts from this blog

Privacy policy

English to Runyankole speech translation app

English to Runyankole translation app