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).

 

Monday, September 15, 2014

Getting your local user site available through native Apache on OS X (10.9,10.10)

Edit the following files to get your local user site working:

In http.conf, uncomment the following lines:

LoadModule userdir_module libexec/apache2/mod_userdir.so

# User home directories
Include /private/etc/apache2/extra/httpd-userdir.conf

In the following file  - "/private/etc/apache2/extra/httpd-userdir.conf", uncomment the following line:

Include /private/etc/apache2/users/*.conf

In your /private/etc/apache2/users directory, create a file with the syntax "your_username.conf". Note to replace the "your_username" with your actual username.

In this file, add the following entries (note again to change the "your_username" entry).
<directory "/Users/your_username/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
    #Order allow,deny
    #Allow from all
</directory>


This is the quickest way to expose your local Sites directory through Apache. You can now access your local directory through a url: http://localhost/~your_username/

You can further refine access rights, but this is the quickest way to get the behavior of user account web sharing back into OS X.