Getting TensorFlow install on Apple M1

Getting TensorFlow install on Apple M1

I started into astronomy datasets and wanted to get acquainted with TensorFlow first. On an Apple M1 I wanted TensorFlow to use the integrated libraries, so that the datasets run natively on Apple’s Metal framework and do not fall back to a slow translated path. These are my scratch notes from setting that up. I have cleaned them up and corrected three things, which I flag as I reach them.

If you came here to get astroNN running, my later post covers that setup: AstroNN + TensorFlow on Apple Silicon: A Setup That Works. The two posts overlap on the install, and the later one is the better starting point for astroNN.

What this gets you

A Python environment that runs on the M1’s native arm64 architecture, TensorFlow installed in it, and a check that TensorFlow sees the GPU through Metal. The check matters because a wrong install works and runs on the CPU only, so nothing fails. It just trains slowly.

Step 1: Xcode Command Line Tools

Install the command line tools:

xcode-select --install

This gives you the compilers and headers that some Python packages need when they have no prebuilt wheel for your machine.

Step 2: Miniforge for arm64

Install Miniforge for arm64 from the Miniforge GitHub releases. It is a small conda distribution that defaults to the conda-forge channel and ships arm64 builds. It creates one base environment.

Correction number one. My notes said to turn off the default base environment with conda config --set auto_activate_base true. That command does the opposite. Setting the value to true makes the base environment activate in every new shell. To turn automatic activation off, run:

conda config --set auto_activate_base false

I prefer it off, because I want every project in its own named environment and no stray packages in base.

Step 3: TensorFlow

My original notes said “Tensorflow 2.5 and dependencies” and used Apple’s conda channel for the dependencies:

conda install -c apple tensorflow-deps
pip install tensorflow-macos
pip install tensorflow-metal

Correction number two. TensorFlow 2.5 was never the current version by the time I published these notes, and it is not current now. The tensorflow-deps and tensorflow-macos pair was Apple’s route for the early M1 releases. Treat the three commands above as a record of what I ran, and check the current instructions before you copy them.

Here is what I could confirm on the plugin’s own page. The tensorflow-metal package, version 1.2.0, was released on 2025-01-31. It supports CPython 3.9 to 3.12 on macOS 12 or later and on ARM64. Its page says to install the base tensorflow package together with tensorflow-metal. Its compatibility table lists TensorFlow 2.14 to 2.18, against plugin versions up to 1.1.0.

Two points I could not confirm. First, the last tensorflow-macos release on PyPI is 2.16.2, from 2024-06-28, so that package no longer follows current TensorFlow. Second, current TensorFlow uses Keras 3, and I have not confirmed whether tensorflow-metal 1.2.0 works with Keras 3. Test it before you build on it.

The route I would try today, which I have not run, is:

python3.11 -m venv tfm
source tfm/bin/activate
pip install tensorflow tensorflow-metal

Python 3.11 sits inside the plugin’s stated 3.9 to 3.12 range.

Step 4: Pandas and Jupyter

In a conda environment, install the data tools from conda-forge:

conda install -c conda-forge -y pandas jupyter

In a plain virtual environment, use pip install pandas jupyter instead.

Step 5: Check the GPU

Before any benchmark, run this in Python:

import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))

My original notes printed the number of GPUs with the older tf.config.experimental.list_physical_devices('GPU') call and len(). The shorter call above is the current spelling. Expected value: a list with one device on an M-series Mac, for example [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]. I have not run this on a current install. An empty list means TensorFlow found no GPU and will use the CPU.

Step 6: A benchmark on MNIST

To compare setups, I trained a small model on MNIST. Install tensorflow_datasets with pip, activate the environment, start jupyter notebook, and open a new Python 3 notebook. Import TensorFlow, print the GPU check from the previous step, and run a short training loop on the MNIST data.

The code snippet I used came from a TensorFlow issues thread on GitHub. I did not paste it here, because it belongs to its author and the thread is the right place to get it with its context.

Run the benchmark twice if you can. The first run has your install as you built it. The second runs with the GPU hidden from TensorFlow, which you can do by setting the environment variable CUDA_VISIBLE_DEVICES to an empty string on systems where it applies, or by calling tf.config.set_visible_devices([], 'GPU') before any other TensorFlow call. Expected result: the GPU run finishes the epoch faster than the CPU-only run. How much faster depends on the model and batch size, so I am not quoting a figure.

Troubleshooting notes

If pip install tensorflow-macos fails to find a matching version, you are probably on a Python version the package does not support. Check python --version against the plugin’s supported range first.

If the GPU list is empty, confirm that tensorflow-metal installed in the same environment you are running. Run pip list and look for both packages.

If importing TensorFlow raises an error about Keras, you are likely mixing a TensorFlow release with a Keras release it does not expect. Install the versions together and do not upgrade one on its own.

Why Metal

Metal is Apple’s graphics and compute framework. TensorFlow does not talk to the M1 GPU directly. The tensorflow-metal plugin translates TensorFlow’s operations into Metal calls. That is why the plugin’s compatibility table matters more than TensorFlow’s own release notes, and why a TensorFlow version newer than the table lists is a risk and not an upgrade.

Subscribe to Just another Geek

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe