TensorFlow is a free and open source platform for machine learning built by Google. This is used by a number of organizations, including Twitter, PayPal, Intel, Lenovo, and Airbus.
TensorFlow can be installed throughout the system, in a virtual Python environment, as a Docker container, or with Anaconda.
This tutorial explains how to install TensorFlow in a Python virtual environment on Ubuntu 20.04.
The virtual environment allows you to have several different isolated Python environments on one computer and install a specific version of the module on a per project basis, without worrying that it will affect your other projects.
Installing TensorFlow on Ubuntu 20.04
Ubuntu 20.04 is shipped with Python 3.8 by default. You can verify that Python 3 is installed on your system by typing:
The output will look like this:
The recommended way to create a virtual environment is to use the venv module, which is included in the python3-venv package.
To install the python3-venv package, run the following command:
sudo apt install python3-venv python3-dev
After the module is installed, you are ready to create a virtual environment for our TensorFlow project.
Navigate to the directory where you want to save your Python 3 virtual environment. This can be your home directory or another directory where your users have read and written permissions.
Create a new directory for the TensorFlow project and enter it:
In the directory, run the following command to create a virtual environment:
python3 -m venv venv
The second Venv is the name of the virtual environment. You can use whatever name you want for the virtual environment.
The above command creates a directory called venv, which contains binary copies of Python, Pip package managers, standard Python libraries, and other supporting files.
To start using the virtual environment, activate it by running the activation script:
Once activated, the virtual environment bin directory will be added at the beginning of the system $ PATH variable. Additionally, the shell prompt will change, and it will display the name of the virtual environment that you are currently using. In this example, i.e. (venv).
Installing TensorFlow requires pip version 19 or higher. Enter the following command to update the 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 utilize the processing power, instead of tensorflow, install the tensorflow-gpu package, which includes GPU support.
In a virtual environment, you can use pip commands instead of pip3 and python, not python3.
There she is! You have successfully installed TensorFlow, and you can start using it.
To verify the installation, run the following command, which will print a version of TensorFlow:
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 version of Tensor Flow may be different from the version shown here.
If you are new to TensorFlow, visit the Getting Started with TensorFlow page and learn how to create your first ML application. You can also clone TensorFlow Models or TensorFlow-Examples repositories from Github and explore and test TensorFlow examples.
When you’re 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 in a virtual environment on Ubuntu 20.04.
If you experience problems or get feedback, leave a comment below.