Thursday, December 31, 2020

Installing Python and TensorFlow on MacOS Big Sur

This is a pretty quick tutorial on how to install the latest version of Python on MacOS, currently Big Sur, and then leverage the GPU for TensorFlow for your ML workloads. In case you have an M1 Mac,  this will turn out to be your optimal way of running your ML loads locally.


Installing Python the correct way on MacOS

The best way to install python on MacOS is through pyenv. You can install pyenv via Brew

        brew install pyenv

Once you have pyenv installed, make sure to install Xcode tools as follows:

xcode-select --install 

You are now ready to install any version of Python. Given that we want to leverage TensorFlow, you will need to install a 3.8 version of Python ( the latest 3.9.0 version of python doesn't seem to work with the Apple fork of TensorFlow ).

export LDFLAGS="-L$(xcrun --show-sdk-path)/usr/lib" 

pyenv install 3.8.6


Add the following lines into your .bashrc or .zshrc:


eval "$(pyenv init -)"

eval "$(pyenv virtualenv-init -)"


That's it. You should be all set with Python 3.8.6. To leverage this version, you need to do one more thing at the command prompt:

pyenv global 3.8.6


Issues installing pyenv


If you're having issues installing pyenv, try the following command:

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.8.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)


Installing TensorFlow


Apple's fork of TensorFlow leverages the Mac GPUs instead of relying on the CUDA NVidea drivers. To install this forked project:

bash -c "$(curl -fsSL https://raw.githubusercontent.com/apple/tensorflow_macos/master/scripts/download_and_install.sh)"

Make sure to activate your new python virtual environment for TensorFlow(depending upon where you have that directory setup):

. ./bin/activate

While running your code, you can look at the Mac Activity Monitor window to confirm that the GPU is getting leveraged ( Cmd+4).