When we can't use a binary package of TensorFlow, we have to compile and install it from source.

Step-by-step guide

For Ubuntu and CPU only 

  1. Install Python and the TensorFlow package dependencies
  2. Install Bazel
  3. Compile and install
  4. (Optional) Update tensorflow estimator package


In usual, we can use pre-built TensorFlow packages for Linux and macOS systems. Sometimes even the package was successfully installed, we couldn't run it. Then you should check your processor type. For example, in our case, a model name of processor is "Common KVM processor" which is used in VM environment.

1. Install Python and the TensorFlow package dependencies

$ sudo apt install python-dev python-pip

$ pip install -U --user pip six numpy wheel setuptools mock
$ pip install -U --user keras_applications==1.0.6 --no-deps
$ pip install -U --user keras_preprocessing==1.0.5 --no-deps


If 'pip' is not working even there's no installation error, you can use 'python -m pip' instead of 'pip.'

2. Install Bazel

1) Install required packages

$ sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

2) Download Bazel

Next, download the Bazel binary installer named bazel-<version>-installer-linux-x86_64.sh from the Bazel releases page on GitHub.

3) Run the installer

$ chmod +x bazel-0.25.2-installer-linux-x86_64.sh
$ ./bazel-0.25.2-installer-linux-x86_64.sh --user

4) Set up your environment

The Bazel executable is installed in your $HOME/bin directory.

$ export PATH="$PATH:$HOME/bin"

You can also add the command to your ~/.bashrc file.

3. Compile the source code

# Download the source code
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow

# Configure the build
$ ./configure

# Bazel build (Take a long time)
$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

# Build the package
$ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# Install the package
$ pip install /tmp/tensorflow_pkg/tensorflow-1.13.1-cp27-cp27mu-linux_x86_64.whl

4. (Optional) Update tensorflow estimator package

If you get the error related to 'tpu.TPUConfig' import, you need to update tensorflow estimator from source as well.

# Download tensorflow estimator
$ git clone https://github.com/tensorflow/estimator.git
$ cd estimator

# Bazel build and install
$ bazel build //tensorflow_estimator/tools/pip_package:build_pip_package
$ bazel-bin/tensorflow_estimator/tools/pip_package/build_pip_package /tmp/estimator_pip
$ pip install /tmp/estimator_pip/tensorflow_estimator-1.14.0-py2.py3-none-any.whl

Related articles