Tensorflow 2.6 and data science on Mac M1 (November 2021)

Marco Sanguineti
3 min readOct 24, 2021

Enhance your data science experience on M1 Apple Silicon

In this brief tutorial, I’ll show you how to set up your Macbook Air / Pro / Mini with Apple Silicon M1 architecture for Tensorflow and Datascience. As you might know, many python libraries haven’t been optimized yet for the new Apple architecture, but at the same time, I'm an enthusiastic Apple user and think that the iOS environment can enhance productivity from several points of view.

As a Python and Deep learning algorithms developer, I had to set up my new notebook (Macbook Pro with M1) for data science, let’s see how to do so.

image by author

Environment

This procedure has been developed for the following environment:

  • macOS BigSur 11.6
  • Python 3.9
  • Tensorflow 2.6.0
  • Miniforge3 for the Arm architecture
  • Homebrew
  • iTerm2 (standard terminal will work fine as well)

Procedure

To get started, install the X-code command-line tools:

xcode-select --install

We got to install Homebrew. Homebrew is a really powerful packages manager for macOS or Linux.

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

To avoid any architecture compatibility problem, I suggest running iTerm (or terminal) with Rosetta until a full stable release of any main package will be available. To do so with your Finder, go to Applications => right-click on iTerm and select Get info. From the pop-up secondary window select Open using Rosetta.

It’s time to install Miniforge for Arm architecture. “This repository holds a minimal installer for Conda specific to conda-forge.” Download the Miniforge3-MacOSX-arm64 version of Miniforge3 . From a terminal, go to the downloads folder:

cd Downloads/

Let’s install Miniforge3 by running:

sh Miniforge3-MacOSX-arm64.sh

Follow the installation instructions (check the above YouTube video for a complete installation guide of Miniforge3) and restart the terminal. If all went fine, your terminal will be started with a base conda environment with Python 3.9. I suggest you create a new conda environment for any different project you’ll be working on. Let’s see how:

conda create --name py38 python=3.8

You can specify a different name and a different python version. Once the installation is completed, activate the new environment:

conda activate py38

It’s time to install our favourites data science libraries. Let’s install Tensorflow 2.6 and its dependencies:

conda install -c apple tensorflow-depspip install tensorflow-macospip install tensorflow-metalconda install -c conda-forge -y pandas jupyter

To check your installation, open a python terminal in the py38 conda environment:

(py38) python

and try importing Tensorflow, checking its version and stability

>>> import tensorflow as tf
>>> tf.__version__
2.6.0
>>> a = tf.Variable([1, 2])
>>> a
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2], dtype=int32)>

Greetings

That’s all! You can find a more complete example (creation of a simple linear regressor with the Keras functional API in Jupyterlab) in the above video.

I hope you’ll find this short guide useful, until next time

Marco

--

--

Marco Sanguineti

Graduated in Mechanical Engineering, I work in the world of AI, Deep Learning and Software Development. Passionate about Technology, Videogames and AI.