How To Install TensorFlow on Ubuntu 20.04
Not using Ubuntu 20.04?
Choose a different version or distribution.
An open-source machine learning software library, TensorFlow is used to train neural networks. Expressed in the form of stateful dataflow graphs, each node in the graph represents the operations performed by neural networks on multi-dimensional arrays. These multi-dimensional arrays are commonly known as “tensors,” hence the name TensorFlow.
In this tutorial, you’ll install TensorFlow in a Python virtual environment with
virtualenv. This approach isolates the TensorFlow installation and gets things up and running quickly. Once you complete the installation, you’ll validate your installation by importing Tensorflow to ensure you have no errors.
Before you begin this tutorial, you’ll need the following:
Step 1 — Creating a Programming Environment
In this step, we’ll create a virtual environment in order to install TensorFlow into it without compromising our other programming projects. If you already have a clean programming environment set up, feel free to skip this step.
First, create a project directory. We’ll call it
tf-demo for demonstration purposes, but choose a directory name that is meaningful to you:
Navigate to your newly created
Then create a new virtual environment called
tensorflow-dev, for instance. Run the following command to create the environment:
- python3 -m venv tensorflow-dev
This creates a new
tensorflow-dev directory which will contain all of the packages that you install while this environment is activated. It also includes
pip and a standalone version of Python.
Now activate your virtual environment:
- source tensorflow-dev/bin/activate
Once activated, your terminal prompt will reflect that you are in the virtual environment:
(tensorflow-dev)[email protected]:~/tf-demo $
At this point you can install TensorFlow in your virtual environment.
Step 2 — Installing TensorFlow
When installing TensorFlow, we want to make sure we are installing and upgrading to the newest version available in PyPi.
Therefore, we’ll be using the following command syntax with pip:
- pip install --upgrade tensorflow
Once you press
ENTER, TensorFlow will install, and you should receive output that indicates that the install along with any dependent packages was successful.
Output... Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.0.1 mock-2.0.0 numpy-1.16.2 pbr-5.1.3 protobuf-3.7.0 setuptools-40.8.0 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0 termcolor-1.1.0 werkzeug-0.15.0 wheel-0.33.1 ... Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
You can deactivate your virtual environment at any time by using the following command:
To reactivate the environment later, navigate to your project directory and run
Now that you have installed TensorFlow, let’s make sure the TensorFlow installation works.
Step 3 — Validating Installation
To validate the installation of TensorFlow, we are going to ensure that we can import the TensorFlow package.
The following prompt will appear on your terminal:
This is the prompt for the Python interpreter, and it indicates that it’s ready for you to start entering some Python statements.
First, type this line to import the TensorFlow package and make it available as the local variable
ENTER after typing in the line of code:
As long as you have received no errors, you have installed TensorFlow successfully. If you have received an error, you should ensure that your server is powerful enough to handle TensorFlow. You may need to resize your server, making sure it has at least 4GB of memory.
In this tutorial, you have installed TensorFlow in a Python virtual environment and validated that TensorFlow works by importing it.
TensorFlow’s programmer’s guide provides a useful resource and reference for TensorFlow development. You can also explore Kaggle, a competitive environment for practical application of machine learning concepts that pit you against other machine learning, data science, and statistics enthusiasts.