Install TensorFlow on your Mac M1/M2/M3 with GPU Support

I recently moved from an Intel based processor to an M1 apple silicon Mac and had a hard time setting up my development environments and tools, especially for my machine learning projects, I was particularly exited to use the new Apple Silicon ARM64 architecture and benefit from the GPU acceleration it offers for my ML tasks. Well after some digging online and finally setting up it up i came up with this small article to help anyone out there in need. In this article, I will show you how to install TensorFlow in a few steps and run some simple examples to test the performance.

Step 1: Install Xcode Command Line Tool

Xcode is a software development tool for macOS that includes a compiler, debugger, and other tools. You will need to install it before installing TensorFlow. To do so, open your terminal and run the following command:

xcode-select --install

Step 2: Install the M1 Miniconda or Anaconda Version

Miniconda is a minimal version of Anaconda, a popular Python distribution that comes with many data science packages. You need to download and install Miniconda3 macOS M1 64-bit.pkg version from the official website: https://docs.conda.io/en/latest/miniconda.html

After installing Miniconda, you can then create a virtual environment for TensorFlow with the following command:

conda create -n tf python=3.10.13 ## or whatever version of python you want

Then, activate the environment with:

conda activate tf

Step 3: Install TensorFlow

Now, you are ready to install TensorFlow and its dependencies. First, install the TensorFlow dependencies with:

conda install -c apple tensorflow-deps

Then, install the base TensorFlow package with:

pip install tensorflow-macos

Note: Make sure you are installing this in your newly created python environment

Finally, install the Metal plugin, which enables TensorFlow to use the GPU on your Mac:

pip install tensorflow-metal

Step 4: Install Jupyter Notebook and common packages

Jupyter Notebook is an interactive web-based environment that allows you to write and run Python code in your browser. It is very useful for data analysis and visualization. To install it, run:

conda install notebook -y

Now, you will need to install and upgrade some common packages for data science so that they are updated to the M1 architecture, packages such as numpy, pandas, matplotlib, scikit-learn, scipy, and plotly. To do so, run:

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

Step 5: Check GPU availability

To verify that TensorFlow can use the GPU on your Mac, you can run the following code in a Jupyter Notebook cell:

import sys
import 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: {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")

If everything is working correctly, you should see something like this:

Python Platform: macOS-14.2.1-arm64-arm-64bit
Tensor Flow Version: 2.15.0
Keras Version: 2.15.0

Python 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:35:25) [Clang 16.0.6 ]
Pandas 2.1.4
Scikit-Learn 1.3.2
SciPy 1.11.4
GPU is available

Conclusion

Congratulations, you have successfully installed TensorFlow on your new Mac M1/M2/M3 with GPU support! You can now use TensorFlow to build and train your own machine learning models and enjoy the speed of the Apple Silicon architecture. Happy coding!

10
Subscribe to my newsletter

Read articles from Fotie M. Constant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fotie M. Constant
Fotie M. Constant

Widely known as fotiecodes, an open source enthusiast, software developer, mentor and SaaS founder. I'm passionate about creating software solutions that are scalable and accessible to all, and i am dedicated to building innovative SaaS products that empower businesses to work smarter, not harder.