This guide provides a step-by-step process to set up TensorFlow with NVIDIA GPU acceleration on Ubuntu-based systems. It assumes that CUDA 11.x and compatible NVIDIA drivers are already installed on your system. By following this guide, you’ll configure TensorFlow with CUDA and cuDNN libraries, enabling efficient deep learning workflows.
Ensure you have NVIDIA drivers and CUDA packages installed (version 11.x). Use the following commands to verify your setup:
To check the installed CUDA version, run:
nvcc --version
Sample output:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Sep_21_10:33:58_PDT_2022
Cuda compilation tools, release 11.8, V11.8.89
Build cuda_11.8.r11.8/compiler.
To view your NVIDIA driver version, GPU name, and total memory:
nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv
Sample output:
name, driver_version, memory.total [MiB]
NVIDIA GeForce GTX 1650, 470.256.02, 3889 MiB
NVIDIA TITAN V, 470.256.02, 12066 MiB
The NVIDIA CUDA Deep Neural Network (cuDNN) library provides GPU-accelerated primitives for deep learning. For CUDA 11.x, the compatible cuDNN version is v8.9.7.
- Visit the NVIDIA cuDNN archive.
- Download the appropriate package for your operating system.
If unsure about your OS details, install and use neofetch
:
sudo apt install neofetch
neofetch --stdout | grep OS:
Sample output:
OS: Ubuntu 22.04.5 LTS x86_64
Note: If you are using Kubuntu, the underlying system is the same as Ubuntu, and the version numbers correspond (e.g., Kubuntu 22.04 is equivalent to Ubuntu 22.04).
Register the downloaded cuDNN repository with dpkg
:
sudo dpkg -i cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb
Refresh your package manager to include the cuDNN repository:
sudo apt update
-
Install the Runtime Library (required for running applications):
sudo apt install libcudnn8=8.9.7.29-1+cuda11.8
-
Install the Developer Library (required for building applications):
sudo apt install libcudnn8-dev=8.9.7.29-1+cuda11.8
If you don’t have Anaconda installed, download and install Anaconda.
Set up a new isolated environment for TensorFlow:
conda create --name tensorflow_gpu python=3.11
Activate the environment to install TensorFlow:
conda activate tensorflow_gpu
Install a version of TensorFlow compatible with your CUDA and cuDNN setup. For CUDA 11.x and cuDNN 8.9.x, install TensorFlow 2.14.1:
pip install tensorflow==2.14.1
Note: TensorFlow recommends using
pip
for installation instead ofconda
. For more details, refer to the official TensorFlow installation guide.
To confirm that TensorFlow is configured to use your GPU, create a new Python file and run the following script. Alternatively, you can download the Hello_GPU.py file from this page/project.
import tensorflow as tf
from tensorflow.python.client import device_lib
# Display TensorFlow version
print("TensorFlow Version:", tf.__version__)
# Check available devices
print("\nAvailable devices:")
for device in device_lib.list_local_devices():
print(f"- {device.name} ({device.device_type})")
# Check GPU details
gpu_devices = tf.config.list_physical_devices('GPU')
if gpu_devices:
print(f"\nNum GPUs Available: {len(gpu_devices)}")
for i, gpu in enumerate(gpu_devices):
print(f" - GPU {i}: {gpu.name}")
else:
print("\nNo GPU detected. TensorFlow is running on CPU.")
# Check CUDA and cuDNN build
print("\nBuild Information:")
print(f"CUDA Enabled: {tf.test.is_built_with_cuda()}")
print(f"cuDNN Enabled: {tf.test.is_built_with_gpu_support()}")
# Dummy computation on each GPU
if len(gpu_devices) >= 2:
print("\nPerforming dummy computations on GPUs...")
# Define small tensors
tensor_a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
tensor_b = tf.constant([[5, 6], [7, 8]], dtype=tf.float32)
# Load tensors to GPU 0
with tf.device('/GPU:0'):
gpu0_result = tf.add(tensor_a, tensor_b)
print(f"\nTensor addition on GPU 0:\n{gpu0_result.numpy()}")
# Load tensors to GPU 1
with tf.device('/GPU:1'):
gpu1_result = tf.multiply(tensor_a, tensor_b)
print(f"\nTensor multiplication on GPU 1:\n{gpu1_result.numpy()}")
else:
print("\nAt least two GPUs are required to perform dummy computations.")
This script outputs:
- TensorFlow version.
- Available devices (CPU and GPU).
- Number and details of GPUs detected.
- Build information for CUDA and cuDNN support.