How to Install TensorFlow on CentOS or RHEL 7

This post will guide you how to install and use TensorFlow on your CentOS or RHEL Linux 7. How do I install TensorFlow machine learning library in Python Virtual Environment on CentOS or RHEL Linux system.


What is TensorFlow?

TensorFlow is an end-to-end open source platform for machine learning. You can use TensorFlow to develop machine learning applications. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications. It is developed by Google to do search ranking in their machine learning system.

TensorFlow can be installed in a Python virtual environment, or in a Docker container.


You need to have a non-root user with sudo privileges so that you want install some necessary packages on your CentOS system.

Method1: Installing TensorFlow with Python3 Virtual Environment

Step1: You need to install the Python3 on your CentOS system, and we have talked this topic in the previous post. You can refer to it.
Or you can directly run the following command to install python36 and other packages,type:

$ sudo yum -y install
$ sudo yum -y install python36u
$ sudo yum -y install python36u-pip
$ sudo yum -y install python36u-devel

Step2: Create a Python Virtual Environment called tensorflow_env using the Python module venv. And each Python Virtual Environment has its own Python binary. Type:

$ mkdir tensorflow_env
$ cd tensorflow_env
$ python3.6 -m venv my_tensorflow

The above commands will create a new my_tensoflow directory which will contain all of the packages that you install while this python virtual environment is activated.


[root@localhost ~]# mkdir tensorflow_env
[root@localhost ~]# cd tensorflow_env/
[root@localhost tensorflow_env]# python3.6 -m venv my_tensorflow
[root@localhost tensorflow_env]# ls my_tensorflow/
bin include lib lib64 pyvenv.cfg

Step3: You need to activate this virtual environment to start working on it with the following command:

$ source my_tensorflow/bin/activate

Once your virtual environment is activated, you should see something similar to the below:

[root@localhost tensorflow_env]# source my_tensorflow/bin/activate
(my_tensorflow) [root@localhost tensorflow_env]#

Note: you can now just use ‘python’ for what we need and install some modules that are only seen by my_tensorflow.

Step4: you can install TensorFlow on your virtual environment, just run the following command to install and upgrade to the latest version of TensorFlow, type:

$ pip3 install --upgrade tensorflow


