Now Reading: How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04 – Promptchan AI

Loading
svg
Open

How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04 – Promptchan AI

January 9, 20248 min read

Deep learning is a cutting-edge technology that enables machines to learn and improve on their own. However, setting up a deep learning environment on your Ubuntu 22.04 system can be a daunting task for those who are new to the technology.

In this article, we will walk you through the process of setting up deep learning architecture on Ubuntu 22.04, including key configurations like Nvidia driver, Cuda, cuDNN, Anaconda setups to ensure a successful installation.

System Requirements

This setup is tested on a virtual machine provisioned on Google Cloud Compute Engine with the below configurations.

  1. GPU: Tesla T4
  2. CPU: N1-Standard2 (2vCPU 7.5GB RAM)
  3. OS: Ubuntu 22.04 (x86/64)
  4. Disk space: 50 GB
  5. Secure Boot: Disabled.

The approx. cost for this machine on US-Central would cost around $250/mo.

Update the System

Before starting with the installation process, it is recommended to update the system with the latest patches and software updates. Run the following command to update your Ubuntu 22.04 system:

sudo apt update
sudo apt upgrade

Verify GPU

Verify if you have the GPU using the below command.

lspci | grep -i nvidia

If you have your GPU you can proceed with the following installation.

Output
00:04.0 3D Controller: NVIDIA Corporation TU104GL [Tesla T4] (rev a1)

In this guide I am having Nvidia Tesla T4 GPU.

Install Pre-required Packages

Before installing Nvidia driver you need to make sure you have all pre-required packages installed.

sudo apt install build-essential

You can use the following command to install kernel headers for your operating system.

sudo apt install linux-headers-$(uname -r)

Now you can proceed with the driver installation

Install NVIDIA Drivers

One of the most important steps to setting up deep learning on Ubuntu 22.04 is to install the appropriate NVIDIA drivers for your graphics card. To do this, open a terminal and run the following commands:

sudo apt install nvidia-driver-530

Now you need to reboot.

sudo reboot

Next, you will need to install the CUDA Toolkit and cuDNN, which are essential for running deep learning frameworks such as TensorFlow and PyTorch. You can download the latest version of the CUDA Toolkit from the NVIDIA website, and cuDNN from the cuDNN website. Once you have downloaded the appropriate files, you can install them by running the following commands:

Install CUDA

wget https://developer.download.nvidia.com/compute/cuda/12.1.1/local_installers/cuda_12.1.1_530.30.02_linux.run
sudo sh cuda_12.1.1_530.30.02_linux.run

Follow the on screen instructions.

  1. Accept the license agreement by typing accept
  2. Unselect the driver and then choose Install by using the arrow keys and space bar to move and select or unselect. You should not have X mark in the driver
  3. Move the arrow key down to Install and click Enter.
Cuda Installation On Ubuntu
How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04 1

Wait for sometime for the installation to complete.

Now CUDA will get installed in /usr/local/cuda-12.1 location.

Symlink the directory.

sudo ln -snf /usr/local/cuda-12.1 /usr/local/cuda

Install CUDNN

To install cuDNN, you need to login to Nvidia website and download the tar.xz version using this official link.

Once downloaded extract the downloaded file and copy the necessary contents to the cuda directory.

tar -xf cudnn-linux-x86_64-8.9.0.131_cuda12-archive.tar.xz

cd cudnn-linux-x86_64-8.9.0.131_cuda12-archive

sudo cp include/cudnn.h /usr/local/cuda-12.1/include
sudo cp lib/libcudnn* /usr/local/cuda-12.1/lib64

sudo chmod a+r /usr/local/cuda-12.1/include/cudnn.h /usr/local/cuda-12.1/lib64/libcudnn*

Now you have CUDA and CUDNN installed.

Once the installation is complete, update the environment variables, and add the following lines to ~/.bashrc

export CUDA_HOME=/usr/local/cuda-12.1
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64
export PATH=${CUDA_HOME}/bin:${PATH}

Activate the environment variables:

source ~/.bashrc

You can check cuda version using the below command.

nvcc -V
Cuda Version
How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04 2

You can also check Nvidia driver installation status using the following command.

nvidia-smi

You will get an output similar to the below one.

Nvidia Driver Installation
How to Set Up Deep Learning with Nvidia, CUDA, cuDNN on Ubuntu 22.04 3

Install Anaconda

Anaconda is a popular Python distribution that comes with many pre-installed libraries and tools used in deep learning. To install Anaconda, run the following commands:

You can update the below link using the latest version from the official anaconda website.

curl https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh --output anaconda.sh
bash anaconda.sh

Follow the on screen instructions.

In the final step you can safely answer Yes to initialize Anaconda3.

Create a New Conda Environment

A Conda environment is a virtual environment that allows you to install and manage different versions of packages and libraries. To create a new Conda environment, run the following command:

conda create --name deep-learning

Activate the Conda Environment

After creating a new Conda environment, you need to activate it by running the following command:

conda activate deep-learning

Install Deep Learning Frameworks

Now that you have installed the necessary drivers and tools, you can install the deep learning frameworks of your choice, such as TensorFlow or PyTorch. You can install these frameworks using pip, the Python package manager, as shown below:

pip3 install tensorflow
pip3 install keras
pip install torch

Verify Your Installation

To test the installation, you can run a sample script that uses TensorFlow, Keras, and PyTorch. Create a new Python file and paste the following code:

sudo nano learning.py

Paste the below code and save the file.

import tensorflow as tf
import keras
import torch
print("TensorFlow version:", tf.version)
print("Keras version:", keras.version)
print("PyTorch version:", torch.version)

Execute the file using the following command:

python learning.py

If everything is installed correctly, you should see the versions of TensorFlow, Keras, and PyTorch printed on the screen.

Wrap Up!

In conclusion, setting up a deep learning environment in Ubuntu 22.04 can be a challenging task, but by following the steps outlined above, you can ensure a smooth and successful installation. By installing the necessary drivers and tools, and then installing your preferred deep learning frameworks, you will be well on your way to developing and running your own deep learning models.

svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg