How to Install TensorFlow on Ubuntu 20.04
3 min read
TensorFlow is a free and open-source platform for machine learning built by Google. It is used by a number of organizations, including Twitter, PayPal, Intel, Lenovo, and Airbus.
TensorFlow can be installed system-wide, in a Python virtual environment, as a Docker container, or with Anaconda.
This tutorial explains how to install TensorFlow in a Python virtual environment on Ubuntu 20.04.
A virtual environment allows you to have multiple different isolated Python environments on a single computer and install a specific version of a module on a per-project basis, without worrying that it will affect your other Projects.
Installing TensorFlow on Ubuntu 20.04
Ubuntu 20.04 ships with Python 3.8 by default. You can verify that Python 3 is installed on your system by typing:
The output should look like this:
The recommended way to create a virtual environment is by using the
venv module, which is included in the
To install the
python3-venv package run the following command:
sudo apt install python3-venv python3-dev
Once the module is installed, you are ready to create a virtual environment for our TensorFlow project.
Navigate to the directory where you would like to store your Python 3 virtual environments. It can be your home directory or any other directory where your user has read and write permissions.
Within the directory, run the following command to create the virtual environment:
python3 -m venv venv
venvis the name of the virtual environment. You can use any name you want for the virtual environment.
The command above creates a directory named
venv, that contains a copy of the Python binary, the Pip package manager , the standard Python library, and other supporting files.
To start using the virtual environment, activate it by running the
Once activated, the virtual environment’s bin directory will be added at the beginning of the system
$PATH variable . Also, the shell’s prompt will change, and it will show the name of the virtual environment you’re currently in. In this example, that is
TensorFlow installation requires
pip version 19 or higher. Enter the following command to upgrade
pip to the latest version:
pip install --upgrade pip
Now that the virtual environment is activated, it’s time to install the TensorFlow package.
pip install --upgrade tensorflow
If you have a dedicated NVIDIA GPU and want to take advantage of its processing power, instead of
tensorflow, install the
tensorflow-gpu package, which includes GPU support.
Within the virtual environment, you can use the command
pip instead of
python instead of
That’s it! You have successfully installed TensorFlow, and you can start using it.
To verify the installation, run the following command, which will print the TensorFlow version:
python -c 'import tensorflow as tf; print(tf.__version__)'
At the time of writing this article, the latest stable version of TensorFlow is 2.2.0:
Your TensorFlow version may differ from the version shown here.
If you are new to TensorFlow, visit the Get Started with TensorFlow page and learn how to build your first ML application. You can also clone the TensorFlow Models or TensorFlow-Examples repositories from Github and explore and test the TensorFlow examples.
Once you are done with your work, deactivate the environment by typing
deactivate, and you will return to your normal shell.
We have shown you how to install TensorFlow inside a virtual environment on Ubuntu 20.04.
If you hit a problem or have feedback, leave a comment below.