(my_tensorflow) [root@localhost tensorflow_env]# pip3 install --upgrade tensorflow
Collecting tensorflow
Downloading (109.2MB)
100% |████████████████████████████████| 109.2MB 48kB/s
Collecting google-pasta>=0.1.6 (from tensorflow)
Downloading (52kB)
100% |████████████████████████████████| 61kB 2.5MB/s
Collecting wrapt>=1.11.1 (from tensorflow)
Collecting six>=1.10.0 (from tensorflow)
Collecting grpcio>=1.8.6 (from tensorflow)
Downloading (2.2MB)
100% |████████████████████████████████| 2.2MB 13.4MB/s
Collecting termcolor>=1.1.0 (from tensorflow)
Collecting keras-preprocessing>=1.0.5 (from tensorflow)
Downloading (41kB)
100% |████████████████████████████████| 51kB 3.3MB/s
Collecting keras-applications>=1.0.6 (from tensorflow)
Downloading (50kB)
100% |████████████████████████████████| 51kB 3.3MB/s
Collecting absl-py>=0.7.0 (from tensorflow)
Downloading (99kB)
100% |████████████████████████████████| 102kB 3.4MB/s
Collecting protobuf>=3.6.1 (from tensorflow)
Downloading (1.2MB)
100% |████████████████████████████████| 1.2MB 7.1MB/s
Collecting numpy<2.0,>=1.14.5 (from tensorflow)
Downloading (17.3MB)
100% |████████████████████████████████| 17.3MB 3.0MB/s
Collecting wheel>=0.26 (from tensorflow)
Collecting gast>=0.2.0 (from tensorflow)
Collecting astor>=0.6.0 (from tensorflow)
Collecting tensorboard<1.15.0,>=1.14.0 (from tensorflow)
Downloading (3.1MB)
100% |████████████████████████████████| 3.2MB 3.2MB/s
Collecting tensorflow-estimator<1.15.0rc0,>=1.14.0rc0 (from tensorflow)
Downloading (488kB)
100% |████████████████████████████████| 491kB 12.4MB/s
Collecting h5py (from keras-applications>=1.0.6->tensorflow)
Downloading (2.8MB)
100% |████████████████████████████████| 2.8MB 6.6MB/s
Requirement already satisfied, skipping upgrade: setuptools in ./my_tensorflow/lib/python3.6/site-packages (from protobuf>=3.6.1->tensorflow) (40.6.2)
Collecting markdown>=2.6.8 (from tensorboard<1.15.0,>=1.14.0->tensorflow)
Downloading (87kB)
100% |████████████████████████████████| 92kB 4.6MB/s
Collecting werkzeug>=0.11.15 (from tensorboard<1.15.0,>=1.14.0->tensorflow)
Downloading (327kB)
100% |████████████████████████████████| 327kB 6.9MB/s
tensorboard 1.14.0 has requirement setuptools>=41.0.0, but you'll have setuptools 40.6.2 which is incompatible.
Installing collected packages: google-pasta, wrapt, six, grpcio, termcolor, numpy, keras-preprocessing, h5py, keras-applications, absl-py, protobuf, wheel, gast, astor, markdown, werkzeug, tensorboard, tensorflow-estimator, tensorflow
Running install for wrapt ... done
Running install for termcolor ... done
Running install for absl-py ... done
Running install for gast ... done
Successfully installed absl-py-0.7.1 astor-0.8.0 gast-0.2.2 google-pasta-0.1.7 grpcio-1.21.1 h5py-2.9.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.16.4 protobuf-3.8.0 six-1.12.0 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.15.4 wheel-0.33.4 wrapt-1.11.2

If you want to verify the installation if it is correct, just run the following command to print the TensorFlow version:

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


(my_tensorflow) [root@localhost tensorflow_env]# python -c 'import tensorflow as tf; print(tf.__version__)'

Now you should install the TensorFlow on your CentOS system.

Method2: Installing TensorFlow with Docker

You can also install TensorFlow in a Docker container on your CentOS 7 system. You just need to install docker-ce firstly, then downloading the TensorFlow image to your local disk, and then run it. See the below steps:

Step1: you need to download the TensorFlow Docker image with docker pull command, type:

$ sudo docker pull tensorflow/tensorflow

Step2: if the downloading process is completed, you can start to run that image on your Docker env with the following command:

$ docker run -it -p 8888:8888 tensorflow/tensorflow

If you want to learn more about running TensorFlow in Docker from its official website.

Creating Simple TensorFlow Program

Once the installation is completed, you can check if your TensorFlow is in running condition or not. So you can write a simple hello world code called with vim text editor. Like below:

$ sudo vim
import tensorflow as tf
hello = tf.constant("Hello, world!")
session = tf.Session()

Save and close the file, then executing this python file with the following commad:

$ python3


(my_tensorflow) [root@localhost tensorflow_env]# python
WARNING: Logging before flag parsing goes to stderr.
W0624 22:22:42.206840 140070126434112] From The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2019-06-24 22:22:42.207494: I tensorflow/core/platform/] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-06-24 22:22:42.214358: I tensorflow/core/platform/profile_utils/] CPU Frequency: 1800000000 Hz
2019-06-24 22:22:42.214494: I tensorflow/compiler/xla/service/] XLA service 0x4470f70 executing computations on platform Host. Devices:
2019-06-24 22:22:42.214511: I tensorflow/compiler/xla/service/] StreamExecutor device (0): , 
b'Hello, world!'


You should know that how to install TensorFlow on your CentOS or RHEL 7 Linux. If you want to see more detailed information about TensorFlow, you can directly go to its official web site.

You might also like:


back to top