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:
We’ll need to prepend our $PATH
variable so Homebrew installed programs are used before any system installations.
It never hurts to verify that an installation went smoothly before proceeding, so restart terminal or source ~/.bash_profile
and enter the following:
Now we can install Python:
To verify our installation:
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.
Installing Virtualenv
Now we have to create the virtual environment in which TensorFlow will run. First, let’s 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:
Finally, to create the virtual environment:
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:
Installing TensorFlow
Now that we’re inside our active virtual environment, it’s finally time to 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:
Fire up a new terminal shell and activate the virtual environment again using the “source” command from earlier. You should see "(
If it is, go ahead and invoke 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:
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.