Building TensorFlow on macOS (CPU)

3 Jun '17

Why would you do this? pip install tensorflow works fine!

That’s true. But the standard package ships without SSE4.1, SSE4.2, and AVX instructions. And when you’re running a mid-2012 Macbook Air, you want all the optimisations you can get. The TensorFlow installation docs are pretty good! This is pretty much a straight crib from the docs. Building TensorFlow takes a while though, on my underpowered machine it took over 1.5 hours.

$ brew install bazel
$ mkdir <project>
$ cd <project>
$ virtualenv -p python3 env
$ source env/bin/activate
$ pip install six wheel numpy
$ cd ..
$ git clone --depth 1 [--branch r1.0]
$ cd tensorflow
$ ./configure
$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
INFO: Elapsed time: 5635.340s, Critical Path: 4545.85s
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ pip install /tmp/tensorflow_pkg/tensorflow-1.2.0rc1-cp36-cp36m-macosx_10_12_x86_64.whl

Before you test your custom build of TensorFlow, change the directory! If you try and run Python from the tensorflow repository, you’ll get a weird error when you import TensorFlow (StackOverflow):

>>> import tensorflow as tf
Traceback (most recent call last):
  File "/Users/tobywf/Documents/tensorflow/tensorflow/python/", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
ModuleNotFoundError: No module named 'tensorflow.python.pywrap_tensorflow_internal'

From any other directory, it’ll work fine:

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(
b'Hello, TensorFlow!'

macOS, Python

Newer Older