Install TensorFlow on Mac M1/M2 with GPU support

Dennis Ganzaroli
5 min readSep 2, 2022

--

Install TensorFlow in a few steps on Mac M1/M2 with GPU support and benefit from the native performance of the new Mac ARM64 architecture.

Fig 01: Apple M1 (image by Apple)

Why use a Mac M1/M2 for Data Science and Deep Learning?

What makes the Macs M1 and the new M2 stand out is not only their outstanding performance, but also the extremely low power consumption.

Fig 02: Apple M2 (image by Apple)

1. Low Power Consumtion

The Mac Mini M1 has a maximum power consumption of 39 W, while a normal gaming PC tower consumes over 50 W when idle and between 150 W and 300 W under peak load.

Fig 03: Mac Mini M1 power consumption (image from Apple)

In a world where energy consumption is becoming more critical every day, efficient use of resources must also be a priority.

2. Powerful CPU

However, a strong CPU is also essential for Data Science tasks, and for Deep Learning you also need a powerful GPU.

Let’s start to check the CPU performance of the M1. Since I am working with a Macbook Air M1 Late 2020, I will check its Geekbench 5 benchmarks.

My Macbook Air scores 1734 on Single-Core and 7572 on Multi-Core.

Fig 04: Macbook Air Geekbench 5 Scores (image by author)

Its performance is comparable with an Intel i7 12th gen or an AMD Ryzen 9.
However, the i7’s 12th gen peak power consumption is 190 W and the AMD Ryzen 9 also reaches 168 W.

Fig 05: CPU Geekbench Scores (image by Geekbench)

For normal Data Science tasks, my Macbook Air is on equal level with the powerful i7 and AMD Ryzen processors, but only needs a quarter of the power consumption for the same performance.

3. A dedicated GPU

But what about GPU power, which is essential for Deep Learning tasks?
In this regard, we know that nVidia graphics cards are the measure of all things and the Macs M1 do not have an nVidia GPU built-in.

However, the Macs’ M1 chips have an integrated multi-core GPU. Depending on the M1 model, the following number of GPU cores are available:

M1: 7- or 8-core GPU
M1 Pro: 14- or 16-core GPU
M1 Max: 24- or 32-core GPU
M1 Ultra: 48- or 64-core GPU

Apple claims the new Macs M1s have CPU, GPU and Deep Learning hardware support on a single chip. But how do these M1 GPU’s perform in Deep Learning tasks?

Since these processors have a completely new architecture, the corresponding software had to be adapted. In the beginning, Data Science platforms like Anaconda ran on the Rosetta emulator, which performed quite well but was not natively compiled for the Apple M1’s ARM64 architecture.

The latest version of Anaconda Distribution finally offers native compilation for the ARM64 architecture of Apple Macs M1 and M2, allowing up to 20% faster computing performance.

The installation steps have been simplified from version to version, but are still not as straightforward as with Intel or AMD processors.

However, with the following instructions, we will have Tensorflow installed and ready to use in just a few minutes.

Installation of Tensorflow with GPU support

Here are the things that we are going to do.

  1. Install Xcode Command Line Tool
  2. Install the M1 Miniconda Version
  3. Install Tensorflow
  4. Install Jupyter Notebook and common packages

1. Install Xcode Command Line Tool

If it’s not already installed in your system, you can install it by running the following command below in your Mac OSX terminal.

xcode-select --install

2. Install the M1 Miniconda Version

Miniconda is the minimal set of features from the extensive Anaconda Python distribution and includes many of the data science related packages that are needed by this class.
Download the Miniconda3 macOS Apple M1 64-bit.pkg from here and install it on your Application directory.

Fig 06: Miniconda3 site (image from Miniconda)

3. Install Tensorflow

Change to the Application/miniconda3 directory in your terminal with:

cd /Applications/miniconda3

In some cases you have to change to the opt directory with:

cd /opt/miniconda3

Install the Tensorflow dependencies:
if the dependencies are not installed jump to the next section

conda install -c apple tensorflow-deps
Fig 07: Installing Tensorflow dependencies (image by author)

Install base Tensorflow:

pip install tensorflow-macos

Install Metal plugin:

pip install tensorflow-metal

4. Install Jupyter Notebook and common packages

Install first Jupyter Notebook:

conda install notebook -y

Now install common additional packages and upgrade the packages so that they are updated to the M1 architecture.

pip install numpy  --upgrade
pip install pandas --upgrade
pip install matplotlib --upgrade
pip install scikit-learn --upgrade
pip install scipy --upgrade
pip install plotly --upgrade

Start now Jupyter Notebook in your desired working directory (change “/Users/Jupyterfiles” with your working directory path)

jupyter notebook --notebook-dir="/Users/Jupyterfiles"

Please note that macOS M1 does not support Qt yet — Anaconda Navigator and Spyder will not be available. Please check back for updates.

5. Check GPU availability

Check the Python version and the GPU availability with this code:

import sysimport tensorflow.keras
import pandas as pd
import sklearn as sk
import scipy as sp
import tensorflow as tf
import platform
print(f"Python Platform: {platform.platform()}")
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
print(f"SciPy {sp.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")

As you can see the installed Python platform is “macOS-12.5-arm64-arm-64bit” and so ready for the M1 architecture.
And what is even more important, the GPU is now directly supported.

Fig 08: Checking Python version and GPU availability (image by author)

Thanks for reading and may the Data Force be with you!
Please feel free to share your thoughts or reading tips in the comments.

If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member.
It’s $5 a month, giving you unlimited access to thousands of Data science articles. If you sign up using my link, I’ll earn a small commission with no extra cost to you.

Follow me on Medium, LinkedIn or Twitter
and follow my Facebook Group “
Data Science with Yodime

Material for this project:
GitHub: Install TensorFlow on Mac M1 GPU

--

--

Dennis Ganzaroli
Dennis Ganzaroli

Written by Dennis Ganzaroli

Data Scientist with over 20 years of experience. Degree in Psychology and Computer Science. KNIME COTM 2021 and Winner of KNIME Best blog post 2020.

Responses (15)