Install Deep Learning Libraries on Apple MacBook M1 Pro

Neeraj Kumar Vaid
3 min readNov 17, 2021

--

How to install Tensorflow and Jupyter-Notebook on latest MacBook M1 Pro?

I was excited to setup my new MacBook M1 Pro to do machine/deep learning with Tensorflow (Keras), Scikit-learn, and Pandas. Below I share the steps that worked for me to install the required libraries on my Macbook, which has the latest M1 Pro chip with 10 CPU cores and 16 GPU cores.

You should open the terminal on you latest MacBook and complete the following steps to set up Tensorflow and other machine learning libraries on your computer.

Step 1: Install Xcode Command Line Tools

These are the tools required for programming and software development on MacOS. Type the following command

xcode-select --install

Step 2: Install Miniforge

Ananconda is not supported on the new M1 chip; so you need to install miniforge, which provides you Conda to install other machine learning packages such as pandas, scikit-learn, etc.

Download “Miniforge3-MacOSX-arm64.sh” from https://github.com/conda-forge/miniforge/releases/. Use the following command to install miniforge on your computer.

bash Miniforge3-MacOSX-arm64.sh

Note that you might need to provide the full path to the downloaded file to execute above command from your terminal. To verify, close your terminal, reopen the terminal, and run the following command.

conda

You should see the following output after running the “conda” command:

Output of “conda” command in Terminal

Step 3: Create and Activate conda Environment

Run the following command to create a Python virtual environment (why? - read this) where you will install Tensorflow.

conda create -name tensorflow

You can replace the virtual environment name “tensorflow” with whatever name you prefer in above command. You should now activate the virtual environment by running the following command.

conda activate tensorflow

Step 4: Install Tensorflow

Let us first install the dependencies for tensorflow by running the following command.

conda install -c apple tensorflow-deps

Now run the following command to install tensorflow on your Mac.

pip install tensorflow-macos

You should also install the metal plug-in for tensorflow by running the following command.

pip install tensorflow-metal

NOTE 1: Make sure you have “pip” installed on your computer before you run the above two commands in your terminal. If you do not have “pip” installed on your computer then you can install it using the following command:

conda install pip

Step 5: Install Jupyter Notebooks, Pandas, and Scikit-Learn

Finally, you can install Jupyter Notebook and other required packages to code your favorite machine learning algorithms on your Mac by using the following commands.

conda install notebook -y
conda install pandas -y
conda install scikit-learn -y

Step 6: Verify the Installation

Run the following command to open a jupyter notebook. Make sure you are in the “tensorflow” virtual environment.

jupyter notebook

Run the following commands in your notebook.

import sys
import tensorflow as tf
import tensorflow.keras
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "AVAILABLE" if gpu else "NOT AVAILABLE")

You should see the following output:

Installation Verification

If you see “GPU is AVAILABLE” then tensorflow was able to detect the M1 GPU. Now you can write your favorite machine/deep learning programs to solve problems of the world on your new MacBook.

NOTE 2: “import tensorflow.keras” might return an error because of the version mismatch. You should be able to fix it by running the following command in your tensorflow virtual environment.

pip install keras==2.6

I have Tensor Flow Version 2.6.0 and Keras Version 2.6.0 installed on my computer.

NOTE 3: Tensorflow should use M1 GPU cores by default. However, if it does not use the GPU, insert the following lines in your code to force it to use M1 GPU for computation.

from tensorflow.python.compiler.mlcompute import mlcomputemlcompute.set_mlc_device(device_name="gpu")

--

--

Neeraj Kumar Vaid
Neeraj Kumar Vaid

Written by Neeraj Kumar Vaid

Applied machine learning researcher | Love Books

Responses (1)