Skip to content

Instantly share code, notes, and snippets.

@umbertogriffo
Last active July 20, 2022 11:54
Show Gist options
  • Save umbertogriffo/e3b44bd0c8e1e13d6e27fcf374a25076 to your computer and use it in GitHub Desktop.
Save umbertogriffo/e3b44bd0c8e1e13d6e27fcf374a25076 to your computer and use it in GitHub Desktop.
MacOS 12 M1 (Apple Silicon) - Installs Tensorflow 2.9.1

This installs Tensorflow under M1 (Apple Silicon)

Note

pyenv has to be installed from Github https://laict.medium.com/install-python-on-macos-11-m1-apple-silicon-using-pyenv-12e0729427a9

Table of contents

Introduction

The new Mac M1 contains CPU, GPU, and deep learning hardware support, all on a single chip. To leverage the new M1 chip from Python, you must use a special Python distribution called Miniforge that will maximize your performance.

If you don't install it on Miniforge you will get the following error:

Solution

# Install Miniforge
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(brew --prefix zlib)/include -I$(brew --prefix sqlite)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib  -L$(brew --prefix sqlite)/lib" pyenv install miniforge3-4.10.3-10
# it returns something like:
# Installed Miniforge3-4.10.3-10-MacOSX-arm64 to /Users/umberto.griffo/.pyenv/versions/miniforge3-4.10.3-10

pyenv global miniforge3-4.10.3-10

conda run which python
# it returns something like:
# /Users/umberto.griffo/.pyenv/versions/miniforge3-4.10.3-10/bin/python

conda update --force conda -y

# Create a virtual environment
conda create --name mlp python=3.8
# Initialize the shell to use `conda activate` (bash, zsh or whatever)
conda init zsh

# Activate it by running
conda activate mlp

# Install TensorFlow
conda install -c apple tensorflow-deps==2.9.0 -y
python -m pip install tensorflow-macos==2.9.1
python -m pip install tensorflow-metal==0.5.0

# Check TensorFlow Version
python -c 'import tensorflow as tf; print(tf.version.VERSION)'

Pipenv and Conda

To create a Pipenv environment where to install the dependencies starting from a Conda's Environment, from the Pycharm terminal run:

# Activate the environment
conda activate mlp

# Setup Pipenv to use Conda's Python executable and site packages directory
pipenv --python=$(conda run which python) --site-packages

# Install the dependencies
pipenv install --dev

How do I prevent Conda from activating the base environment by default?

Disable:

conda config --set auto_activate_base false

Enable:

conda config --set auto_activate_base true

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment