Installing Tensorflow on Apple Silicon

Yashowardhan Shinde
Geek Culture
Published in
3 min readOct 20, 2022

Although a lot of content is present about the installation of Tensorflow on the new ARM-powered Mac, I still struggled to set up my Tensorflow environment on the Macbook Air M1. So I'm writing this blog to walk you through an easy installation guide that never fails me and can help you set up your Tensorflow environment within minutes every time. So let's get started!

The contents of this blog are as follows:

  1. Can you use TensorFlow with GPU support on Apple Silicon?
  2. Requirements to set up Tensorflow with GPU support on Mac
  3. Step-by-step guide for installing TensorFlow with GPU support.

Can you use TensorFlow GPU on Apple Silicon?

Early on, I did not know that you could take advantage of the GPU cores in the new ARM chips introduced by Apple. I came across the official Apple guide for installing Tensorflow GPU on a Mac powered by the new Apple silicon, which they call TensorFlow-Metal. So yes, you can use TensorFlow with GPU support on Apple Silicon.

Requirements to set up Tensorflow with GPU support on Mac

So the basic requirement for installing TensorFlow-Metal is Miniforge. You can download Miniforge easily using the following steps:

  1. Download the Miniforge File from here.
  2. Use the commands as shown below:
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate

If this doesn't work for some reason, you can refer to this video by Dr. Jeff Heaton on YouTube. This video is well-made and explains the installation of Miniforge and Anaconda on Mac M1 in depth. Once you have Miniforge installed, you are good to go. I would highly recommend watching this video once.

Step-by-step guide for installing TensorFlow

Start by creating a new CONDA virtual environment and activating it. Use the commands below to do so.

conda create -n tf python=3.9conda activate tf

Now we need to install the TensorFlow-Metal packages one by one. These include TensorFlow-deps, TensorFlow-macOS, and TensorFlow-metal. To install these, you can run the following commands:

conda install -c apple tensorflow-depspython -m pip install tensorflow-macos==2.9python -m pip install tensorflow-metal

Once this is done, you are good to go. You can check if your installation was successful by using the following commands. You should be able to see the TensorFlow version and a GPU device when you use the tf.config.experimental.list_physical_devices('GPU') command.

Figure 1. Successful Installation Result

But now comes the tricky part. Many people will get an error at this stage, and 99% of the time, this is mainly because of the mismatch between the required and the installed NumPy version. The error could look like the one shown in Figure 2. The problem is that each TensorFlow version is built with a specific NumPy version.

Figure 2. The most common error after installation is completed

No need to worry. This error can be easily fixed by uninstalling NumPy and reinstalling the correct version.

conda remove numpyconda install -c conda-forge numpy=x.xx

Now there should be no problems with your TensorFlow, and you are finally done with the installation. Using the code snippet shown earlier in the blog, Figure 1, you can ensure that everything works properly. I hope this guide helps you and you will never have trouble installing TensorFlow again. Consider following me on Medium and connecting with me on LinkedIn!

Check out my other articles here!

--

--

Yashowardhan Shinde
Geek Culture

Computer Science Undergraduate, Machine Learning and Deep Learning Enthusiast.