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.

tensorflow1

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.

Prerequisites


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 https://centos7.iuscommunity.org/ius-release.rpm
$ 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.

Outputs:

[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

Outputs:

(my_tensorflow) [root@localhost tensorflow_env]# pip3 install --upgrade tensorflow
Collecting tensorflow
Downloading https://files.pythonhosted.org/packages/de/f0/96fb2e0412ae9692dbf400e5b04432885f677ad6241c088ccc5fe7724d69/tensorflow-1.14.0-cp36-cp36m-manylinux1_x86_64.whl (109.2MB)
100% |████████████████████████████████| 109.2MB 48kB/s
Collecting google-pasta>=0.1.6 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/d0/33/376510eb8d6246f3c30545f416b2263eee461e40940c2a4413c711bdf62d/google_pasta-0.1.7-py3-none-any.whl (52kB)
100% |████████████████████████████████| 61kB 2.5MB/s
Collecting wrapt>=1.11.1 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/23/84/323c2415280bc4fc880ac5050dddfb3c8062c2552b34c2e512eb4aa68f79/wrapt-1.11.2.tar.gz
Collecting six>=1.10.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting grpcio>=1.8.6 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/99/83/18f374294bf34128a448ee2fae37651f943b0b5fa473b5b3aff262c15bf8/grpcio-1.21.1-cp36-cp36m-manylinux1_x86_64.whl (2.2MB)
100% |████████████████████████████████| 2.2MB 13.4MB/s
Collecting termcolor>=1.1.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Collecting keras-preprocessing>=1.0.5 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/28/6a/8c1f62c37212d9fc441a7e26736df51ce6f0e38455816445471f10da4f0a/Keras_Preprocessing-1.1.0-py2.py3-none-any.whl (41kB)
100% |████████████████████████████████| 51kB 3.3MB/s
Collecting keras-applications>=1.0.6 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl (50kB)
100% |████████████████████████████████| 51kB 3.3MB/s
Collecting absl-py>=0.7.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/da/3f/9b0355080b81b15ba6a9ffcf1f5ea39e307a2778b2f2dc8694724e8abd5b/absl-py-0.7.1.tar.gz (99kB)
100% |████████████████████████████████| 102kB 3.4MB/s
Collecting protobuf>=3.6.1 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/d2/fb/29de8d08967f0cce1bb10b39846d836b0f3bf6776ddc36aed7c73498ca7e/protobuf-3.8.0-cp36-cp36m-manylinux1_x86_64.whl (1.2MB)
100% |████████████████████████████████| 1.2MB 7.1MB/s
Collecting numpy<2.0,>=1.14.5 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/87/2d/e4656149cbadd3a8a0369fcd1a9c7d61cc7b87b3903b85389c70c989a696/numpy-1.16.4-cp36-cp36m-manylinux1_x86_64.whl (17.3MB)
100% |████████████████████████████████| 17.3MB 3.0MB/s
Collecting wheel>=0.26 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/bb/10/44230dd6bf3563b8f227dbf344c908d412ad2ff48066476672f3a72e174e/wheel-0.33.4-py2.py3-none-any.whl
Collecting gast>=0.2.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/4e/35/11749bf99b2d4e3cceb4d55ca22590b0d7c2c62b9de38ac4a4a7f4687421/gast-0.2.2.tar.gz
Collecting astor>=0.6.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/d1/4f/950dfae467b384fc96bc6469de25d832534f6b4441033c39f914efd13418/astor-0.8.0-py2.py3-none-any.whl
Collecting tensorboard<1.15.0,>=1.14.0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/91/2d/2ed263449a078cd9c8a9ba50ebd50123adf1f8cfbea1492f9084169b89d9/tensorboard-1.14.0-py3-none-any.whl (3.1MB)
100% |████████████████████████████████| 3.2MB 3.2MB/s
Collecting tensorflow-estimator<1.15.0rc0,>=1.14.0rc0 (from tensorflow)
Downloading https://files.pythonhosted.org/packages/3c/d5/21860a5b11caf0678fbc8319341b0ae21a07156911132e0e71bffed0510d/tensorflow_estimator-1.14.0-py2.py3-none-any.whl (488kB)
100% |████████████████████████████████| 491kB 12.4MB/s
Collecting h5py (from keras-applications>=1.0.6->tensorflow)
Downloading https://files.pythonhosted.org/packages/30/99/d7d4fbf2d02bb30fb76179911a250074b55b852d34e98dd452a9f394ac06/h5py-2.9.0-cp36-cp36m-manylinux1_x86_64.whl (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 https://files.pythonhosted.org/packages/c0/4e/fd492e91abdc2d2fcb70ef453064d980688762079397f779758e055f6575/Markdown-3.1.1-py2.py3-none-any.whl (87kB)
100% |████████████████████████████████| 92kB 4.6MB/s
Collecting werkzeug>=0.11.15 (from tensorboard<1.15.0,>=1.14.0->tensorflow)
Downloading https://files.pythonhosted.org/packages/9f/57/92a497e38161ce40606c27a86759c6b92dd34fcdb33f64171ec559257c02/Werkzeug-0.15.4-py2.py3-none-any.whl (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 setup.py install for wrapt ... done
Running setup.py install for termcolor ... done
Running setup.py install for absl-py ... done
Running setup.py 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__)'

Outputs:

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

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 testTensorFlow.py with vim text editor. Like below:

$ sudo vim testTensorFlow.py
#!/bin/python3
import tensorflow as tf
hello = tf.constant("Hello, world!")
session = tf.Session()
print(session.run(hello))

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

$ python3 testTensorFlow.py

Outputs:

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

2019-06-24 22:22:42.207494: I tensorflow/core/platform/cpu_feature_guard.cc:142] 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_utils.cc:94] CPU Frequency: 1800000000 Hz
2019-06-24 22:22:42.214494: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4470f70 executing computations on platform Host. Devices:
2019-06-24 22:22:42.214511: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): , 
b'Hello, world!'

Conclusion


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:

Sidebar



back to top