Skip to content

Instantly share code, notes, and snippets.

@JohnLyu94
Forked from philwo/README.md
Created November 9, 2020 03:48
Show Gist options
  • Save JohnLyu94/b8167ea5f0816137ef05cc5f8f9cef04 to your computer and use it in GitHub Desktop.
Save JohnLyu94/b8167ea5f0816137ef05cc5f8f9cef04 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment