Got stuck trying to install TensorFlow on Mac M1
Asked Answered
S

2

5

I have been trying to install TensorFlow on my Macbook Air with a M1 chip.
Using Python 3.9.7. Originally was on MacOS 11, but subsequently upgraded to 12.01

At first, I tried these instructions [https://towardsdatascience.com/installing-tensorflow-on-the-m1-mac-410bb36b776] but got stuck when trying to execute

pip3 install --upgrade --force --no-dependencies https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl

ERROR: tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl is not a supported wheel on this platform.

So I tried to follow these instructions [https://www.tensorflow.org/install/source#macos_1] to compile TensorFlow, but when I try

bazel build //tensorflow/tools/pip_package:build_pip_package

I get these errors: ERROR: /Users/scottbrown/tensorflow/tensorflow/lite/python/BUILD:62:10: Target '//tensorflow/lite/python:tflite_convert' depends on toolchain '@local_config_cc//:cc-compiler-darwin', which cannot be found: error loading package '@local_config_cc//': cannot load '@local_config_cc_toolchains//:osx_archs.bzl': no such file' ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted: Analysis failed

When I try

pip3 install tensorflow-macos

I get this error: Building wheel for h5py (pyproject.toml) ... error ERROR: Command errored out with exit status 1: command: /opt/homebrew/opt/[email protected]/bin/python3.9 /opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/in_process/_in_process.py build_wheel /var/folders/gz/28jpdfcd3b3g4pm7zl0wmrkh0000gn/T/tmpz_m057zj cwd: /private/var/folders/gz/28jpdfcd3b3g4pm7zl0wmrkh0000gn/T/pip-install-kz29fkw2/h5py_0747e63c821445b6944ecb4fc6b2d1e1

Stiffler answered 3/12, 2021 at 23:11 Comment(0)
B
7

I'm basing my answer on the article from Prabhat Kumar Sahu: How to install Tensorflow on M1 Mac the easy way

Set up environment

Make sure you have homebrew, xcode, and miniforge installed.

create a virtual environment

conda create --name mlp python=3.8

activate environment

conda activate mlp

Install tensorflow for mac-os

(sets up the wheel files etc.)

conda install -c apple tensorflow-deps
pip install tensorflow-macos
pip install tensorflow-metal

That's it. You should have the environment all ready to go. Look at Prabhat's article for a sample Jupyter Notebook test for an example of how to benchmark/test your environment.

Berkin answered 8/12, 2021 at 11:49 Comment(3)
Hi! Thanks for replying. I just tried these steps. I clicked the link to Prabhat's article and followed steps 5 and 6 there since it seems like your instructions correspond with his earlier steps. When I tried executing the suggested code in Jupyter Notebook, I get this error: AttributeError Traceback (most recent call last) <ipython-input-1-59309fb5a3f1> in <module> 1 import tensorflow as tf -> 2 print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) AttributeError: module 'tensorflow' has no attribute 'config'Stiffler
@ScottBrown were you able to get tensorflow running on jupyter?Mintz
Yes... now this is over a year ago, but I believe the missing step was to execute this command: conda activate tensorflowStiffler
K
2

Hey guys I had the same issue but I fixed it with the following instructions :

NOTE: If using conda environment built against pre-macOS 11 SDK use:

SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-macos

otherwise, you will get errors like: “not a supported wheel on this platform”

STEPS :

OS Requirements macOS 12.0+ (latest beta)

Currently Not Supported Multi-GPU support Acceleration for Intel GPUs V1 TensorFlow Networks

Installation Instructions

Step 1: Environment setup CPU TYPE x86: AMD

Create virtual environment (recommended):

python3 -m venv ~/tensorflow-metal

source ~/tensorflow-metal/bin/activate

python -m pip install -U pip

NOTE: python version 3.8 required

CPU TYPE : arm64 : Apple Silicon

Download and install Conda env:

chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh sh ~/Downloads/Miniforge3-MacOSX-arm64.sh source ~/miniforge3/bin/activate

OR conda env create --file=environment.yml --name tf_m1 and then activate tf_m1

Install the TensorFlow dependencies:

conda install -c apple tensorflow-deps

When upgrading to new base TensorFlow version, we recommend:

uninstall existing tensorflow-macos and tensorflow-metal

python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal

Upgrade tensorflow-deps

conda install -c apple tensorflow-deps --force-reinstall

or point to specific conda environment

conda install -c apple tensorflow-deps --force-reinstall -n my_env

tensorflow-deps versions are following base TensorFlow versions so:

For v2.5:

conda install -c apple tensorflow-deps==2.5.0

For v2.6:

conda install -c apple tensorflow-deps==2.6.0

NOTE: Python versions 3.8 and 3.9 supported

Step 2: Install base TensorFlow

python -m pip install tensorflow-macos

NOTE: If using conda environment built against pre-macOS 11 SDK use:

SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-macos

otherwise you will get errors like: “not a supported wheel on this platform

Step 3: Install tensorflow-metal plugin

python -m pip install tensorflow-metal
Kiangsi answered 22/5, 2022 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.