Reading Time: 5 minutes

In this tutorial, we are going to take a look at how to get started with TensorFlow on CentOS. We will be covering two methods. First, we will take a look at installing TensorFlow in a Python virtual environment via the Python package manager pip. After that, we will walk through installing TensorFlow via the Anaconda package manager. Finally, we will cover building a TensorFlow pip package from source.

Machine learning is a rapidly evolving sphere of software development, which provides many exciting and amazing technological advancements. Under the hood, it is comprised of complex mathematical formulae and algorithms. TensorFlow offers an avenue for developers to gain access to the power of machine learning and its applications via an elegant Python interface.

How to Install TensorFlow on CentOS

Pre-flight Check:

  • These instructions are being performed as the root user on a Liquid Web Self-Managed CentOS 8 server.
  • These instructions will work on either CentOS 7 or CentOS 8 servers.
  • These instructions assume you have Python version 3.4 or greater already installed on your system.
  • These instructions also assume you have a working version of Anaconda or Miniconda installed. If you’re looking for guidance on getting started with Conda, head over to this article: /kb/using-conda-for-alternate-python-installations/

PIP

Step 1: Update and Setup the Environment

As a best practice, we need to ensure that our system packages are up to date by running the following command:

[root@centos8 ~]# yum update -y

Step 2: Create and Activate the Virtual Environment

Now that the system is up to date, let’s create a virtual Python environment:

[root@centos8 ~]# python3 -m venv pip-tensorflow

The last argument of the command above, “pip-tensorflow” is the name of the virtual environment. Feel free to name the virtual environment whatever you like.

Now that a virtual environment exists, we need to activate it:

[root@centos8 ~]# source pip-tensorflow/bin/activate
(pip-tensorflow) [root@centos8 ~]#

To install TensorFlow 2, the latest and recommended version of TensorFlow, we need to update a couple of key Python packages named setuptools.:

(pip-tensorflow) [root@centos8 ~]# pip install -U pip setuptools
Cache entry deserialization failed, entry ignored
Collecting pip
  Using cached https://files.pythonhosted.org/packages/00/b6/cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
Cache entry deserialization failed, entry ignored
Collecting setuptools
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/f9/d3/55738b20d3832dfa3cd3d9b07e29a8162edb480bf988332f5e6e48ca444/setuptools-44.0.0-py2.py3-none-any.whl (83kB)
    100% |████████████████████████████████| 83kB 1.MB/s
Installing collected packages: pip, setuptools
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
  Found existing installation: setuptools 39.2.0
    Uninstalling setuptools-39.2.0:
      Successfully uninstalled setuptools-39.2.0
Successfully installed pip-19.3.1 setuptools-44.0.0

Step 3: Install and Test TensorFlow

The environment is now ready for TensorFlow to be installed:

(pip-tensorflow) [root@centos8 ~]# pip install tensorflow

TensorFlow is now installed! To verify this and get started with a basic example, we will drop into a Python shell:

Note:
Depending on the version of Python 3 on your machine, the version output for Python might differ slightly on your system than we have in this example.
(pip-tensorflow) [root@centos8 ~]# python
Python 3.6.8 (default, Oct  7 2019, 17:58:22)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Once you’re in the Python shell, copy and paste this snippet of text in and hit enter:

import tensorflow as tf

vals1 = tf.constant([1, 1, 2, 2])
vals2 = tf.constant([2, 2, 4, 4])

result = tf.multiply(vals1, vals2)

tf.print(result)

The output should look like this:

(pip-tensorflow) [root@centos8 ~]# python
Python 3.6.8 (default, Oct  7 2019, 17:58:22)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> vals1 = tf.constant([1, 1, 2, 2])
2020-01-08 22:28:10.881818: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-08 22:28:10.885198: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3599995000 Hz
2020-01-08 22:28:10.885329: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x558aeb6013b0 executing computations on platform Host. Devices:
2020-01-08 22:28:10.885347: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version
>>> vals2 = tf.constant([2, 2, 4, 4])
>>>
>>> result = tf.multiply(vals1, vals2)
>>>
>>> tf.print(result)
[2 2 8 8]

You may notice that there are some warnings regarding the TensorFlow binary not having been compiled to use specific CPU features. The reason for this is that the TensorFlow Pip packages are built without these kernel options in order to allow a wider range of installation options for as many systems as possible.

Not to worry though, as you can still use TensorFlow without issue. It is possible to compile TensorFlow from source to create a package that is compiled to utilize these additional CPU features. For the purposes of this tutorial, we will focus on the basics of TensorFlow and silence these warnings.

(pip-tensorflow) [root@centos8 ~]# echo "export TF_CPP_MIN_LOG_LEVEL=2" >> .bash_profile
(pip-tensorflow) [root@centos8 ~]# source .bash_profile

If we run the example again, we should see those warnings are now hidden:

(pip-tensorflow) [root@centos8 ~]# python
Python 3.6.8 (default, Oct  7 2019, 17:58:22)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> vals1 = tf.constant([1, 1, 2, 2])
>>> vals2 = tf.constant([2, 2, 4, 4])
>>>
>>> result = tf.multiply(vals1, vals2)
>>>
>>> tf.print(result)
[2 2 8 8]

Install TensorFlow Using Anaconda

Step 1: Create the Environment and Install TensorFlow

One of the nice things about utilizing Anaconda or Miniconda to get started with TensorFlow is that you can create the environment and install the package at the same time.

[root@centos8 ~]# conda create -n conda-tensorflow tensorflow -y

In the above command, “conda-tensorflow,” is just the name of the conda environment where TensorFlow will be installed. You can choose any name you like for the environment. Another aspect to note about installing via Conda is that we don’t have to worry about upgrading dependencies for TensorFlow because that is handled for us.

Step 2: Activate and Test TensorFlow

You may have noticed after the creation of the Conda environment and installation of TensorFlow, Conda provides instructions on how to activate and deactivate the environment:

# To activate this environment, use
#
#     $ conda activate conda-tensorflow
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Let’s go ahead and activate it:

[root@centos8 ~]# conda activate conda-tensorflow
(conda-tensorflow) [root@centos8 ~]#

To test things out, let’s once again drop into a Python shell and copy and paste in the same snippet we used earlier.

(conda-tensorflow) [root@centos8 ~]# python
Python 3.7.6 | packaged by conda-forge | (default, Jan  7 2020, 20:28:53)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> vals1 = tf.constant([1, 1, 2, 2])
>>> vals2 = tf.constant([2, 2, 4, 4])
>>>
>>> result = tf.multiply(vals1, vals2)
>>>
>>> tf.print(result)
[2 2 8 8]

That’s all there is to it! This tutorial presents a good example of the kind of work that TensorFlow can do. At this point, you’re ready to start building neural networks and training them to do any number of interesting things, like image recognition or natural language processing!

Avatar for Justin Palmer

About the Author: Justin Palmer

Justin Palmer is a professional application developer with Liquid Web

Latest Articles

In-place CentOS 7 upgrades

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article