Install TensorFlow on M1 Mac (Debug included)
To install my TensorFlow, I tried to follow How To Install TensorFlow on M1 Mac (The Easy Way) and Installing Tensorflow on Apple Silicon but I still encountered various problems. This post combined all the resources I found online with a simple explanation of the process under the hood and potential bugs that you might encounter during the installation process.
Installation steps
Step 1: Install Xcode
TensorFlow is a machine learning framework, and xcode supports multiple programming languages, including Objective-C, Swift, and C++, and provides a variety of templates and frameworks that developers can use to get started with their projects quickly. Xcode is a complete developer toolset for creating apps for Mac, iPhone, iPad, Apple Watch, and Apple TV. Xcode As far as I know, many machine learning package requires xcode, so it’s always good to set up beforehand.
You can run this command in your terminal.
xcode-select --install
Step 2: Install Miniforge3
Miniforge is a package manager. Without it, you will receive an error that TensorFlow isn’t available in your conda developer channel. It helps you get packages in the base environment obtained from the conda-forge channel. Here are the installation steps. (p.s. you need to install miniforge3 rather than miniforge that’s the error I wasn’t aware of.)
File to download: Miniforge3-MacOSX-arm64
Run this code to download it on your mac
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
Activate the miniforge3 environment
source ~/miniforge3/bin/activate
Note: To check if your environment is successfully activated, you can run the following code to check all the environments you have so far.
conda info - envs
Step 3: Create a new virtual environment for TensorFlow installation
We create a new environment to contain the Tensorflow package we are going to install. The reason for creating a virtual environment is to maintain separation and independency for packages. If we install everything in our base environment, it might create lots of dependency conflicts among packages.
Here, you create a new environment called tf in your minforge3 folders, and you activate tf.
conda create -n tf python=3.9
conda activate tf
If you check your environment through conda info --envs
, your output should look like this image. Your * should point toward your tf environment inside minforge3 folders.
As you can see from the output, it does point toward miniforge3 and the tf folder. Here are some potential errors that you might also encounter during the installation process.
Error output 1 — Install miniforge instead of minforge3: /usr/local/Caskroom/miniforge/base
Error output 2 — I create a virtual environment, tf, in my anaconda rather than minfiorge3: /Users/swimmingcircle/opt/anaconda3/envs/tf
If you don’t install it on miniforge3, you will encounter errors for the next step.
Step 4: Install all the TensorFlow-related packages
After you have the package manager and environment you need, you can start install all Tensflow package.
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos==2.9
python -m pip install tensorflow-metal==0.5.0
tensorflow-deps
: Metapackage for installing dependencies of TensorFlowtensorflow-macos
: Tensorflow for mac os system.tensorflow-metal
: Enable Tensorflow GPU on mac. Without it, you can only access your CPU, resulting in slower running in your model. But you can still use TensorFlow on your notebook without having this package. If your suspect this package returns an error, you can use the following code to uninstall it and run your tensorflow to check if tensorflow-metal is the reason:python -m pip uninstall tensorflow-metal
Potential version compatibility error: The version of the packages needs to be compatible with each other. For instance, is tensorflow-macos==2.9
is compatible with tensorflow-metal==0.5.0
and tensorflow-metal==0.6.0
If you didn’t specify the correct version, you might receive the following error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/swimmingcircle/miniforge3/envs/tf/lib/python3.9/site-packages/tensorflow/__init__.py", line 443, in <module>
_ll.load_library(_plugin_dir)
File "/Users/swimmingcircle/miniforge3/envs/tf/lib/python3.9/site-packages/tensorflow/python/framework/load_library.py", line 151, in load_library
py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: dlopen(/Users/swimmingcircle/miniforge3/envs/tf/lib/python3.9/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 0x0006): symbol not found in flat namespace '__ZN3tsl8internal10LogMessage16VmoduleActivatedEPKci'
You can use this code to force reinstall.
python -m pip install tensorflow-macos==2.9.0 tensorflow-metal==0.5.0 --force-reinstall
Step 5: Check if you install TensorFlow successfully on your terminal
To check if you have successfully installed it, you can directly check if on your terminal through these commands.
python3
import tensorflow as tf
tf.__version__
tf.config.experimental.list_physical_devices('GPU')
Your terminal output should look like the image below. It will take a while when you run the import tensorflow as tf
but shouldn’t take more than a minute. Here, you check your TensorFlow version and check if you can run it on your GPU.
Step 5: Test your Tensorflow on your notebook
Lastly, run the following code on your notebook and check if it is able to finish all the epochs.
Input code
import tensorflow as tf
tf.config.experimental.list_physical_devices('GPU')
cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
include_top=True,
weights=None,
input_shape=(32, 32, 3),
classes=100,)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
Expected output
Potential notebook error: If you install jupyter notebook through vscode. interface, you might receive an error when running your TensorFlow. You can uninstall Tensorflow, reinstall TensorFlow, and install your jupyter notebook on the terminal through this code, pip install jupyter notebook
.
In addition, you can change using GPU or CPU using the following code, but GPU is usually a faster choice!
Conclusion
Hope you are able to debug and install your TensorFlow on M1! 🎉🎉🎉 In general, installation problems are usually annoying and take much longer time to figure out. I was mad all the time when trying to figure the bugs out. So be patient and take the opportunity as a way to learn the computer system!