nvtop showing live NVIDIA GPU utilisation

NVIDIA + CUDA + cuDNN + TensorFlow (Py311) Setup on Fresh Ubuntu

This guide takes a fresh Ubuntu machine with an NVIDIA GPU to a working TensorFlow GPU environment without Docker and without the apt CUDA packages: install the driver, Miniconda, the CUDA 12.1 toolkit from the runfile, cuDNN 8.9.7, then TensorFlow in a Python 3.11 environment, and verify each layer before moving on.

Overview


How the stack fits together

GPU software stack, bottom to top TensorFlow / PyTorch Python framework cuDNN deep learning kernels cuBLAS, cuFFT ... CUDA libraries CUDA runtime + toolkit nvcc, libcudart NVIDIA kernel driver nvidia-smi, max CUDA version GPU hardware e.g. RTX 3060, 12 GB

Each layer depends on the one beneath it, and most installation failures come from a version mismatch between two adjacent layers. The kernel driver is the only component installed system-wide through apt; nvidia-smi reports its version and the newest CUDA version it can run. The CUDA toolkit supplies the runtime and compiler; any toolkit up to that maximum works with the driver. cuDNN provides tuned convolution, normalisation and attention kernels and must match the CUDA major version. The framework is compiled against specific CUDA and cuDNN versions and will fall back to CPU, usually with only a warning, if it cannot load them.

Version compatibility notes

This guide pins driver 535, CUDA 12.1, cuDNN 8.9.7 and TensorFlow 2.16 because that combination was tested together. Before changing any one of them, check TensorFlow's tested build configurations table and NVIDIA's cuDNN support matrix. Two simpler alternatives exist: pip install "tensorflow[and-cuda]" pulls NVIDIA's CUDA and cuDNN libraries as pip wheels into the environment, and PyTorch wheels bundle their own CUDA runtime, so in both cases only the driver must be installed on the host. The manual toolkit install below is still useful when you need nvcc for compiling custom kernels or other libraries. A hardware-specific walkthrough is on the GeForce RTX 3060 installation page.

Step 1: Base System Preparation

Update Ubuntu


sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential wget curl git

Reboot


sudo reboot

Step 2: Install NVIDIA Driver

Detect Recommended Driver


ubuntu-drivers devices

Install NVIDIA Driver (example: 535)


sudo apt install -y nvidia-driver-535
sudo reboot

Verify Driver


nvidia-smi

Step 3: Install Miniconda

Download Miniconda


wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Restart Shell


exec bash

Step 4: Create Python 3.11 Environment

Create and Activate Py311


conda create -n Py311 python=3.11 -y
conda activate Py311

Verify Python


python -V

Step 5: Install CUDA Toolkit 12.1 (Runfile)

Download CUDA 12.1 Runfile


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

Run Installer (Toolkit Only)


sudo sh cuda_12.1.1_530.30.02_linux.run

Persist CUDA Environment


sudo tee /etc/profile.d/cuda-12.1.sh << 'EOF'
export CUDA_HOME=/usr/local/cuda-12.1
export PATH=/usr/local/cuda-12.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64:$LD_LIBRARY_PATH
EOF

source /etc/profile.d/cuda-12.1.sh

Verify CUDA


nvcc --version

Step 6: Install cuDNN 8.9.7 (Correct Version)

Download cuDNN


wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz

Extract and Install


tar -xf cudnn-linux-x86_64-8.9.7.29_cuda12-archive.tar.xz
sudo cp -r cudnn-linux-x86_64-8.9.7.29_cuda12-archive/include/* /usr/local/cuda-12.1/include/
sudo cp -r cudnn-linux-x86_64-8.9.7.29_cuda12-archive/lib/* /usr/local/cuda-12.1/lib64/
sudo ldconfig

Verify cuDNN


ls /usr/local/cuda-12.1/lib64/libcudnn*

Step 7: Install TensorFlow (GPU)

Install TensorFlow


pip install --upgrade pip
pip install tensorflow==2.16.2

Step 8: Verify TensorFlow GPU

GPU Test Script


import tensorflow as tf

gpus = tf.config.list_physical_devices("GPU")
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

print("TensorFlow:", tf.__version__)
print("Built with CUDA:", tf.test.is_built_with_cuda())
print("GPUs:", tf.config.list_physical_devices("GPU"))

Run


python gpu_test.py

Expected Output


Step 9: Sample Training Script (GPU)


import tensorflow as tf
import time

gpus = tf.config.list_physical_devices("GPU")
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

x = tf.random.normal((1_000_000, 10))
y = tf.reduce_sum(x, axis=1, keepdims=True)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(256, activation="relu"),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dense(1),
])

model.compile(optimizer="adam", loss="mse")

with tf.device("/GPU:0"):
    start = time.time()
    model.fit(x, y, epochs=3, batch_size=4096)
    print("Training time:", time.time() - start)

Final State

Troubleshooting

Related pages

NVIDIA GeForce RTX 3060 installationnvidia Stable Diffusion Web UIdocument ingestiona GPU workload to test the setup with Deep learningSection