Building and Installing TensorFlow from Source on Your Mac

This guide walks through building and installing the TensorFlow binary from source on a macOS system. I am publishing this as a reference for myself but hopefully others might find it useful.

This procedure was tested with the following assumptions:

  • macOS High Sierra
  • Intel Core i7 Processor
  • TensorFlow 1.4.1
  • Homebrew 1.3.8
  • Python 3.6.2
  • JDK 8u152
  • Bazel 0.8.1

Note that the resulting binary we build will be optimized for CPU support only.

Installing Bazel on macOS

Install Java Development Kit

At the time of publication, Bazel required the Java Development Kit 8.0. If it is not already installed follow these steps.

  1. Download the DMG image that containers the JDK installer from Oracle's Java SE Development Kit 8 Downloads page.
  2. The file should be named something like jdk-8u152-macosx-x64.dmg.
  3. Mount the DMG image, double-click the installer, and follow the guided prompts.
Install Bazel

This procedures assumes you are using Homebrew. You can refer to the Bazel documentation for alternatives.

  1. From a Terminal, execute brew install bazel to install Bazel using Homebrew.
  2. Confirm Bazel is installing by verifying the output of bazel version.

Install Python Dependencies

It is strongly recommended that you use a virtualenv or Conda environment to isolate the packages and installation of TensorFlow from source. This enables you to maintain multiple combinations of package versions for various projects or experiments.

Since I use Anaconda, I created a new environment by executing the command conda create -n tfsrc python=3.6.

TensorFlow requires the following Python packages:

  • six
  • NumPy
  • wheel

If these are not present on your machine or current environment execute the following to install:

$ pip install six numpy wheel

Prepare the TensorFlow Installation

Clone the TensorFlow Repository

Clone the latest TensorFlow repository by issuing the following command:

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

This will take a short while depending on your Internet connection bandwidth due to the repository's size (~250 MB).

If you wish to install a release version rather than trunk you can display the available releases and check out one using the following command sequence (example is TensorFlow release 1.4):

$ git branch -l --remote
$ git checkout r1.4
Configure and Build the TensorFlow pip Package

I've posted a script that is based on the work of Sasha Nikiforov and a Stackoverflow post with a few minor modifications to support Anaconda environments. Here are the contents:

Clone the build_tf.sh script gist to the same directory containing the TensorFlow source:

$ curl -O https://gist.githubusercontent.com/frankhinek/20f8086d70886c56405fe4431f998ac4/raw/98c2ea1570d35bf20c0761390eb6d423ea387b02/build_tf.sh

Make the script executable:

$ chmod u+x build_tf.sh

Execute the script to configure and build the TensorFlow pip package using all of the CPU optimizations available for your CPU:

$ ./build_tf.sh

During the TensorFlow configuration process, accept all the defaults by pressing the Enter key repeatedly until the configuration is complete unless you have a specific need.

This build process will take some time to complete. In my experience it averages around 30 minutes, but a system with more than 16GB of RAM will compile faster.

The script will build a .whl file and store it in the /tmp/tensorflow_pkg directory.

Installing TensorFlow

The final step is to install the custom-built TensorFlow binary using the wheel file. The specific name of the while will vary depending on the version of TensorFlow and Python in your environment.

In my case, the command to install is:

$ pip install /tmp/tensorflow_pkg/tensorflow-1.4.1-cp36-cp36m-macosx_10_7_x86_64.whl
Verify Installation

The last step is to confirm that TensorFlow is now installed and working as expected. Before proceeding be sure that you change to a directory other than the one that contains the TensorFlow source tree. We want to import the pip installed package and not the one in the source directory.

Launch the python interactive shell and execute the following commands:

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

If the system outputs the following, then TensorFlow is installed and working:

Hello, TensorFlow!