TensorFlow 2 install in Ubuntu 18 with pip

This article describes how to install TensorFlow 2.0 in Ubuntu 18 using PIP, the Python package management tool. TensorFlow is a free open-source machine learning library for Python created by Google. Using TensorFlow you can easily train models, test predictions, and create your own machine learning applications.

Ubuntu 18 comes pre-installed with Python 3. This article will describe how to install TensorFlow 2.0 for Python 3 using PIP. The matplotlib is another useful Python package which you can use to visualise results from machine learning passes, so we’ll describe how to install this as well.

Install PIP3

Although Ubuntu 18 comes with Python 3, it does not have PIP installed by default.

  1. You can check if PIP is installed in your system by opening a terminal and entering the following command:

    pip3 --version
    

    If PIP is installed, you will see the version details printed, for example:

    pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

  2. If PIP is not installed, you can install it with these commands:

    sudo apt update
    sudo apt install python3-pip
    

    Then run pip3 --version again to confirm PIP is installed.

Install TensorFlow 2.0

There are 2 different versions of TensorFlow 2.0, the first is a CPU-only build and the other build includes GPU support.

Note: If you are running Ubuntu in VirtualBox, you will need to install the CPU-only build.

  1. To install the CPU-only build:

    pip3 install tensorflow==2.0.0-alpha0
    

    This make take a few minutes depending on your Internet connection.

  2. You can verify the version of TensorFlow that was installed with the following Python command:

    python3 -c "import tensorflow as tf; print(tf.__version__)"
    

    You should see the version details printed out, for example:

    2.0.0-alpha0

Install matplotlib

The matplotlib Python package is useful to visualise TensorFlow results from machine learning passes.

  1. First, install the Tkinter module in Ubuntu (which is required by matplotlib):

    sudo apt-get install python3-tk
    
  2. You can verify Tkinter is working with the following Python command:

    python3 -c "import tkinter; tkinter._test()"
    

    You should see a window generated, which verifies it is working. Close Quit.

    tkinter test window

  3. Now, install matplotlib:

    pip3 install matplotlib
    
  4. You can verify matplotlib is working correctly by generating a simple graph:

    python3 -c "import matplotlib.pyplot as p; p.plot(range(20), range(20)); p.show()"
    

    You should see a new window with a basic graph plotted:

    matplotlib graph

Congratulations, that concludes the install of TensorFlow 2.0 in Ubuntu 18 running on VirtualBox.