Tutorial — How to Install TensorFlow 2.0 on Ubuntu 20.04?

Kunal Ajay Kulkarni
The Startup
Published in
6 min readJan 6, 2021

An open-source machine learning library, TensorFlow is used to train, test develop, and deploy neural networks. In this tutorial, you’ll install TensorFlow in a Python virtual environment with virtualenv. This approach isolates the TensorFlow 2.0 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 diving deep into the process, first, let us understand what Tensor and TensorFlow are.

What is Tensor?

Mathematically speaking, a Tensor is an object which is represented as a multi-dimensional array. It is a container that can contain data in N different dimensions, and ranks. These tensors ate used for various statistical and machine learning applications. Also, Tensors are used as an input to the neural network to create the neural network.

Apart from machine learning, Tensors are also useful in physics. They provide compact mathematical frameworks for solving physics problems in areas such as fluid mechanics, general relativity, electrodynamics, and so on.

What is TensorFlow?

TensorFlow is a free and open-source end-to-end machine learning platform created by the Google Brain team in 2015. Its core open-source library helps users to test, train, develop and deploy their machine learning models. TensorFlow is Google Brain’s second-generation system. It is written in all major programming languages such as Python, C++, and CUDA. It runs across all major 64-bit OS platforms such as Windows, macOS, Linux, JavaScript, iOS, and Android. It can run on multiple CPUs, TPUs and GPUs.

The initial version of TensorFlow was released in November 2015 under Apache License by the Google Brain team. Their latest release — TensorFlow 2.0 — came in September 2019. it is being used by all major corporate giants all over the world, including NVIDIA, Uber, Twitter, PayPal, Intel, Netflix, AMD, and of course Google.

Prerequisites for installing TensorFlow 2.0 on Ubuntu –

TensorFlow is supported on the following 64-bit systems –

  1. Python 3.5 and above
  2. Ubuntu 20.04
  3. Pip 19.0 and newer version
  4. Administrative access to your system
  5. You should know how to use the command line

There are many important updates in TensorFlow 2.0 like multi-GPU support, and so on, but the most important update is that the Keras is now officially a deep learning API for TensorFlow. This means that you can implement Keras inside TensorFlow 2.0, by tf.keras when training your own high-level deep learning neural networks and algorithms. So, let’s begin: -

1. Install the Python IDE –

Before starting the tutorial, it is essential that you should have Python 3.5 and above installed on your computer. Also, you need to install a PIP package and a virtual environment. If you already have these installed, you can ignore this step and move on to the next one. If you haven’t installed it yet, here is how you can download and install it –

For Python — https://www.python.org/downloads/

For PIP — https://pip.pypa.io/en/stable/installing/

For the virtual environment — https://docs.python.org/3/library/venv.html

First, let us understand how to install Python 3 on Ubuntu 20.04 –

Step 1 — Setting up Python 3

Ubuntu 20.04 and other versions of Linux come with Python 3 pre-installed. To make sure that other versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

The -y command will confirm that we are agreeing to all the terms and conditions before the installation of Python. Depending on your version of Linux, you may get different prompts for permission or system updates, which you have to agree. Once the installation is successfully done, you can check the version of the Python you installed by using the below command –

$ python 3 -V

This will give you an output in the terminal command which will look similar to this — Python 3.8.5. Now let’s install pip to manage software packages in Python –

$ sudo apt install -y python3-pip

Python packages can be installed by typing –

$ pip install package_name

Here, the package_name refers to the Python library or packages, such as Django for Web Development, or pandas for data manipulation. So, if you would like to install pandas, you can do it with the command, pip 3 install pandas. Once the Python is set up, and the pip is installed, we can set up a virtual environment for our projects.

Step 2 — Setting up a virtual environment

Virtual environment lets you have a separate space on your desktop or laptop for building Python projects, ensuring that each of your projects can have its own set of dependencies, and that won’t affect any of your other projects. And the good thing is that you can set up as many Python programming environments as you would like. If you already have Ubuntu installed on your machine, then you can skip this step.

While there are many ways to create a virtual environment in Python, we will be using venv mode here, which comes in-built with Python 3. So, let’s install it by using the following command –

$ sudo apt install -y python3-venv

After installing this, you will be able to create VE easily. So, now we’ll create a new directory to put our Python projects in –

$ mkdir environments
$ cd environments

Once you are in the directory you have just created, you can create a virtual environment by typing the following –

$ python3 -m venv my_env

You can view the items created by the new directory by using the following command –

$ ls my_env

To use this environment, you first need to activate it by using the following command –

$ source my_env/bin/activate

Now your command prompt will be prefixed with the name of your environment you just created; in this case, it is called my_env.

2. Create a Programming Environment to Install TensorFlow

In this step, we will create a VE to install TensorFlow 2.0 without affecting our other Python projects. You can create this in your home directory or you can use the directory you just have created. You can create a new project directory by using this command –

$ mkdir ~/tf_newproject

Note — I have created this just directory just for demonstration purposes. You should choose a name that’s meaningful to you or which you can remember easily.

Now navigate to your new directory by using this command –

$ cd ~/tf_newproject

Create a new VE. For example- tensorflow-np.

$ python3 -m venv tensorflow-np

This creates a new tensorflow-np directory which will contain all of the packages that you install while this environment is activated. It also includes pip. Now activate this new virtual environment –

$ source tensorflow-np/bin/activate

Once this virtual environment is activated, your command prompt will show that you are in the virtual environment.

3. Installing TensorFlow 2.0

While installing TensorFlow, we want to make sure we are installing the newest version available. Therefore, we will use the following command to install the TensorFlow –

$ pip install tensorflow

Once you press ENTER, Tensorflow will install. To update TensorFlow to the latest version, add –upgrade to the above commands.

$ pip install --upgrade pip
$ pip install --upgrade tensorflow

To verify if TensorFlow is installed successfully, use the command given below:

python -c 'import tensorflow as tf; print(tf.__version__)'

Also, you can use the following commands in the Jupyter notebook –

import tensorflow as tf
print(tf.__version__)

Also, you can run the following commands –

Once you are done with your project, deactivate the environment by using the following command –

$ deactivate

Conclusion

In this article, we’ve learned how to install TensorFlow on Ubuntu 20.04. If you come across any problem or want to give feedback, please leave a comment below.

Thank You!

--

--

Kunal Ajay Kulkarni
The Startup

Instrumentation Engineer | Data Science and Machine Learning enthusiast | Avid Reader