How to Install TensorFlow on Ubuntu


TensorFlow is an open-source machine learning platform. Google designed the software to help researchers, data scientists, and developers simplify the process of implementing machine learning models.

This end-to-end library for numerical computation can run on multiple CPUs, GPUs, as well as mobile operating systems.

In this tutorial, learn how to install to TensorFlow on Ubuntu 20.04 or 22.04 using two methods.

How To Install TensorFlow On Ubuntu

Note: For CentOS, see our guide on installing TensorFlow on CentOS.


Step 1: Install Required Packages

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

  • Python-dev for installing header files for Python extensions.
  • The pip package manager.
  • Miniconda for creating a separate environment and simpler GPU setup. Skip the Miniconda installation if you already have Anaconda installed.

Follow the steps below to install the required packages:

1. Open the terminal window and start by updating the repository with:

sudo apt update

Wait for the update to complete, and proceed to the next step.

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

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

3. Download the Miniconda shell script using curl:

curl -o
Miniconda curl download terminal output

4. Run the script to start the Miniconda install:

Miniconda installation terminal output

Press Enter to view the license agreement and write yes at the end to accept the terms.

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

Miniconda location confirm terminal output

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

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

8. Verify the installation with:

conda -V
conda -V version 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 running different TensorFlow versions on the same system without conflicts.

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.

2. Create an environment named tensorflow:

conda create --name tensorflow python=3.9
conda create tensorflow environment python3.9

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

3. Activate the environment with:

conda activate tensorflow
Conda activate tensorflow terminal output

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

Step 3: Install TensorFlow

The next steps differ depending on whether you are installing 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 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==package_version

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.

Follow the steps below to install TensorFlow for GPU:

1. Install the CUDA toolkit and cuDNN packages with:

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
conda install cudatoolkit and cudnn terminal output

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

2. Configure the system paths to activate when running the environment. Create a directory:

mkdir -p $CONDA_PREFIX/etc/conda/activate.d

3. Export the paths with:

echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/' > $CONDA_PREFIX/etc/conda/activate.d/

4. Update the pip package manager:

pip install --upgrade pip
pip install upgrade pip terminal output

5. Lastly, install TensorFlow with GPU support with the following command:

pip install tensorflow

The default tensorflow library contains both CPU and GPU support by default.

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.


This article showed you the steps for installing TensorFlow on Ubuntu 20.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.

Was this article helpful?
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
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