How to Install TensorFlow on Ubuntu 24.04

TensorFlow is a powerful open-source machine learning framework developed by Google. It allows me to build and train deep learning models efficiently. In this guide, I will walk through the steps to install TensorFlow on Ubuntu 24.04, including setting up a virtual environment and enabling GPU support if needed.

How to Install TensorFlow on Ubuntu 24.04

 

Step 1: Update Your System

Before installing TensorFlow, I need to update my system packages to ensure compatibility.

sudo apt update && sudo apt upgrade -y

This ensures that all installed packages are up to date.

 

Step 2: Install Python and pip

TensorFlow requires Python 3.6 or later. Ubuntu 24.04 comes with Python 3, but I need to make sure it's installed along with pip (Python's package manager).

sudo apt install python3 python3-pip -y

To verify the installation, I can check the Python version:

python3 --version

 

Step 3: Create a Virtual Environment

Using a virtual environment helps me manage dependencies and avoid conflicts with system-wide packages.

  1. Install the virtual environment package:

    sudo apt install python3-venv -y
  2. Create a virtual environment named tensorflow_env:

    python3 -m venv tensorflow_env
  3. Activate the virtual environment:

    source tensorflow_env/bin/activate

Once activated, the terminal prompt will change, indicating that I'm working within the virtual environment.

 

Step 4: Upgrade the pip

To prevent issues during installation, I should upgrade the pip to the latest version:

pip install --upgrade pip

 

Step 5: Install TensorFlow

With the virtual environment active, I can now install TensorFlow using pip:

pip install tensorflow

To verify the installation, I can run the following command:

python -c "import tensorflow as tf; print(tf.__version__)"

If TensorFlow is installed correctly, this command will display the installed version number.

 

Step 6: Install TensorFlow with GPU Support (Optional)

I need to install additional software if I have an NVIDIA GPU and want to accelerate TensorFlow computations.

1. Install NVIDIA Drivers and CUDA Toolkit

  1. Check my GPU model:

    lspci | grep -i nvidia
  2. Install NVIDIA drivers:

    sudo apt install nvidia-driver-535 -y
  3. Install CUDA Toolkit:

    sudo apt install nvidia-cuda-toolkit -y

2. Install cuDNN

Download and install the cuDNN library from the NVIDIA website and follow the installation instructions.

3. Install TensorFlow-GPU

Once my system is set up with CUDA and cuDNN, I can install the GPU-enabled version of TensorFlow:

pip install tensorflow-gpu

To verify GPU support, I can run:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If TensorFlow detects my GPU, it will be listed in the output.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS