Ubuntu

How to Install and Use TensorFlow on Ubuntu 22.04

TensorFlow is an open-source environment that helps users to perform different machine learning-oriented tasks. It includes comprehensive tools, libraries and a community, ensuring a robust environment for the researchers to develop machine learning and AI-related applications with ease. Different platforms have widely adopted it, including PayPal, Twitter, and other popular websites. Thus, it is an essential program for Ubuntu users to install on their operating system.

In this article, we will describe how you can install and use TensorFlow on Ubuntu 22.04.

Install and Use TensorFlow on Ubuntu 22.04

To install and use TensorFlow on Ubuntu 22.04, you will need to perform the following steps.

Step 1: First verify that you have the latest version of python3 installed on Ubuntu 22.04 using the following command:

$ python3 --version


Step 2: Now, install Python virtual environment via venv module on Ubuntu using the following command:

$ sudo apt install python3-venv python3-dev


Step 3: Now, create a Tensor environment folder in your home directory using the following command:

$ mkdir tensor_envrionment


Step 4: Visit the directory and create a virtual environment with the name of your choice using the following command.

$ cd tensor_envrionment

$ python3 -m venv <virtual_environment_name>


The “tensor_env” is the virtual environment name. You can set your own.

Step 5: In order to use the created environment, you will first need to activate it using the following command:

$ source tensor_env/bin/activate


The above command has successfully activated the virtual environment.

Step 6: To install TensorFlow, you will first be required to upgrade your pip installer. The pip is a Python packages manager that includes the latest TensorFlow version. Use the below-given command to upgrade the pip installer:

$ pip install --upgrade pip


Step 7: Once the pip installer is upgraded, you can then install TensorFlow using the following command:

$ sudo python3 -m pip install tensorflow


The installation will take some time as it will download different packages and dependencies required to run the TensorFlow environment. However, it will install TensorFlow on Ubuntu without an error.

To confirm the successful installation of TensorFlow on Ubuntu 22.04, use the following command.

$ sudo python3 -m pip show tensorflow

Testing a code using TensorFlow

To use TensorFlow on Ubuntu 22.04, let’s create a test code to output the version of TensorFlow .

$ python3 -c 'import tensorflow as tf; print("The version of TensorFlow is", tf.__version__)'


The above command will work for GPU enabled machines as TensorFlow will require GPU to execute various machine learning tasks.

Removing TensorFlow from Ubuntu 22.04

To completely remove TensorFlow from Ubuntu 22.04, you will require the following command which will remove all related files of TensorFlow from Ubuntu 22.04.

$ sudo python3 -m pip uninstall tensorflow

Conclusion

TensorFlow is an excellent environment for data scientists and researchers to develop different machine learning and AI-related applications. The TensorFlow on Ubuntu 22.04 can easily be installed using the pip installer whose steps are mentioned above. Once the installation is completed, you can use the TensorFlow environment on your Ubuntu terminal to execute various machine learning codes.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.