Skip to main content

Building TensorFlow for CentOS 6

Technical
Author
Kuan-Yi Li
Table of Contents
You should no longer need this as you can install and run TensorFlow smoothly with Anaconda now!

Just install Anaconda and install TensorFlow by running

conda install tensorflow

or

conda install tensorflow-gpu

and you’re good to go since Continuum Analytics have applied our patch.

Note: Do NOT install TensorFlow package from conda-forge as that is for newer version of Linux distros.

Introduction
#

The official-released binary packages of TensorFlow are built for newer version of Linux distros. Here is how to build TensorFlow binary package for CentOS 6.

Install Dependencies
#

Admin privilege is required here.

OpenJDK (for Bazel)
#

yum install java-1.8.0-openjdk-devel

Developer Toolset (for Bazel and TensorFlow)
#

C++11 compatible compiler is required during the building process. Follow the instructions in this article to install Developer Toolset. It is worth noting that, in this case, installing just devtoolset-6-toolchain would be sufficient.

Python 2.7 (for TensorFlow)
#

This step also requires Software Collections installed. Therefore, if you’ve installed Developer Toolset by following the instructions above, you should have no problem installing these dependencies.

yum install python27 python27-numpy python27-python-devel python27-python-wheel

Build and Install Bazel
#

Enter Build Environment
#

Enter Software Collection environment with Developer Toolset.

scl enable devtoolset-6 bash

Prepare Source Code
#

Method 1 (Recommended) #

The standard way of compiling a release version of Bazel from source is to use a distribution archive. From version 0.4.1, bootstrapping Bazel from GitHub repository is no longer supported as only the distribution archives contain generated artifacts required for direct compilation. Download bazel-<VERSION>-dist.zip from the release page.

# download distribution archive
wget https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip
unzip bazel-0.9.0-dist.zip -d bazel-0.9.0-dist
cd bazel-0.9.0-dist

Method 2 (Only Works for Version <= 0.4.0)
#

# clone source code repository
git clone https://github.com/bazelbuild/bazel.git
cd bazel

# select version (optional)
git checkout 0.4.0

Build and Install
#

# compile
./compile.sh

# install
mkdir -p ~/bin
cp output/bazel ~/bin/

# exit from Software Collection environment
exit

Build TensorFlow with Bazel
#

Enter Build Environment
#

Enter Software Collection environment with Developer Toolset and Python 2.7.

scl enable devtoolset-6 python27 bash

Prepare Source Code
#

# clone source code repository
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow

# select version (optional)
git checkout v1.5.0

Since GNU C library version in CentOS 6 is less than 2.17, a slight modification needs to be applied before compilation.

Modify tf_extension_linkopts function in tensorflow/tensorflow.bzl from

def tf_extension_linkopts():
  return []  # No extension link opts

to

def tf_extension_linkopts():
  return ["-lrt"]

Build
#

After above modification, we can now start building!

Here we disable jemalloc when configuring—otherwise the build will fail.

# configure workspace
export \
    PYTHON_BIN_PATH=/opt/rh/python27/root/usr/bin/python \
    PYTHON_LIB_PATH=/opt/rh/python27/root/usr/lib/python2.7/site-packages \
    TF_NEED_JEMALLOC=0 \
    TF_NEED_GCP=0 \
    TF_NEED_HDFS=0 \
    TF_NEED_S3=0 \
    TF_ENABLE_XLA=0 \
    TF_NEED_GDR=0 \
    TF_NEED_VERBS=0 \
    TF_NEED_OPENCL=0 \
    TF_NEED_CUDA=0 \
    TF_NEED_MPI=0 \
    CC_OPT_FLAGS="-march=native"
./configure

# build
~/bin/bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# exit from Software Collection environment
exit

Finishing Up
#

Finally, you will found a tensorflow-1.5.0-cp27-none-linux_x86_64.whl (filename may vary upon versions) in /tmp/tensorflow_pkg/. With this self-built binary package, we can now deploy TensorFlow to CentOS 6 by pip.

Enter Python Environment on Deployment Target
#

Enter Python 2.7 environment either by Software Collection or by tools like pyenv.

# by Software Collection
scl enable python27 bash

# or by pyenv
pyenv shell anaconda2-5.0.1

Install
#

Depending on your environment setup, admin privilege might be needed here.

pip install tensorflow-1.5.0-cp27-none-linux_x86_64.whl

Note: sudo clears environment variables before running commands for security reasons, causing Software Collection environment, which relies on environment variables, to dysfunction. If you are installing TensorFlow package in Software Collection environment through sudo, you might find this command helpful.

sudo scl enable python27 -- pip install tensorflow-1.5.0-cp27-none-linux_x86_64.whl

It sets up Software Collection environment variables AFTER sudo cleans them up.

References
#