Debian

How to Install TensorFlow with NVIDIA CUDA/cuDNN Acceleration on Debian 12

TensorFlow is a Python library for artificial intelligence and machine learning. TensorFlow can be used to train and create new AI models, import existing AI models, load the test data and check the performance of AI models, save the trained AI models, and so on.

TensorFlow can use CPU and GPU to compute the complex Artificial Intelligence (AI) and Machine Learning (ML) calculations. TensorFlow can use any CUDA-supported NVIDIA GPU to accelerate the AI/ML programs. If you don’t have a CUDA-supported GPU, TensorFlow will use the CPU for AI/ML codes. Without GPU acceleration, the performance of TensorFlow will degrade in complex AI/ML programs.

In this article, we will show you how to install TensorFlow with NVIDIA CUDA/cuDNN acceleration on Debian 12 “Bookworm”.

Topic of Contents:

  1. Checking If You Have NVIDIA GPU Installed on Your Computer
  2. Installing Python 3 PIP and Python Venv on Debian 12
  3. Creating a Python 3 Virtual Environment for TensorFlow
  4. Upgrading Python 3 PIP on the Python 3 Virtual Environment
  5. Installing TensorFlow with NVIDIA CUDA Acceleration Support
  6. Installing TensorRT on Debian 12
  7. Activating TensorFlow Python 3 Virtual Environment
  8. Accessing TensorFlow and Checking If NVIDIA GPU/CUDA Acceleration Is Available
  9. Conclusion

Checking If You Have NVIDIA GPU Installed on Your Computer

For TensorFlow to accelerate the AI programs with NVIDIA GPU/CUDA, you must have the NVIDIA GPU drivers and NVIDIA CUDA and cuDNN installed on your Debian 12 operating system.

If you need any assistance in installing the NVIDIA GPU drivers on your Debian 12 operating system, read this article.

If you need any assistance in installing the NVIDIA CUDA and cuDNN drivers on your Debian 12 operating system, read this article.

Once you have the NVIDIA GPU drivers installed on your Debian 12 system, the “nvidia-smi” command should be available.

A screenshot of a computer Description automatically generated

The NVIDIA kernel modules should also be loaded on your Debian 12 system.

A screenshot of a computer Description automatically generated

Once you have the NVIDIA CUDA drivers installed, you should have the “nvcc” command available on your Debian 12 system.

A screenshot of a computer Description automatically generated

Installing Python 3 PIP and Python Venv on Debian 12

To install TensorFlow on Debian 12, you need to have the Python 3 PIP and Python virtual environment (venv) module installed.

First, update the APT package repository cache with the following command:

$ sudo apt update

A screenshot of a computer program Description automatically generated

To install Python 3 PIP and Python 3 virtual environment (venv), run the following command:

$ sudo apt install python3-pip python3-venv python3-dev

To confirm the installation, press “Y” and then press <Enter>.

A screenshot of a computer Description automatically generated

Python 3 PIP and Python 3 venv are being installed. It takes a while to complete.

A screenshot of a computer Description automatically generated

At this point, Python 3 PIP and Python 3 venv should be installed.

A screenshot of a computer program Description automatically generated

Creating a Python 3 Virtual Environment for TensorFlow

The standard practice for installing the Python libraries on Debian 12 is installing them in a Python virtual environment so that they don’t interfere with the system’s Python packages/libraries.

To create a new Python 3 virtual environment for TensorFlow in the “/opt/tensorflow” directory, run the following command:

$ sudo python3 -m venv /opt/tensorflow

Upgrading Python 3 PIP on the Python 3 Virtual Environment

To upgrade Python 3 PIP to the latest version on the Python 3 virtual environment “/opt/tensorflow”, run the following command:

$ sudo /opt/tensorflow/bin/pip install --upgrade pip

A screenshot of a computer Description automatically generated

Installing TensorFlow with NVIDIA CUDA Acceleration Support

To install TensorFlow with NVIDIA CUDA acceleration support on the Python “/opt/tensorflow” virtual environment, run the following command:

$ sudo /opt/tensorflow/bin/pip install tensorflow[and-cuda]

TensorFlow with NVIDIA CUDA acceleration is being installed. It takes a while to complete.

A screenshot of a computer Description automatically generated

At this point, TensorFlow with NVIDIA CUDA acceleration support should be installed.

A screenshot of a computer Description automatically generated

Installing TensorRT on Debian 12

NVIDIA TensorRT optimizes the performance of TensorFlow deep learning even more. You can install TensorRT on the TensorFlow Python “/opt/tensorflow” virtual environment with the following command:

$ sudo /opt/tensorflow/bin/pip install tensorrt

NVIDIA TensorRT is being installed on the Python virtual environment. It takes a while to complete.

A screenshot of a computer Description automatically generated

At this point, NVIDIA TensorRT should be installed.

A screenshot of a computer Description automatically generated

Activating TensorFlow Python 3 Virtual Environment

To activate the TensorFlow Python “/opt/tensorflow” virtual environment, run the following command:

$ . /opt/tensorflow/bin/activate

The TensorFlow Python 3 virtual environment should be activated.

A screenshot of a computer Description automatically generated

Accessing TensorFlow and Checking If NVIDIA GPU/CUDA Acceleration Is Available

To open the Python 3 interactive shell, run the following command:

$ python3

Python 3 interactive shell should be opened.

A screenshot of a computer Description automatically generated

First, import TensorFlow with the following line of code:

$ import tensorflow as tf

Once TensorFlow is imported, you can check the version number of TensorFlow that you installed with the following line of code. As you can see, we have TensorFlow 2.13.1 installed on our Debian 12 system.

$ tf.__version__

To verify that TensorFlow can use the NVIDIA GPU that you installed on your computer for CUDA acceleration, run the following line of code. As you can see, our NVIDIA GPU is accessible from TensorFlow.

$ print(tf.config.list_physical_devices('GPU'))

A screenshot of a computer Description automatically generated

To exit out of the Python interactive shell, run the following line of code:

$ quit()

Conclusion

In this article, we showed you how to install the Python 3 PIP and Python 3 virtual environment (venv) on Debian 12. We also showed you how to create a Python 3 virtual environment for TensorFlow on Debian 12 and how to install TensorFlow with NVIDIA GPU/CUDA acceleration support and NVIDIA TensorRT on Debian 12 as well. Finally, we showed you how to activate the TensorFlow Python virtual environment and access TensorFlow on Debian 12.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.