Mastodon

Installing TensorFlow on macOS Sierra

After deciding to start diving into the wonderfully weird world of machine learning and artificial neural networks, I looked into a few libraries to try out, and ultimately chose TensorFlow, an open-source library developed by Google Brain for machine learning and deep neural network research. Perfect! TensorFlow is a Python library, and comes in two flavors: CPU-only support or GPU support. TensorFlow and most neural nets run far more efficiently on GPUs thanks to the parallel architecture of graphics processors. It’s recommended to first attempt a vanilla install of TensorFlow, before adding GPU support, so we’ll start there.

Of the variety of avenues available to install TensorFlow, virtualenv is recommended, as it isolates the installation to a virtual environment, preventing other Python programs from affecting it or being affected themselves. TensorFlow is available for both Python 2 and 3, and although Python 3 is faster, while experimenting with it I found myself experiencing more bugs than when using Python 2, such as string literals being expressed as byte literals, among other syntax errors.

Installing Python

So, if you already have your preferred version of Python installed, feel free to skip this section. Otherwise, let’s start with checking GCC. Command Line Tools installs the needed GCC complier. To install Python, you can either download it directly, or use Homebrew, an open-source package manager. To install Homebrew, fire up Terminal and enter the following:

$
/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

We’ll need to prepend our $PATH variable so Homebrew installed programs are used before any system installations.

$
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

It never hurts to verify that an installation went smoothly before proceeding, so restart terminal or source ~/.bash_profile and enter the following:

$
brew doctor

Now we can install Python:

$
brew install python

To verify our installation:

$
which python

You should see an output referencing /usr/local/bin/python.

Pip is the default package management system for Python, and we’ll be using it to install virtualenv. Pip and setuptools come preinstalled with Python 2.7 and above, but we’ll want to make sure they’re up to date.

$
pip install --upgrade pip setuptools

Installing Virtualenv

Now we have to create the virtual environment in which TensorFlow will run. First, let’s install virtualenv:

$
pip install virtualenv

Next we’ll make a directory for the virtual environment and TensorFlow install to live in. I just made a folder named “TensorFlow” in my home folder:

$
mkdir TensorFlow

Finally, to create the virtual environment:

$
virtualenv -–system-site-packages ~/TensorFlow

The last argument of this command is the actual path to the folder you’ve created. Our next step is to activate the virtual environment we’ve installed:

$
source ~/TensorFlow/bin/activate

Installing TensorFlow

Now that we’re inside our active virtual environment, it’s finally time to install TensorFlow:

$
pip install tensorflow

After TensorFlow has successfully installed, we’re going to, you guessed it! Validate the installation. Close out of your virtual environment by simply entering the following command:

$
deactivate

Fire up a new terminal shell and activate the virtual environment again using the “source” command from earlier. You should see "()" preceding the usual terminal prompt, indicating it’s active.

If it is, go ahead and invoke Python:

$
python

Try this short “Hello, TensorFlow!” program:

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

If all went well, you should see:

$
Hello, TensorFlow!

During my execution of this test program, the compiler warned of multiple libraries that weren’t compiled that could speed up CPU computation if they were however, building TensorFlow from Source is the only way to compile these libraries. For more information about installation and troubleshooting, check out TensorFlow’s installation guide.