How to Install TensorFlow on Ubuntu

Introduction

TensorFlow is an open-source machine learning library designed by Google to simplify AI development for researchers, data scientists, and developers.

This end-to-end library for numerical computation can run operations on multiple CPUs or GPUs. It is also available for various OSes, including Ubuntu.

This guide shows how to install TensorFlow for CPU or GPU on Ubuntu.

How To Install TensorFlow On Ubuntu

Prerequisites

Step 1: Install Python Development Packages

Before installing TensorFlow, set up the Python development environment. It includes the following:

  • Python-dev. Provides header files for building Python extensions.
  • pip. The Python package manager for installing TensorFlow and other Python packages.
  • Miniconda. A minimal installer for Conda. It helps create and manage environments and simplifies GPU setup. Skip the Miniconda installation if you already have Anaconda installed or prefer using the built-in venv or virtualenv.

Follow the steps below to install the required packages:

1. Open the terminal window and update the package list:

sudo apt update

Wait for apt to finish the update.

2. Install python3-dev and python3-pip with the following command:

sudo apt install python3-dev python3-pip
apt install python3-dev and python3-pip terminal output

3. Download the Miniconda shell script using the wget command:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget miniconda install script download terminal output

4. Run the script to start the Miniconda install:

bash Miniconda3-latest-Linux-x86_64.sh
Miniconda installation terminal output

Press Enter to continue and view the license agreement.

5. Navigate to the end of the license terms and write yes at the end to accept.

License terms agreement Miniconda accepted

6. Choose the installation location. Press Enter to accept the suggested location.

Miniconda location confirm terminal output

7. Lastly, when asked to initialize Miniconda3, type yes.

8. Restart the terminal. The terminal starts in an isolated environment and shows a (base) prefix.

9. Verify the installation with:

conda -V
conda -V 24.7.1 terminal output

The conda version prints to the output.

Step 2: Create a Conda Environment

Use conda to create a separate Python environment for installing TensorFlow. Isolated environments allow different TensorFlow versions to be used on the same system without conflicts. Follow these steps:

1. Update conda to the latest version:

conda update -n base -c defaults conda
conda update terminal output

When asked to proceed, press y and wait for the updates to complete. Otherwise, the output shows that the latest conda version has been installed already.

2. Create an environment named tf2:

conda create --name tf2 python=3.12
conda create environment tf2 python 3.12

Press y to confirm the environment setup.

The command creates an isolated environment with Python 3.12. If you use a different Python version, change the number to the version on your system. See how you can check the Python version.

Step 3: Activate Conda Environment

Activate the environment with:

conda activate tf2
conda activate tf2 terminal shell switch

The shell prefix changes to (tf2). Keep the environment active for the rest of the installation process.

Step 3: Install TensorFlow

The following steps differ depending on whether you install TensorFlow for CPU or GPU. The choice depends on the workload requirements and available resources.

Option 1: Install TensorFlow For CPU

The tensorflow-cpu software package is simple to set up for beginners and supports CPU-only workloads. To install the package, type the following command:

pip install tensorflow-cpu

The installation downloads and sets up all the required dependencies.

Note: Specify the TensorFlow version to install an older build, for example: pip install tensorflow-cpu==[version].

Alternatively, install the third-party tensorflow package:

pip install tensorflow

The package is built, maintained, tested, and released by AWS.

Option 2: Install TensorFlow For GPU

If using TensorFlow for GPU-based machine learning workloads, the setup requires an NVIDIA CUDA-enabled GPU with the correct Nvidia driver installed (version >= 525.60.13).

Follow the steps below to install TensorFlow for GPU:

1. Update the pip package manager:

pip install --upgrade pip
pip install --upgrade pip already satisfied terminal output

The pip version should be 19.0 or higher.

2. Install TensorFlow with GPU support with the following command:

pip install tensorflow[and-cuda]

The command also installs the CUDA toolkit and the cuDNN package. The CUDA toolkit enables GPU-accelerated development, while the cuDNN package provides GPU acceleration for deep neural networks.

Step 4: Verify TensorFlow Installation

To verify the TensorFlow installation in Ubuntu, enter the following command in a terminal window:

python -c "import tensorflow as tf; print(tf.random.normal([10,10]))"
TensorFlow Python Tensor sample test

The output prints a Tensor with random values, indicating the installation worked.

To list available devices, run:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices())"

The command lists all available CPUs and GPUs.

Conclusion

This guide showed how to install TensorFlow on Ubuntu 24.04. With the desired TensorFlow version (CPU and GPU support) installed on your system, you can now move on to developing your machine learning models.

Next, see our list of machine learning projects for all levels for project ideas.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Install Keras With TensorFlow Backend on Linux
August 24, 2020

Follow the step by step instructions to learn how to prepare your system for the installation of Keras and...
Read more
How to Install and Use TensorFlow on CentOS 7
October 8, 2019

TensorFlow is Google's open-source platform for machine learning. It was designed to simplify the process of...
Read more
What is Server Virtualization? Definition and How it Works
February 24, 2019

A virtualized server allows one piece of hardware to be used as multiple virtual servers. Learn about Server...
Read more
How to Install NVIDIA Tesla Drivers on Linux or Windows
September 7, 2018

Growing demands for extreme computing power have lead to the unavoidable presence of bare metal servers...
Read more