How to install TensorFlow on Ubuntu 20.04?

Overview

Neural networks are trained using TensorFlow, a machine learning toolkit that is free and open-source. Each node in the graph is represented like a stateful dataflow graph and represents an action that even a neural network takes on a multidimensional array. These multidimensional arrays also referred to as "tensors," are the inspiration for the name TensorFlow.

In this tutorial, you'll learn how to install TensorFlow in a virtual Python environment using virtualenv. With this approach, the TensorFlow setup is isolated, and operations begin immediately. Import TensorFlow after installation to check for errors and ensure your installation is accurate.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 20.04 equipped system

  • A regular root user

  • Internet connection

  • Knowledge of Command Line Interface(CLI)

Get Started

Step 1: Programming Environment Configuration

We'll build a virtual environment in this phase so that we may install TensorFlow there without jeopardizing our other programming endeavors.

  • Make a project directory first and go to the directory you just created:

mkdir ~/tf-demo
cd ~/tf-demo
  • Use the given command to setup the python3 virtual environment:

apt install python3-venv

When prompted to continue, Press y.

  • Then, develop a fresh virtual environment. To build the environment, enter the following command:

python3 -m venv tensorflow-dev

The packages you install when this environment is engaged will be stored in a new directory that is created as a result of the above command. A standalone Python version and pip are also included in the installation.

  • Your virtual environment can now be activated:

source tensorflow-dev/bin/activate

When active, the terminal prompt will show that you are in a virtual environment. You can now set up TensorFlow in your newly created virtual environment.

Step 2: TensorFlow Installation

Make sure you are installing and updating to the most recent version of TensorFlow when installing it.

  • Consequently, we'll execute the following command:

pip install --upgrade tensorflow

If the installation and any related packages were successful, you should get results indicating that.

Now that TensorFlow has been installed, let's check to see if everything worked out as expected.

Step 3: Verifying the Installation

We are going to confirm we are able to import the TensorFlow package in order to verify the TensorFlow installation.

  • You'll see the following prompt on your terminal:

python

The Python interpreter is now ready for you to start typing some Python statements, as indicated by this prompt.

  • The TensorFlow package will be imported and made accessible as a local variable once you have typed this line. After entering the line of code, hit ENTER.

import tensorflow as tf

TensorFlow has been successfully installed if no errors are given by the terminal.

Conclusion

TensorFlow was set up in a virtual Python environment using this tutorial, and we also tested its functionality by importing it.

Last updated