Install and Use TensorFlow on Ubuntu 22.04

TensorFlow is a freely available open-source platform for performing Artificial Intelligence and Machine Learning tasks. It can help you enhance your skills related to coding, machine learning products. TensorFlow is also utilized to train models using Python or JavaScript. On Windows operating systems, TensorFlow can be installed using Anaconda or pip; However, on Linux-based systems such as Ubuntu 22.04, it can be installed with Python 3 and pip.

In this post, we will go through the procedure to install and utilize TensorFlow on Ubuntu 22.04 in detail.

Installation of TensorFlow on Ubuntu 22.04

To install TensorFlow on Ubuntu 22.04, follow the below-mentioned steps.

Step 1: Update and upgrade APT packages

First update and Upgrade APT packages of the Ubuntu repository. To do so fire up the terminal by using “CTRL+ALT+T” and run the following command:

$ sudo apt update && sudo apt upgrade

Now we will move to next step.

Step 2: Verify Python3 version

As stated above, to work with TensorFlow, it is required to have Python3 installed on your system. For the verification of the installed Python3 version, utilize the provided command:

$ python3 --version

In case, if you do not have Python3 installed on your system, then type out the following command:

$ sudo apt install python3-pip

Python3-pip installation will take some minutes to complete:

Step 3: Installing virtual environment

Now we need to install Virtual environment using Python3 venv command:

$ sudo apt install python3-venv python3-dev

Step 4: Make directory for tensor environment

Create a directory named“tensor_enviroment” for setting up TensorFlow by utilizing following command:

$ mkdir tensor_environment

Step 5: Move to tensor_environment directory

Now, move to the “tensor_enviroment” directory using the given “cd” command:

$ cd tensor_environment

Step 6: Create Virtual Environment

In this step, you have to create a Virtual Environment “tensorenviroment” using “venv” command as shown below:

$ python3 -m venv tensorenviroment

Step 7: Activate Virtual Environment

Then, activate the created Virtual Environment named “tensorenviroment” using below-given command:

$ source tensorenviroment/bin/activate

Step 8: Upgrade pip version

We need to Upgrade pip version using the below-given command:

$ pip install --upgrade pip

Step 9: Install TensorFlow

After performing the previous given steps, install TensorFlow on your Ubuntu 22.04 system with the help of the following command:

$ sudo python3 -m pip install tensorflow

As you can see, we have successfully installed TensorFlow on our system

Step 10: Validate TensorFlow installation

Now we can verify TensorFlow installation by checking its version:

$ sudo python3 -m pip show tensorflow

Now we will move towards the procedure to use TensorFlow.

Using TensorFlow on Ubuntu 22.04

This section will demonstrate the method to use TensorFlow for running Python code. So, to test the working of TensorFlow for the specified purpose, run the following piece of code on your Ubuntu 22.04 terminal:

$ python3 -c 'import tensorflow as tf; print("Our Tensorflow version is", tf.__version__)'

We have shown the procedure to use TensorFlow on Ubuntu 22.04. Now check out its uninstallation method.

Uninstallation of TensorFlow from Ubuntu 22.04

To uninstall TensorFlow from Ubuntu, run the below-given command:

$ sudo python3 -m pip uninstall tensorflow

We have demonstrated all the required steps for the installation and usage of TensorFlow on Ubuntu 22.04.

Conclusion

To install and use TensorFlow on Ubuntu 22.04, we need to have the latest python 3 version on your system. Then, create and activate Virtual Environment. After that, upgrade the pip installer’s version and then use “$ sudo python3 -m pip install tensorflow” command to install TensorFlow on Ubuntu 22.04 In this write up, we have completely explained all the steps required for installation and usage of TensorFlow on Ubuntu 22.04.