Skip to content

Instantly share code, notes, and snippets.

@philwo
Created November 11, 2019 14:18
Show Gist options
  • Save philwo/f3a8144e46168f23e40f291ffe92e63c to your computer and use it in GitHub Desktop.
Save philwo/f3a8144e46168f23e40f291ffe92e63c to your computer and use it in GitHub Desktop.
How to build TensorFlow 2.0 on Ubuntu 18.04 (x86_64) with Bazelisk

This is how I managed to build TensorFlow 2.0 on Ubuntu 18.04 (x86_64) with Bazelisk:

$ sudo apt update
$ sudo apt full-upgrade
$ sudo apt install curl

# Install Bazelisk.
$ sudo curl -Lo /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/v1.1.0/bazelisk-linux-amd64
$ sudo chmod +x /usr/local/bin/bazel

# This should work and print a Bazelisk and Bazel version.
$ bazel version
Bazelisk version: v1.1.0
Build label: 1.1.0
[...]

# Now we're following the official "Build from source" steps:
# https://www.tensorflow.org/install/source
$ sudo apt install python python3-{dev,pip,six,numpy,wheel,setuptools,mock}
$ pip3 install -U --user 'future>=0.17.1'
$ pip3 install -U --user keras_applications --no-deps
$ pip3 install -U --user keras_preprocessing --no-deps

# Download TensorFlow 2.0:
$ curl -LO https://github.com/tensorflow/tensorflow/archive/v2.0.0.tar.gz
$ tar xvfz v2.0.0.tar.gz
$ rm v2.0.0.tar.gz
$ cd tensorflow-2.0.0

# Find out which Bazel version we need to build this release:
$ grep -r _TF_MAX_BAZEL_VERSION .
./configure.py:_TF_MAX_BAZEL_VERSION = '0.26.1'

# Tell Bazelisk to build this version of TensorFlow with the matching release.
# Note: If you build TensorFlow from HEAD, this is not necessary, because the
# master branch now already includes a .bazelversion file.
$ echo "0.26.1" > .bazelversion

# Verify that we use the correct Bazel version now:
$ bazel version
[...]
Build label: 0.26.1

# Configure the build.
# When asked for the location of Python, make sure to enter /usr/bin/python3,
# otherwise it will use Python 2.x. For the rest of the questions, I just pressed
# enter to accept the defaults.
$ ./configure

# Build TensorFlow:
$ bazel build //tensorflow/tools/pip_package:build_pip_package
$ ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# Install the package:
$ pip3 install --user /tmp/tensorflow_pkg/tensorflow-2.0.0-cp36-cp36m-linux_x86_64.whl

# Try it!
$ mkdir ~/tmp
$ cd ~/tmp
$ cat > hellotf.py <<'EOF'
#!/usr/bin/env python3

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
EOF
$ python3 hellotf.py
@paapu88
Copy link

paapu88 commented Nov 11, 2019

Your instructions are very good and clear.

This probably nothing related to bazel, but unfortunately I get

mka@mkaHemLap:~/git/tensorflow-2.0.0$ bazel build //tensorflow/tools/pip_package:build_pip_package
ERROR: /home/mka/git/tensorflow-2.0.0/tensorflow/core/kernels/BUILD:3669:1: C++ compilation of rule '//tensorflow/core/kernels:batch_matmul_op' failed (Exit 4)
gcc: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See file:///usr/share/doc/gcc-7/README.Bugs for instructions.
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 14149.609s, Critical Path: 314.20s
INFO: 4659 processes: 4659 local.
FAILED: Build did NOT complete successfully

Google did not know about this. Could it be the bazel version?

@philwo
Copy link
Author

philwo commented Nov 11, 2019

It looks like the compiler crashed. Can you see any messages from your kernel when running “dmesg” that would point to this? I can imagine that this error might happen due to running out of memory, overheating hardware or of course it could also be a software bug.

Depending on the cause we might be able to tweak the Bazel build invocation to make it work, e.g. by running less in parallel.

Feel free to send me the dmesg log via email (my email should be in my GitHub profile), happy to take a look.

@paapu88
Copy link

paapu88 commented Nov 12, 2019

Looks like your first guess was correct:

dmesg

[47935.821772] Out of memory: Kill process 10389 (cc1plus) score 247 or sacrifice child
[47935.821787] Killed process 10389 (cc1plus) total-vm:1562684kB, anon-rss:1495848kB, file-rss:0kB, shmem-rss:0kB

@Sarvagya2009
Copy link

On entering sudo chmod +x /usr/local/bin/bazel, I get the following error:

chmod: missing operand after '+x/usr/local/bin/bazel'.

Any idea why this happens?

@philwo
Copy link
Author

philwo commented Apr 14, 2020

@Sarvagya2009 The error message sounds like you’re missing a space between the “+x” and the “/usr/local/bin/bazel” parts.

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