TensorFlow is a machine learning platform by Google. It is open source and has a huge number of tools, libraries and other resources developed by both its developer community as well as Google and other corporations.

TensorFlow is available for all the popularly used operating systems, viz. Windows, Mac OS, GNU/Linux. It can be downloaded and installed from either Python Package Index using the pip tool and can be run in a virtual python environment. Another way to use it is to install it as a Docker container.

Install TensorFlow using pip

pip is the official package management utility for Python packages. Python and pip are not installed on CentOS by default.

To install the packages, run:

sudo dnf install python3

Whenever the installation asks for confirmation of download, etc., enter Y and then press Enter key to continue the setup. The package python3 will install Python 3 as well as Pip 3.

It is recommended to run TensorFlow inside a Python virtual environment. A virtual environment lets the user run multiple Python environments, with different versions of required packages, isolated from each other, on the same computer. This is to make sure that the development done inside one virtual environment with a specific version of a package doesn’t affect development in another environment.

To run the Python virtual environment, we need to use the module venv. First of all, create and go to your TensorFlow project directory.

mkdir dev/tf
cd dev/tf

To create a virtual environment in this directory, run:

python3 -m venv tf_venv

This will create a new directory tf_venv which is the Python virtual environment. It contains the minimal required files, viz. Python executable file, Pip executable file and some other required libraries.

To start the virtual environment, run:

source bin/ac

This will change the name of the prompt to tf_venv, i.e., the name of the virtual environment folder.

Now we will install TensorFlow in this virtual environment. For TensorFlow, the minimum required pip version is 19. To upgrade pip to latest version, run:

pip install --upgrade pip

As seen above, version 20.0.2 of pip was installed.

Install package TensorFlow in a similar way.

pip install --upgrade tensorflow

The package is quite large in size (~420 MB) and might take some time to get download and install along with its dependencies.

Once installed, we can verify the TensorFlow installation with a small piece of code to check the version of TensorFlow.

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

To exit the virtual environment, run:

deactivate

Install TensorFlow using Docker Container

Docker is now a well-established way to install and run programs in a virtualized environment called Container. It is in a way similar to a Python virtual environment which we saw in the previous method. However, Docker is much broader in scope, and Docker containers are completely isolated and have their own configurations, software bundles and libraries. Containers can communicate with each other through channels.

We can install and run TensorFlow through a Docker container and run it in a virtualized environment. Developers of TensorFlow maintain a Docker Container image which is tested with each release.

First of all, we need to install Docker on our CentOS system. For this, refer the official Docker installation guide for CentOS.

Next, to download the latest container image for TensorFlow, run:

docker pull tensorflow/tensorflow

Note: If your system has a dedicated Graphics Processing Unit (GPU), you can instead download the latest container image with GPU support using the command below.

docker pull tensorflow/tensorflow:latest-gpu-jupyter

Your system must have appropriate drivers for the GPU installed so that the GPU capabilities can be utilized by TensorFlow. For more information on GPU support for TensorFlow, check the documentation on the Github repository.

To run TensorFlow in the Docker container, run:

docker run -it --rm tensorflow/tensorflow python -c "import tensorflow as tf; print(tf.__version__)"

Let’s first try to breakdown what each part of the command means.

run is the docker command to start a container. The flags -it are supplied when we want to start an interactive shell (Eg. Bash, Python). --rm flag, called Clean Up, is specified so that the file system and logs created internally by Docker for the container run are destroyed when the container exits. This flag should not be used if logs are required in the future for debugging purposes. But for small foreground runs like ours, it can be used.

In the next part, we specify the name of our Docker container image, i.e., tensorflow/tensorflow. Following that is the program/command/utility we want to run in the container. For our testing, we are invoking the Python interpreter in the container and passing it the code which prints version of TensorFlow.

We can see that Docker is printing some log while starting the container. After the container starts, our Python code runs and the TensorFlow version is printed (2.1.0).

We can also start the Python interpreter as a shell, so that we can continue running multiple lines of TensorFlow code.


Conclusion

In this article, we saw two methods to install TensorFlow on CentOS. Both methods are meant for running TensorFlow in a virtualized environment, which is a recommended approach while using TensorFlow.

If you are a beginner in TensorFlow, you can start with the basics from the official TensorFlow tutorials.