How to install the TensorFlow Python machine learning library on CentOS 8

TensorFlow is an essential open source machine learning library built by Google. It can run on both GPUs and CPUs of various devices. TensorFlow is used by many organizations including PayPal, Intel, Twitter, Lenovo, and Airbus. It can be installed as a Docker container, or in a Python virtual environment, or with Anaconda.

In this article, you will learn how to install the popular Python machine learning library TensorFlow on CentOS 8 using a Python virtual environment.

Installing TensorFlow on CentOS 8

TensorFlow provides compatibility with both Python 2 and Python 3. In this tutorial, we will be using Python 3, and inside a virtual environment, we will install TensorFlow. Using a virtual environment, you can create multiple isolated Python environments on the same system and install a specific version of a module as required by the project without affecting other Python projects.

To install TensorFlow on CentOS 8, we need to follow these steps:

Open a terminal window with the shortcut Ctrl + Alt + t. Or open it by clicking Actions and choosing Terminal from the left side panel of your desktop.

Log in as root (or log in as a user with administrator rights and use sudo -s) to install the required packages for TensorFlow on your system.

Python is not installed by default on CentOS 8. Install Python 3 with the following command in a terminal:

Install Python 3

$ sudo dnf install python3

The above command will install python 3.6 and pip3 on your system. It is already installed on my system as you can see in the screenshot. You can start python by explicitly typing python 3 in the terminal.

Note. To start with python 3 it is recommended to create a virtual environment to use the venv module.

You will now be taken to the directory where you want to store your TensorFlow projects. You can store in your home directory or another where you have all read and write permissions. Create a new directory and name it “tensorflow_project” for your TensorFlow project, then change to that directory. Use the following command to complete these steps:

$ mkdir tensorflow_project
$ cd tensorflow_project

Create directory for TensorFlow

You will now create a virtual environment. Use the following command to create a virtual environment in the tensor_flow directory:

$ python3 -m venv venv

The above command creates a directory called venv that stores a copy of the python binary, pip of the python standard library, and other supporting files. You can assign any name to the virtual environment.

Use the following command to activate the virtual environment:

$ source venv/bin/activate

Create virtual environment in Python

Once the virtual environment is activated, the bin directory will be added to the beginning of the path, and the terminal prompt will change to display the current name of the virtual environment. We use the name venv here.

Tensorflow supports pip version 19 or higher. You need to update pip to the latest version. You run the following command in the terminal to update the pip:

(venv) $ pip install --upgrade pip

Install pip

After activating the virtual environment, you will install the TensorFlow library by running the following command:

(venv) $ pip install --upgrade tensorflow

Install TensorFlow

You can test the installation using the following command, which will print the TensorFlow version:

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

After executing this command, the TensorFlow version will be displayed on the terminal.

Check your TensorFlow installation

When you are done, you deactivate the environment and return to your normal working shell. Use the following command on a terminal to deactivate the virtual environment:

Disable TensorFlow

(venv) $ deactivate

Now we are back in your usual shell and continue to work.

If you haven’t used TensorFlow before, visit the TensorFlow home page to learn how to work with machine learning applications. You can also run cloned TensorFlow models or examples from the Github repos for testing on your system.

Output

In this article, you learned how to install the TensorFlow library on CentOS 8. In addition, you also learned how to create and deactivate a virtual environment in Python using the terminal. I hope you enjoyed this tutorial and it helps you.

How to install the TensorFlow Python machine learning library on CentOS 8

Sidebar