How To Install TensorFlow on M1 Mac (The Easy Way)

Prabhat Kumar Sahu
4 min readJun 13, 2021
Tensorflow

Last year in November 2020 apple releases their first ARM64-based M1 chip. It got a lot of attention from everyone.

Being a tech enthusiast and a programmer, I was amazed to see the performance of the new apple M1 chip. The benchmarks were really good.

Recently only some months back, I bought myself an M1 Macbook Pro with 8Gigs of RAM and 512GB of SSD.

I wanted to set up and test how machine learning frameworks are working in this new chip. Here are the setup instructions for Tensorflow.

At this time of writing Apple has released its Tensorflow 2.5 for Apple Silicon.

It’s very easy to set up.

Before jumping into, I hope Homebrew is already installed in your system if not you can install it by running the following in your terminal

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Here are the things that we are going to do.

  • Step 1: Xcode Command Line Tools
  • Step2: Install Miniforge
  • Step3: Create a virtual environment with python3.8
  • Step4: Install Tensorflow 2.5 and its dependencies
  • Step5: Install Jupyter Notebook, Pandas
  • Step6: Run a Benchmark by training the MNIST dataset

Step1: Install Xcode Command Line Tools

I have already installed Xcode Command Line Tools on my mac. If it’s not already installed in your system, you can install it by running the following command below in your terminal.

xcode-select --install

Step2: Install Miniforge

Install miniforge for arm64 (Apple Silicon) from miniforge GitHub.

Miniforge enables installing python packages natively compiled for Apple Silicon.

After the installation of miniforge, by default, it gives us one base environment. You can turn off the default base env by running

conda config --set auto_activate_base false

Step3: Create a virtual environment

Let’s create a virtual environment named mlp with python3.8 installed.

conda create --name mlp python=3.8

And activate it by running

conda activate mlp

Step4: Installing Tensorflow-MacOS

Install the Tensorflow dependencies:

conda install -c apple tensorflow-deps

Install base TensorFlow:

pip install tensorflow-macos

Install metal plugin:

pip install tensorflow-metal

Step5: Install Jupyter Notebook & Pandas

conda install -c conda-forge -y pandas jupyter

Step6: Run a Benchmark by training the MNIST dataset

Let’s install Tensorflow Datasets

pip install tensorflow_datasets

Make sure your conda environment is activated.

Let’s open a Jupyter Notebook and do the benchmark. In your terminal run

jupyter notebook

It will open a browser window

Create a new python3 notebook

Let’s first import TensorFlow and check

import tensorflow as tfprint("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

Let’s run a benchmark

I got the code snippet from

https://github.com/apple/tensorflow_macos/issues/25

Copy and paste this code below in the new notebook

%%timeimport tensorflow as tfimport tensorflow_datasets as tfdsprint("TensorFlow version:", tf.__version__)
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
tf.config.list_physical_devices('GPU')
(ds_train, ds_test), ds_info = tfds.load(
'mnist',
split=['train', 'test'],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
def normalize_img(image, label):
"""Normalizes images: `uint8` -> `float32`."""
return tf.cast(image, tf.float32) / 255., label
batch_size = 128ds_train = ds_train.map(
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(batch_size)
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)
ds_test = ds_test.map(
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_test = ds_test.batch(batch_size)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, kernel_size=(3, 3),
activation='relu'),
tf.keras.layers.Conv2D(64, kernel_size=(3, 3),
activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
# tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
# tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['accuracy'],
)
model.fit(
ds_train,
epochs=12,
validation_data=ds_test,
)

You will get the output below. The time to run may be different

It took a total of 1min 57s second to run 12 epochs with 128 batch size.

You can use any other code editor or any other way to do the test benchmark also.

Conclusion

As you can see Tensorflow is successfully installed in our system and we are able to run some code also.

If you have any questions, recommendations, or critiques, I can be reached via Twitter or via my mail. Feel free to reach out to me.

References:

--

--