How to install tensorflow on a mac studio ?

Published: December 03, 2022

Updated: December 14, 2022

Tags: Python; TensorFlow; Apple Mac;

DMCA.com Protection Status

Recently bought a new mac studio and needed Tensorflow for my work. On my previous iMac I installed tensorflow using Anaconda and it worked very well. However now with my new mac studio (apple M1 chip), I got the following error on a jupyter noteboook:

"the kernel appears to have died. it will restart automatically"

when I do:

import tensorflow as tf

And if I launch python on the terminal:

import tensorflow as tf

gives

ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none).

So after a couple of hours trying to understand why and how to fix it, I came with the following solution that works fine for me:

Installing tensorflow using miniforge

To use tensorflow, you need to install miniforge (Note: no need to remove anaconda if it is already installed; it is possible to work with both; see previous section).

Step0: make sure that you are not in an anaconda environment. Enter several times:

conda deactivate

to leave conda.

Step1 go to github miniforge and download the script "Miniforge3-MacOSX-arm64.sh" on your machine. Then open a terminal and run the script:

sh Miniforge3-MacOSX-arm64.sh

Accept the licence and enter yes (miniforge initialized).

Then start a new terminal window !! (if you don't do that the next steps will not works).

Step2: create a new conda environment called for example worklab_tf

conda create --name worklab_tf python=3.8

Step3: activate the new environment

conda activate worklab_tf

Step4

conda install -c apple tensorflow-deps

Step5

pip install tensorflow-macos

Step6

pip install tensorflow-metal

Step7

conda install -c conda-forge -y pandas jupyter

Step8

jupyter notebook

Then if you enter in a jupyter notebook cell:

import tensorflow as tf

it should returns no error message.

Working with anaconda and miniforge together

Ok so now when I try:

conda env list

I got environment from miniforge3 and anaconda3:

conda env list
# conda environments:
#
                            /Users/student/miniforge3/envs/worklab_tf
base                     /Users/student/opt/anaconda3
mlp                      /Users/student/opt/anaconda3/envs/mlp
websitedev           /Users/student/opt/anaconda3/envs/websitedev
worklab                /Users/student/opt/anaconda3/envs/worklab

but it is ok you can easily switch from on to another by editing the .zshrc file. Just open the file:

vi .zshrc

file should be located /Users/student (replace student by your name)

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/bmarchant/miniforge3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/bmarchant/miniforge3/etc/profile.d/conda.sh" ]; then
        . "/Users/bmarchant/miniforge3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/bmarchant/miniforge3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

then just replace miniforge3 by anaconda3.

And voilà !!. It took me more time than I expected to install Tensorflow but at least it works now on my mac studio (I assume it is a temporary solution before it works again with anaconda).