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

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

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.

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

Step3: Create a virtual environment

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

And activate it by running

Step4: Installing Tensorflow-MacOS

Install the Tensorflow dependencies:

Install base TensorFlow:

Install metal plugin:

Step5: Install Jupyter Notebook & Pandas

Step6: Run a Benchmark by training the MNIST dataset

Let’s install Tensorflow Datasets

Make sure your conda environment is activated.

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

It will open a browser window

Create a new python3 notebook

Let’s first import TensorFlow and check

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

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:

--

--

{ ML Engineer 💻 }

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store