Read in 3 minutes

last updated 

How to Install TensorFlow on Ubuntu 18.04

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 or as a Docker container using pip. For learning purposes, it is best to install TensorFlow in a Python virtual environment. This way you can 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.

This tutorial describes how to install TensorFlow on Ubuntu 18.04.

Installing TensorFlow on Ubuntu 18.04

The following sections provide a step by step instructions about how to install TensorFlow in a Python virtual environment on Ubuntu 18.04.

Installing Python 3 and venv

Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by typing:

python3 -V

The output should look like this:

Python 3.6.6

Starting from Python 3.6, the recommended way to create a virtual environment is to use the venv module. To install the python3-venv package that provides the venv module run the following command:

sudo apt install python3-venv

Once the module is installed we are ready to create a virtual environment for our TensorFlow project.

Creating a Virtual Environment

Start by navigating 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.

Create a new directory for the TensorFlow project and navigate into it:

mkdir my_tensorflow
cd my_tensorflow

Once inside the directory, run the following command to create the virtual environment:

python3 -m venv venv

The command above creates a directory called venv, which contains a copy of the Python binary, the Pip package manager, the standard Python library and other supporting files. You can use any name you want for the virtual environment.

To start using this virtual environment, you need to activate it by running the activate script:

source venv/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the $PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In this case that is venv.

Upgrade pip to the latest version to avoid issues when installing packages:

pip install --upgrade pip

Installing TensorFlow

Now that the virtual environment is activated, it’s time to install the TensorFlow package.

pip install --upgrade tensorflow

If you have a dedicated 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 pip3 and python instead of python3.

To verify the installation use 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 1.13.1

1.13.1

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.

deactivate

Conclusion

In this tutorial, we have shown you how to install TensorFlow Ubuntu 18.04.

If you hit a problem or have feedback, leave a comment below.