TensorFlow is a free open source machine learning platform created 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 using Anaconda.
This article 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 the same machine and install a specific version of the module for each project without worrying about impacting 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 to use the venv module, which is included in the python3-venv package.
Run the following command to install the python3-venv package:
sudo apt install python3-venv python3-dev
After installing the module, you are ready to create a virtual environment for our TensorFlow project.
Change to the directory where you want to store your Python 3 virtual environments. This can be your home directory or any directory where your user has read / write permissions.
Create a new directory for the TensorFlow project and navigate to it:
mkdir my_tensorflowcd my_tensorflow
Inside the directory, run the following command to create a virtual environment:
python3 -m venv venv
The second name of the virtual environment is venv. You can use any name for the virtual environment.
The above command creates a directory called venv which contains a copy of the Python binary, Pip package manager, Python standard library, and other supporting files.
To start using the virtual environment, activate it by running the activate script:
Upon activation, the bin directory of the virtual environment will be added to the beginning of the $ PATH system variable. In addition, the shell prompt will change to reflect the name of the virtual environment you are in. In this example, this is (venv).
Installing TensorFlow requires pip version 19 or higher. Enter the following command to update 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, install the tensorflow-gpu package, which includes GPU support, instead of tensorflow.
In a virtual environment, you can use pip instead of pip3 and python instead of python3.
All! You have successfully installed TensorFlow and you can start using it.
To test the installation, run the following command, which will display the version of TensorFlow:
python -c 'import tensorflow as tf; print(tf.__version__)'
At the time of this writing, the latest stable version of TensorFlow is 2.2.0:
Your version of TensorFlow may differ from the version shown here.
If you are new to TensorFlow, please visit “Getting started with TensorFlow»And learn how to create your first ML app. You can also clone repositories TensorFlow models or TensorFlow- examples from Github, and explore and test TensorFlow examples.
Once you are done with your work, deactivate the environment by typing deactivate and you will be returned to your normal shell.
We showed you how to install TensorFlow in a virtual environment on Ubuntu 20.04.
If you run into an issue or have feedback, please leave a comment below.