How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered applications and deep neural networks.

TensorFlow installation can be carried out system-wide, as a docker container, in a Python virtual environment, or with Anaconda. Creating a virtual environment and installing Tensorflow is an efficient way to get the job done. The virtual environment allows the developers to work on multiple Python project environments on a single machine, and they can also install specific application versions inside an isolated virtual project environment without interrupting the other python projects. This approach may help in resolving version dependency-related problems.

In this tutorial, we learn how to install TensorFlow on Ubuntu 20.04 in a Python virtual environment.

Prerequisites

  • A Ubuntu 20.04 server
  • Minumum 4GB RAM
  • Python 3.8 or higher
  • A user with sudo priviledge

Step 1: Verify the Python installation

Installing Python is a basic requirement for the TensorFlow library. Python 3.8 is already installed on Ubuntu.  Execute the following command to display the installed python version that validates either Python is installed on your Ubuntu system or not:

$ python3 -V

The following output shows on the terminal in which you can see the installed python version:

python3 version

Step 2: Install Python Venv modules

It is recommended to use the venv module for the creation of a virtual environment, which is already incorporated in the Python3-venv package. The venv module can be installed using the  following command:

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

Step 3: Create directory for TensorFlow project

Once python3-venv packages are installed on your Ubuntu system, you will create the new python virtual environment for the TensorFlow project. The virtual environment must be within the home directory of your Ubuntu distribution.

If you wish a virtual environment created inside a new directory then, execute the following ‘mkdir’ command to create a new directory called tensorflow_project:

$ mkdir tensorflow_project

Using the following command you will navigate into the new directory that we have just created in the preceding section:

$ cd tensorflow_project
create tensorflow project directory

Step 4: Create Python virtual environment (venv) and Activate venv

Your system is now prepared for creating a virtual environment called virtual_env (venv). Using the following command, you can create a new python virtual environment inside a directory:

$ python3 -m venv venv

You can name the virtual environment as you like. The preceding command creates the virtual environment named venv that contains all python libraries, pip packages manager, and a copy of the necessary python binary. To use a virtual environment, you need to activate the venv as:

$ source venv/bin/activate
create and activate python virtual environment

Step 5: Upgrade python pip package manager

PIP is a Python package manager used for the installation and maintenance of packages that can be installed via pip. The PIP version 19 or higher is a pre-requisite for the installation of TensorFlow. By using the following command, you can upgrade PIP to the  pip version 19 or higher:

(venv) $ pip install --upgrade pip
upgrade pip

Step 6: Install TensorFlow using Pip package manager

In this step, we will move towards the installation of TensorFlow through pip. Execute the following command to install TensorFlow library using pip package manager:

(venv) $ pip install --upgrade tensorflow

This will install and upgrade TensorFlow to the newest.

install and upgrade tensorflow

To install a specific version of TensorFlow, type:

$ pip install tensorflow==2.6.0

Step 7: Verify TensorFlow Installation

Once the TensorFlow library is installed successfully on your Ubuntu system, the following command will be used for verification of the installation process:

(venv) $ python -c 'import tensorflow as tensor_flow; print(tensor_flow.__version__)'

The installed TensorFlow version displays on the terminal, here, we installed 2.7.0 tensorflow version on Ubuntu system.

Step 8: Deactivate virtual environment

After using a particular virtual environment, you can deactivate it by running the following command that will move you back to the normal shell environment:

(venv) $ deactivate

Conclusion

We provided the step-by-step guidelines for the installation of Tensorflow on the Ubuntu 20.04 system. We discussed in this article how to create a new virtual environment and activate it. We performed TensorFlow installation inside the virtual environment. In the end, we explained how we can deactivate the python virtual environment by using a just single command.

Leave a Comment