Installing TensorFlow on the M1 Mac | by Wei-Meng Lee | Towards Data Science

If you are a Mac user, you probably have one of the latest machines running Apple Silicon. To utilize Apple’s ML Compute framework for native hardware acceleration on M1 Macs, you need to install Apple’s hardware-accelerated TensorFlow and TensorFlow Addons for macOS 11.0+.
This article will show how how to prepare your M1 Mac for developing deep learning projects using TensorFlow. In addition, I will show you how to install Jupyter Notebook and the various additional libraries you need for your deep learning projects.
Apple maintains a Github page at https://github.com/apple/tensorflow_macos/issues/153 detailing the instructions to install TensorFlow in a Conda enviroment. My article provides a step-by-step instructions to do that.
Important note. Before proceeding with the following instructions, make sure that you do not have Anaconda installed on your Mac. If you have one installed, simply remove the Anaconda folder from your Mac to uninstall it.
The first tool you need to install is Xcode, which is available from the Mac AppStore:
When you install Xcode, it will also install the Command Line Tools. But if you have not installed it yet, the following command will do it:
$ xcode-select --install
Instead of using Anaconda, you will use MiniForge, a lightweight Python interpreter with full access to the Conda ecosystem.
Conda is an open source package and environment management system that runs on Windows, Mac OS and Linux.
You can download Miniforge from: https://github.com/conda-forge/miniforge.
For the M1 Mac, click the Miniforge3-MacOSX-arm64 item (second last in the list above). Once it is downloaded, launch Terminal and go to the directory that contains the Miniforge3-MacOSX-arm64.sh file and then use the bash command, like the following, to install Miniforge:
$ bash Miniforge3-MacOSX-arm64.sh
You will be prompted to accept the licensing agreements. Just accept them and proceed with the installation. After the installation is done, close Terminal.
In a new Terminal window, launch conda by typing:
$ conda
You will see the following:
To install TensorFlow on Apple’s M1 machines, first download the environment.yml file from https://raw.githubusercontent.com/mwidjaja1/DSOnMacARM/main/environment.yml.
This file contains the instructions to create a Python environment with the dependencies you need. To create a new environment for TensorFlow, change to the directory containing the environment.yml file:
$ cd Downloads
Then, type the following command:
$ conda env create --file=environment.yml --name tf_m1
The tf_m1 is the new environment name that I have chosen. Feel free to change it to your own desired name. You can now activate the newly created environment:
$ conda activate tf_m1
The list of Apple’s TensorFlow libraries are available at: https://github.com/apple/tensorflow_macos/releases:
At the time of writing, there are currently three versions available:
v0.1alpha3
v0.1alpha2
v0.1alpha1
Selecting the latest release (v0.1aplha3) will lead you this this page: https://github.com/apple/tensorflow_macos/releases/tag/v0.1alpha3:
Observe and copy the links for the two files:
tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl — https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl — https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
In the tf_m1 environment, you can now install the above two libraries using the following command:
$ pip install --upgrade --force --no-dependencies https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
Once the two libraries are installed, you can test out TensorFlow by launching Python and then importing TensorFlow and printing out its version number:
If the above succeeds, your TensorFlow is now installed correctly on your M1 Mac.
Besides installing the TensorFlow libraries, you would very likely need to install other libraries like matplotlib, sklearn, OpenCV, Pandas, etc. So go ahead and install them as follows:
$ conda install -c conda-forge matplotlib -y$ conda install -c conda-forge scikit-learn -y$ conda install -c conda-forge opencv -y$ conda install -c conda-forge pandas -y
The -y option saves you the trouble of answering the confirmation when installing the libraries.
Finally, you would want to install Jupyter Notebook:
$ conda install notebook -y
That’s it! Test if Jupyter Notebook is installed correctly:
$ jupyter notebook
If you can see the following, you are all set!
If you try to load a saved model:
from tensorflow.keras.models import load_modelmodel = load_model('trained_model.h5')
You may encounter the following error:
AttributeError: 'str' object has no attribute 'decode'
Turns out that internally the load_model() is using the newer version of the h5py module. To fix this, you can downgrade the version of h5py to version 2.10.0 using the following command:
$ pip install 'h5py==2.10.0' --force-reinstall
Have a great time with TensorFlow on the M1 Mac!