Skip to content

This guide is for users who need TensorFlow for advanced tasks like deep learning and want to focus on development rather than dealing with complex installations. It provides clear steps to set up TensorFlow with NVIDIA GPU acceleration on Ubuntu-based systems, ensuring a smooth start to your projects.

Notifications You must be signed in to change notification settings

williamnyren/TensorFlow-GPU-installation-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

TensorFlow-GPU Installation Guide

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.


Prerequisites: NVIDIA Driver and CUDA Setup

Ensure you have NVIDIA drivers and CUDA packages installed (version 11.x). Use the following commands to verify your setup:

Check CUDA Version

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.

Check NVIDIA Driver and GPU Information

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

Install cuDNN Library

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.

Step 1: Download cuDNN

  1. Visit the NVIDIA cuDNN archive.
  2. Download the appropriate package for your operating system.

Step 1a: Verify Your OS

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).


Step 2: Add cuDNN to Package Manager

Register the downloaded cuDNN repository with dpkg:

sudo dpkg -i cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb

Step 3: Update the Package List

Refresh your package manager to include the cuDNN repository:

sudo apt update

Step 4: Install cuDNN Libraries

  1. Install the Runtime Library (required for running applications):

    sudo apt install libcudnn8=8.9.7.29-1+cuda11.8
  2. Install the Developer Library (required for building applications):

    sudo apt install libcudnn8-dev=8.9.7.29-1+cuda11.8

Python Environment Setup

Step 1: Install Anaconda

If you don’t have Anaconda installed, download and install Anaconda.

Step 2: Create a Conda Environment

Set up a new isolated environment for TensorFlow:

conda create --name tensorflow_gpu python=3.11

Step 3: Activate the Environment

Activate the environment to install TensorFlow:

conda activate tensorflow_gpu

TensorFlow Installation

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 of conda. For more details, refer to the official TensorFlow installation guide.


Verifying Installation

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.

About

This guide is for users who need TensorFlow for advanced tasks like deep learning and want to focus on development rather than dealing with complex installations. It provides clear steps to set up TensorFlow with NVIDIA GPU acceleration on Ubuntu-based systems, ensuring a smooth start to your projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages