Installing Tensorflow in M1 Mac

Viewed 5795

I followed this tutorial, https://www.youtube.com/watch?v=ykCY_tJbhNw to install tensorflow in a mac (M1 processor). However when I import tensrflow I get the following error. Any ideas what it might be?

The steps include,

  • installing brew (was alreadyy installed)
  • install conda (was already installed)
  • installing miniforge
  • download tensorflow-macos from github page
  • conda activate mlEnv
  • /Users/imantha/Software/tensorflow_macos/install_venv.sh -p
  • When prompted to input the path : /Users/imantha/Software/anaconda3/envs/mlEnv/
  • and select yes to install tensorflow
  • tensorflow was successfully installed and when typed conda list, the package is listed.

However when I start python session in the shell and type import tensorflow as tf, I get the following error

Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found.  Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: 
dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image 
found.  Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong 
 architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong 
architecture


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
3 Answers

There are quite a few things to do in order to install TensorFlow on the Macbook M1 chip:

  • First, install Python 3.8.9 (I have tried on this version, not sure about any other versions)

  • Go to your project dir

  • install hdf5 by running brew install hdf5 if you do not have brew, you can download it here: https://brew.sh/

  • Above hdf5 install will spit out its location: use it and run: export HDF5_DIR=<hdf5_location> for version 1.13.0 (my case) the following:

export HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.13.0
  • create a virtual environment using virtualenv venv (choose whatever method for creating virtual environment or do not create virtual environment, it's fine. To install virtualenv, run pip3 install virtualenv)
  • (Skip this if not using virtual environment) Activate virtual environment by running: source venv/bin/activate
  • Now, we have everything require to install Tensorflow on mac with M1 chip including GPU version
  • To install Tensorflow and related libs, run following: (If using virtual env, run following)
venv/bin/pip install --no-binary=h5py h5py
venv/bin/pip install tensorflow-macos
venv/bin/pip install tensorflow-metal

Not using virtual env, run following:

pip3 install --no-binary=h5py h5py
pip3 install tensorflow-macos
pip3 install tensorflow-metal
  • Now, your TensorFlow installation has been completed!
  • Try it out:
import tensorflow as tf

print("TF version:", tf.__version__)
print("Number GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
print("Number CPUs Available: ", len(tf.config.experimental.list_physical_devices('CPU')))

enter image description here

Build Tensorflow from source by following the instructions mentioned here. See this comment.

OR,

Try the workaround mentioned here.

The below way is the simplest and efficient way from which we can install tensorflow in Mac M1

NOTE: Mac M1 has ARM64 arch not X86_64 so we need to be very careful while downloading packages. Anaconda is available for Mac M1 users : https://repo.anaconda.com/archive/Anaconda3-2022.05-MacOSX-arm64.sh

Following are the steps to victory:

  1. Download Anaconda from above link
  2. create virtual env ( Optional )
  3. pip install tensorflow-macos

Thats it!

Reference video link which may be useful : https://www.youtube.com/watch?v=5DgWvU0p2bk

Related