Best Steps To Install TensorFlow on Ubuntu 22.04

In this guide, you will learn How To Install TensorFlow on Ubuntu 22.04.

TensorFlow allows developers to create dataflow graphs—structures that describe how data moves through a graph, or a series of processing nodes. Each node in the graph represents a mathematical operation, and each connection or edge between nodes is a multidimensional data array or tensor.

TensorFlow applications can be run on almost any target that’s convenient: a local machine, a cluster in the cloud, iOS and Android devices, CPUs, or GPUs.

Steps to Install TensorFlow on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install Python 3 and Pip on Ubuntu 22.04

Because we want to install TensorFlow in a Python virtual environment, we need to have Python installed on our server.

By default, Ubuntu 22.04 ships with Python 3. To verify it, check your Python version:

python3 --version
Output
Python 3.10.4

If you don’t have Python 3 installed on your server, you can install it by running the command below:

sudo apt install python3

Then, install pip on Ubuntu 22.04 by running the command below:

sudo apt install python3-pip

Verify your installation by checking its version:

pip --version
Output
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

Install Python virtual environment on Ubuntu 22.04

The recommended way to create a virtual environment is by using the venv module, which is provided by the python3-venv package.

First, update your local package index with the command below:

sudo apt update

Then, use the command below to install the Python virtual environment:

sudo apt install python3-venv

Create a virtual environment on Ubuntu 22.04

At this point, you need to create and activate a Python-based virtual environment.

First, create a new directory for your TensorFlow project and switch to it, here we named it tf_project.

# mkdir tf_project
# cd tf_project

Then, from your TensorFlow project directory, run the command below to create the virtual environment:

python3 -m venv venv

This command creates a directory named venv, which contains a copy of the Python binary, the Pip package manager, the standard Python library, and other supporting files.

At this point, you need to activate your virtual environment with the following command:

source venv/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the system $PATH variable. Also, the shell’s prompt will change, and it will show the name of the virtual environment you’re currently in. In this example, that is (venv).

TensorFlow installation requires pip version 19 or higher. Run the following command to upgrade pip to the latest version:

(venv) $ sudo pip install --upgrade pip
Output

...
Successfully installed pip-22.3.1

Installing TensorFlow on Ubuntu 22.04

At this point, you can start your TensorFlow installation on your server. To do this, run the command below:

(venv) $ sudo pip install --upgrade tensorflow

This command will install all the necessary packages along with TensorFlow.

Note: Within the virtual environment, you can use pip instead of pip3 and python instead of python3.

Verify your installation, by using the command below:

(venv) $ python -c 'import tensorflow as tf; print(tf.__version__)'
Output
2.11.0

For more information, you can visit the TensorFlow Learning Page.

Conclusion

At this point, you have learned to Install TensorFlow on Ubuntu 22.04.

Hope you enjoy it.

You may like these articles:

How To Set up PiVPN on Ubuntu 22.04

How To Install Joomla on Ubuntu 22.04

Install Wireshark on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!