Tutorial : TensorFlow on an M1 Mac using Jupyter notebooks and Miniforge

Clayton Pilat
5 min readJan 19, 2021

If you’re like me and you got a brand new M1 Mac over the holidays and are planning to use it for machine learning, there are a few hurdles you need to jump through in order to get it to run TensorFlow and other Python libraries. However, the work is worth it. Once I got everything up and running I was able to see an increase in training speed of over 80%. This tutorial uses the majority of steps from AI Buzz’s article here, however, there are a few mistakes and additional steps that I needed to do to get it to work properly. So I’ve amalgamated my research into this one article below.

Assumptions

If you’re a data scientist or developer I’m going to assume that you already first set these up when you got your Mac. If not, below are some guides on how to install them.

Step 1: Installing Miniforge

I’m accustomed to using Miniconda when working on different data science projects, however, in order to get specific packages to work, we need to use Miniforge. The link can be found here: https://github.com/conda-forge/miniforge . Select the download for ‘arm64 (Apple Silicon)’ architecture.

Miniforge github repo.

Once its downloaded, run a bash program to install Miniforge. The code can be viewed below.

bash Miniforge3-MacOSX-arm64.sh

Keep pressing return to read through the licensing and terms, and type yes and press return when prompted. Once this is complete, close your terminal and reopen it. Write into the terminal:

conda

If you followed through the install correctly you will see the list of conda commands in your terminal like shown below.

Step 2: Setting up environment

Now miniforge is set up we need to create an environment to use. You can swap out TensorFlow for any other name you want to use.

conda create --name tensorflow python=3.8

When prompted type y and press return to continue. Once its finished creating the environment activate it using:

conda activate tensorflow

and your terminal should now be prefixed with (tensorflow) instead of (base).

Step 3: Downloading and installing required packages

Now that the environment is ready to go we need to install the MacOS M1 specific packages to get TensorFlow running.

Head to : https://github.com/apple/tensorflow_macos/releases/tag/v0.1alpha1 and download the tar file. Double check that you are downloading the latest version.

Unzip the file, either using an unarchiver program or using the terminal. Open the unzipped file and copy the arm64 path.

Then type into the terminal :

libs="$YOURCOPIEDPATH"

Next we need to set the environment to use. This should already be done, but lets set it again to be safe. Type into the terminal:

conda env list

And copy the path of your tensorflow project. Once you’ve copied this, type into the terminal:

env="$YOURCOPIEDPATH"

Install these packages next by coping and pasting these into the terminal (DO THESE ONE AT A TIME DO NOT COPY AND PASTE THE WHOLE BLOCK):

conda install cached-propertyconda install sixpip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl"pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl"pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_addons-0.11.2+mlcompute-cp38-cp38-macosx_11_0_arm64.whl"conda install -c conda-forge -y absl-pyconda install -c conda-forge -y astunparseconda install -c conda-forge -y gast
conda install -c conda-forge -y opt_einsum
conda install -c conda-forge -y termcolorconda install -c conda-forge -y typing_extensionsconda install -c conda-forge -y wheelconda install -c conda-forge -y typeguardpip install wrapt flatbuffers tensorflow_estimator google_pasta keras_preprocessing protobufpip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_macos-0.1a1-cp38-cp38-macosx_11_0_arm64.whl"pip install tensorboard

Troubleshooting and common errors

grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform.

For some reason the version of Python that you download from the website for MacOS doesnt seem to like these files, so uninstall your current version of Python (if you dont know how to do that click here). And then install the Python from xcode by pasting this into your terminal.

xcode-select --install

conda command not found

I had this error because I didn't properly uninstall MiniConda. Edit the .zshc file and remove the conda config and reinstall miniforge to create the new config.

nano ~/.zshrc

Step 4: Additional packages

Matplotlib

When trying to install Matplotlib I get this error,

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

In order to fix this, you can use the command:

brew install libjpeg

and then,

pip install matplotlib

Pandas

pip install pandas

Numpy

pip install numpy

Sci-kit learn

For some reason pip didnt work for installing sklearn packages, so I had to do this through conda forge.

conda install -c conda-forge scikit-learn

Keras

pip install keras

Jupyter Notebook

pip install notebook

Step 5: Conclusion

Once you have installed everything you should be able to write into the terminal:

jupyter notebook

It will open up a new window/tab in your preferred browser.

Type in import tensorflow, and it should work!

--

--