imantha
imantha

Reputation: 3828

Installing Tensorflow in M1 Mac

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,

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.

Upvotes: 2

Views: 8150

Answers (4)

Jakob
Jakob

Reputation: 893

If you want to use an Anaconda distribution, you might want to consider Miniconda3. Currently, I'm not sure whether it's possible to install TensorFlow using Anaconda.

  • Download Miniconda3 using this link. Both the .bash and the .pkg files should do - choose one of them. However, for M1, only the .bash file seem to have worked, while for M2 both worked.

  • If you used the .bash file above, then do the following to start the installation:

chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh 
  • Create a conda environment with Python 3.9: conda create -n tf_env python=3.9 (latest Python version didn't work as I'm writing this).

  • Install TensorFlow and dependencies:

conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
  • At this point, you should make sure that the Numpy version is up to date to not run into errors when importing TensorFlow. I used version 1.23.3. python -m pip install numpy --upgrade.

  • Install jupyter notebook using conda install notebook.

  • If everything has been installed without errors, running the following code in a jupyter notebook should correctly import TensorFlow and print out the version as well as verifying the presence of a GPU.

import tensorflow as tf

gpu = len(tf.config.list_physical_devices('GPU')) > 0
print(f"TensorFlow Version: {tf.__version__}")
print("GPU is", "available" if gpu else "NOT available")

Upvotes: 0

Nakul
Nakul

Reputation: 351

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

Upvotes: 0

Manmay Barot
Manmay Barot

Reputation: 185

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

Upvotes: 2

user11530462
user11530462

Reputation:

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

OR,

Try the workaround mentioned here.

Upvotes: 0

Related Questions