How to have Tensorflow2 working on MacOS M1 and manage your libs effectively using pyenv and virtualenv

Short Intro

Last few months I’ve been developing myself in the field of Machine Learning and recently I dove into Deep Learning. Unfortunately, there was a wall I hit painfully, as Tensorflow doesn’t natively support M1 chipset.

So, I spent dozens of hours trying out many different tutorials on how to fix that, which made my OS messy and problematic to manage (I’m looking at you, Anaconda). And then I learnt about pyenv and after few more hours — got it all fully manageable, clean, easy to re-install and working. Almost like NPM for javascript projects 🙂

In the following solution I assumed you are using MacOS Monterey v12.4 with M1 chipset and also that you have Homebrew installed.

How to

Let’s create a new folder, for instance: tensorflow-test and in that folder, create a script install.sh with the following content:

brew install pyenv
pyenv install 3.9.13
pyenv global 3.9.13
pip install virtualenv
virtualenv env
source env/bin/activate
ipython kernel install --name "env" --user
pip install tensorflow-macos tensorflow-metal jupyter pandas matplotlib seaborn sklearn plotly babyplots xgboost

Now, run the script using the following command:

chmod +rx ./install.sh
./install.sh

This may take a while, depending on your internet speed. The result for me looks like this (might be a bit different than yours, as I already have some tools installed):

Now, if you don’t see (env) to the left of your login then you need to repeat this:

source env/bin/activate

Now, you can test if everything works:

which python
virtualenv at work

As you can see, your python installation lies in tensorflow-test/env folder, which is veeery expected 🙂

Now, let’s quickly test if tensorflow is really working:

python -c "import tensorflow as tf;tf.test.gpu_device_name();tf.config.list_physical_devices('GPU')"
running tensorflow commands

No issues with importing tensorflow and as the response you should get something similar to what’s above — the line marked in red means tensorflow-metal was able to recognize the GPU of your Macbook.

Summary

That’s all! Quick and dirty 🙂 That way you can have different python environments with different libs configured for different projects and you can utilize the Macbook’s GPU as well. Nice 🙂

That’s all! Quick and dirty 🙂 That way you can have different python environments with different libs configured for different projects. Nice 🙂

If you have any questions, opinions or you see a mistake — please mention it in the comments below.

If I have more time then I will add few words about using Tensorflow in Jupyter notebook.

Thanks!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
C. J. Kepinsky

C. J. Kepinsky

Works professionally as a software engineer since 2002. Pragmatist. Loves family, meditation, nature and tech news.