Introduction

TensorFlow is an open-source machine-learning platform. Google designed the software to help researchers, data scientists, and developers simplify the process of implementing machine-learning models.

This end-to-end library for numerical computation can run on multiple CPUs, GPU, as well as mobile operating systems. In this tutorial, learn how install to TensorFlow on Ubuntu 18.04.

tutorial on installing tensorflow on ubuntu system


Note: For CentOS, see our guide on installing TensorFlow on CentOS.


Prerequisites

  • An Ubuntu Linux system (16.04 or later)
  • Access to a command line/terminal window (Ctrl+Alt+T)
  • A user account with sudo privileges
  • Pip 19.0 or later

Step 1: Install Required Packages

Before you can install TensorFlow, you’ll need to set up the Python development environment. It includes the following software:

  • Python (version 3.4 or later)
  • the pip package manager (no older than version 19.0)
  • Virtualenv (software for isolating Python environments)

Open the terminal window and start by updating the repository with:

sudo apt update

Next, install the first two packages with the command:

sudo apt install python3-dev python3-pip

screenshot of installing pip and python for tensorflow

Then, run the following command for a wide-system installation of Virtualenv:

sudo pip3 install -U virtualenv

installation of virtualenv on ubuntu 18.04

Step 2: Creating a Virtual Environment

Now that you have Virtualenv on your Ubuntu system, you can use it to create and isolate Python environments.

Create your first enviroment in a new ./venv directory:

virtualenv --system-site-packages -p python3 ./venv

createing a new virtual environment
Then, activate the virtual environment to start working inside it. Run the following command:

source ./venv/bin/activate

Your shell prompt should now have a (venv) prefix as in the image below:

virtual environment acctivated example

Once you activate venv, move on to installing pip inside the isolated environment:

pip install --upgrade pip

installing pip in virtual environment on ubuntu

If you want to see a complete list of all the packages inside the virtual environment, use the command:

pip list

It displays all the packages and their respective versions, as in the following image:

listing packages using pip list

Step 3: Installing TensorFlow

The next step differs depending on whether you are installing TensorFlow for CPU or TensorFlow for GPU. The choice depends on the nature of your workload and hardware options.

Option 1: Install TensorFlow for CPU

The default TensorFlow software package supports CPU-based workloads. To install the package and its dependencies, type the following command:

pip install --upgrade tensorflow

Option 2: Install TensorFlow for GPU

TensorFlow for GPU requires a dedicated NVIDIA CUDA®-enabled GPU and related drivers. This software package is intented for GPU-based machine-learning workloads.

To install the latest stable version of TensorFlow for GPU, run the command:

pip install --upgrade tensorflow-gpu

For a list of hardware and software requirements for TensorFlow for GPU, please refer to TensorFlow’s documentation on GPU support.

Option 3: Installing Old Versions of TensorFlow

Older versions of TensorFlow for CPU and GPU are also available for download.

Version 1.14 and older is installed by running the command in the following format:

pip install tensorflow==package_version

To install TensorFlow for CPU 1.14, run the command:

pip install tensorflow==1.14

To install TensorFlow for GPU 1.14, run the command:

pip install tensorflow-gpu==1.14

Version 1.15 supports both CPU and GPU-based workloads. To install TensorFlow 1.15, type the  command:

pip install tensorflow-gpu==1.15rc2

Step 4: Verifying TensorFlow Installation

To verify the installation of TensorFlow in Ubuntu , enter the command in the terminal window:

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

Conclusion

This article showed you steps for installing TensorFlow on Ubuntu 18.04. With the desired version (CPU or GPU support) installed on your system, you can now move on to developing your machine-learning models.


Next you should also read