Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Building Tensorflow on CentOS 7.X from Source

I figured out how to build tensorflow from source in centOS. This process does not require any root access and you can do it anywhere. This will save your time and no need to worry much after this.

#What to prepare:

  1. Java 8
  2. Bazel
  3. Tensorflow
  4. CuDNN and CUDA toolkit (assume you have install them)

#Installation ##Bazel

I am using Bazel 0.3.2 since I found issues with Bazel in master branch.

check your JAVA_HOME since Bazel requires Java 8, you should download and install it first. This tutorial will not cover it.

$ git clone https://github.com/bazelbuild/bazel.git
$ git checkout tags/0.3.2
$ cd bazel
$ ./compile.sh

It takes some time and after you finished, and add the binary path into the PATH variable, in this case

$ /PATH-TO-BAZEL/output/

##TensorFlow

This is the toughest software to build that I ever know (if you don't have any access to admin). You can download Tensorflow from github as mentioned in the website https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html

$ git clone https://github.com/tensorflow/tensorflow

Then, you may need to hack the code before you can start the configuration free from issues. Go to file tensorflow/third_party/gpus/crosstool/CROSSTOOL and update cxx_builtin_include_directory with

cxx_builtin_include_directory : "/usr/local/cuda/targets/x86_64-linux/include"

Run the configuration

$ ./configure

If you are wonder to use Tensorflow in a GPU with less than 3.5 compute capabilities, you may run this command and type the compute capabilities you desired

TF_UNOFFICIAL_SETTING=1 ./configure

Build with bazel

# Without GPU support
$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

# To build with GPU support:
$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

# Build whl file
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

Install tensorflow

pip install --upgrade /tmp/tensorflow_pkg/<your whl file>.whl

And done!

@haoxue15

This comment has been minimized.

Copy link

@haoxue15 haoxue15 commented Mar 14, 2017

Pretty good!

@daisyron

This comment has been minimized.

Copy link

@daisyron daisyron commented May 19, 2017

I tried your instructions with the latest tensorflow (1.1) with no success: I've go the following error:

ERROR: /root/tensorflow/tensorflow/core/kernels/BUILD:2912:1: undeclared inclusion(s) in rule '//tensorflow/core/kernels:fake_quant_ops_gpu':
this rule is missing dependency declarations for the following files included by 'tensorflow/core/kernels/fake_quant_ops_gpu.cu.cc':
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/limits.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/syslimits.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stddef.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stdarg.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stdint.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/x86intrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/ia32intrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/mmintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/xmmintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/mm_malloc.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/emmintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/immintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/fxsrintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/adxintrin.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/float.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stdbool.h'.
In file included from /usr/local/cuda-8.0/bin/..//include/host_config.h:173:0,
                 from /usr/local/cuda-8.0/bin/..//include/cuda_runtime.h:78,
                 from <command-line>:0:

System information:

  • Linux Centos 7.2
  • Kernel: 3.10.0-327.36.1.el7.x86_64
  • Compiler: gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
  • Tensorflow: 1.1, installed from source.
  • nvcc: Cuda compilation tools, release 8.0, V8.0.44
  • CUDA: 8.0
  • cudaa: 5.1.5
  • bazel: 0.4.5

BTW on fedora 23 it works fine (have gcc 5.3, while centos 7 has 4.8.5).

Any help?

Thanks,
Ron.

@mpenagar

This comment has been minimized.

Copy link

@mpenagar mpenagar commented Jan 17, 2018

Thanks!

Think that instead of

$ git clone https://github.com/bazelbuild/bazel.git
$ git checkout tags/0.3.2
$ cd bazel
$ ./compile.sh

It should be:

$ git clone https://github.com/bazelbuild/bazel.git
$ cd bazel
$ git checkout tags/0.3.2
$ ./compile.sh

Anyway, I managed to build bazel (https://docs.bazel.build/versions/master/install-compile-source.html):

$ mkdir bazel
$ cd bazel
$ wget https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip
$ unzip bazel-0.9.0-dist.zip
$ ./compile.sh
$ cd ..

@cqychen

This comment has been minimized.

Copy link

@cqychen cqychen commented Feb 10, 2018

bazel ,I suggest to use command :yum install bazel

@haramoz

This comment has been minimized.

Copy link

@haramoz haramoz commented Sep 26, 2018

I am unable to use this command yum install bazel because i do not have root access, the centOS i need to setup is in the cluster environment.

@jorgeramirez

This comment has been minimized.

Copy link

@jorgeramirez jorgeramirez commented May 24, 2019

For those coming from Google and trying to compile tensorflow current 1.12 release. Then, first,
use bazel from here. I encountered some issues while trying to use the newest version of bazel.

When trying to do

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

I encountered an error and fixed it following this tip. After this fix tensorflow compiled successfuly.

@AmericanEnglish

This comment has been minimized.

Copy link

@AmericanEnglish AmericanEnglish commented Nov 7, 2019

As of tensorflow 2.0

I noticed that, as someone who came from google, these instructions didn't help except the build statement itself.
If you're someone who doesn't have root access and/or working in a cluster style environment. I'd like to mention these issues which talk about fixes for problems I encountered:

  1. Header errors despite their existence
    tensorflow/tensorflow#33975
  2. SWIG and issues with libstdc++.so.1 errors despite gcc being up to date. This issue talks about using a local version of SWIG instead.
    bazelbuild/bazel#4053

I'd also like to respectfully plug my own gist that talks about it as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment