How to have Tensorflow2 working on MacOS M1 and manage your libs effectively using pyenv and virtualenv
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.
In the following solution I assumed you are using MacOS Monterey v12.4 with M1 chipset and also that you have Homebrew installed.
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
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
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:
Now, you can test if everything works:
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')"
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.
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.