avatarSophie Owen

Summary

The article provides a step-by-step guide on how to install TensorFlow on Mac M1 and M2 computers.

Abstract

The author of the article, a data scientist, shares their experience in overcoming the challenges of installing TensorFlow on a Macbook Pro with an M1 Pro chip. The process involves installing Xcode Command Line Tools, Miniconda, and creating a virtual environment. The guide addresses the lack of official support for TensorFlow on MacOS and the absence of GPU support for M1 and M2 chips by detailing the installation of a specialized version of TensorFlow optimized for these systems. The article also includes instructions for installing additional tools such as Jupyter Notebook and Pandas, and it concludes with a method for verifying the successful installation of TensorFlow.

Opinions

  • The author acknowledges the significant upgrade from an Intel-based Macbook Pro to one with the Apple M1 Pro chip, highlighting the improved user experience.
  • Installing TensorFlow on Mac M1 and M2 was initially challenging due to the lack of official methods and GPU support, which the author describes as a "head-scratcher."
  • The author expresses satisfaction with the final outcome, suggesting that the installation process becomes "easy" after following their guide.
  • The article implies that the installation of TensorFlow on Mac M1 and M2 is not officially supported by TensorFlow, necessitating a workaround solution.
  • The author is determined to assist others by sharing their experience, indicating a community-oriented approach to problem-solving.
  • The author recommends their previous article on installing Conda and creating virtual environments on Mac M1 for readers unfamiliar with the process, demonstrating a commitment to thoroughness and reader comprehension.

How to Install TensorFlow on Mac M1 & M2 (Easy)

Photo by Wesson Wang on Unsplash

Introduction

In 2020, Apple launched the game-changing M1 Chip, creating quite a buzz in the tech world. My chance to experience it came when I started my current job and received a Macbook Pro featuring the Apple M1 Pro chip. This was a significant upgrade from my old Macbook Pro (2017) with a 3.5 GHz Dual-Core Intel Core i7, and it was a relief to be free from the notorious butterfly keys.

As a Data Scientist, I frequently use libraries like TensorFlow. While installing Scikit-Learn and PyTorch was a breeze, installing TensorFlow on my new Macbook Pro M1 proved to be a head-scratcher. After hours of troubleshooting with my team, we managed to find a workaround.

However, we discovered two key challenges:

  1. There was no official method for installing TensorFlow on a Macbook Pro M1/M2.
  2. TensorFlow lacked official GPU support for MacOS.

Determined to assist others in the same predicament, I decided to share my experience and guide on how to successfully install TensorFlow on an Apple M1 or M2-powered Macbook. In this blog post, I’ll simplify the process and help you get TensorFlow running on your Macbook with ease.

How to Install TensorFlow

  1. Install XCode Command Line Tools
  2. Install Miniconda
  3. Create a virtual environment
  4. Install TensorFlow
  5. Install Jupyter Notebook and Pandas
  6. Check the Installation

Step 1: Install Xcode Command Line Tools

This is essential in some guides except I managed to do it (although somehow I got away with not having to do this). You can do this easily from the Appstore, or through terminal:

xcode-select --install

Step 2: Install Miniconda

If you’re not already familiar with creating a conda environment, I recommend checking out my previous article on this topic before proceeding to step 3. This will ensure that you have the necessary foundation in place before diving into the next part of the process.

Step 3: Create a virtual environment

Once you have Anaconda or Miniconda up and running, you can move on to the next steps.

To create and activate your virtual environment, follow these commands:

Start by creating your virtual environment. In my case, I named it tf-env and specified python=3.8.

conda create --name tf-env python=3.8

Then you need to activate the environment:

conda activate tf-env

You can see at the top of the terminal below that (base) changes to (tf-env) once it’s activated.

Step 4: Installing Tensorflow-MacOS

So far, the process has been quite straightforward, hasn’t it? You can breathe easy because it only gets simpler from here.

TensorFlow dependencies, or “tensorflow deps,” are the necessary components and libraries required to ensure that TensorFlow operates smoothly on your system. These dependencies play a crucial role in the proper functioning of TensorFlow. Before moving forward, it’s essential to address these dependencies to make the TensorFlow installation seamless.

To kickstart our TensorFlow installation, execute the following commands:

conda install -c apple tensorflow-deps

“TensorFlow-macos” refers to a specialized version of the TensorFlow deep learning framework designed to run on macOS-based systems, particularly those equipped with Apple’s M1 or M2 chips. This version of TensorFlow is optimized to take full advantage of the unique architecture and capabilities of Apple’s custom silicon.

We install this with the following command:

python3 -m pip install tensorflow-macos

And finally we install tensorflow_datasets. This is not essential but is a great way to get started with your TensorFlow projects.

pip install tensorflow_datasets

From here, you’re ready to get started and everything else is optional!

Step 5: Install Jupyter Notebook & Pandas

This is so that we can start building projects in our virtual environment. To install Jupyter Notebook and Pandas we run the following commands:

python3 -m pip install jupyter
python3 -m pip install pandas

Step 6: Check the installation

Finally let’s test everything installed properly.

First we need to activate our python environment within terminal:

python3

Now you can type in python commands to the terminal. Let’s test TensorFlow installed properly by importing it. It will take a while to import if this is the first time you’re running it, so give it a few minutes to load.

import tensorflow as tf

If you don’t get any error and it goes straight to the next line, displaying >>> then congratulations! You’ve installed TensorFlow!

Let’s have a go at importing TensorFlow datasets.

import tensorflow_datasets as tfds

I hope you found this article helpful, if so please like or comment and subscribe for more content!

TensorFlow
Macos
M1 Chip
Conda
Apple
Recommended from ReadMedium