38

I'm trying to get tensorflow working on my MacBook pro M1. However, I keep getting the following error when trying to import: zsh: illegal hardware instruction python

I have downloaded and installed tensorflow via this link.

These were my installation steps:

  • install a venv: python3 -m venv venv.
  • drag the install_venv.sh (which is located within the downloaded folder) file to the terminal, add -p at the end.
  • select the directory of the venv as the location where tensorflow should be installed.
  • activate the venv.
  • type "python".
  • try to import tensorflow: import tensorflow as tf.

I'm using Python 3.8.2.

paradocslover
  • 2,932
  • 3
  • 18
  • 44
georgev
  • 383
  • 1
  • 3
  • 8
  • 1
    You can follow this blog for the similar issue and try the same suggested solution from the thread https://forums.macrumors.com/threads/macos-10-15-catalina-on-unsupported-macs.2183772/page-426. Also for latest update on tensorflow for mac OS you can follow this link https://blog.tensorflow.org/2020/11/accelerating-tensorflow-performance-on-mac.html –  Dec 22 '20 at 10:10
  • This https://stackoverflow.com/a/72381925/2546381 worked! – VanagaS May 25 '22 at 17:27

3 Answers3

36

This worked for me after trying a bunch of solutions to no avail.

Step 1 Using pyenv install python version 3.8.5 and set it as your default python version. This tutorial(https://realpython.com/intro-to-pyenv/) is helpful for getting pyenv configured properly.

Step 1.1 Use this post(https://github.com/pyenv/pyenv/issues/1446) if you have troubles running pyenv in zsh.

Step 1.2 Once you have python version 3.8.5 running which you can check by running python -V which should output:

Python 3.8.5

Step 2 Install virtualenv via pip install virtualenv

Step 2.1 Create a virtual environment by running virtualenv ENV

Step 2.2 Activate that virtual environment by running source ENV/bin/activate

Step 3 Install the tensorflow wheel called tensorflow-2.4.1-py3-none-any.whl located at this public google drive link https://drive.google.com/drive/folders/1oSipZLnoeQB0Awz8U68KYeCPsULy_dQ7

Step 3.1 Assuming you simply installed the wheel to downloads run pip install ~/Downloads/tensorflow-2.4.1-py3-none-any.whl in your activated virtual environment

Step 4 Type python which will bring up >>>in your terminal and type

>>> import tensorflow
>>>

If there is no 'zsh illegal hardware instruction" error you should be good to go.

Note: If you are using anaconda, the above will also work. You can skip the virtual env steps (assuming you have a virtual env activated through Conda) and just go straight to the pip install as mentioned above (steps 3 and later).

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125
Vakidis
  • 394
  • 4
  • 5
  • 9
    Apologies for the cyber awareness but how would one verify the affiliation/identity of the maintainer providing that wheel file from over google drive? – matanster Nov 27 '21 at 14:11
  • 1
    @matanster this is something I would also be wary of. In my case, it was such a pain to get it to run any other way so I went with this not optimal solution. – Vakidis Nov 29 '21 at 17:47
  • If it's any consolation, I have done this 3-4 times now and my computer has not been hacked : ) – logankilpatrick Dec 19 '21 at 23:41
  • 1
    You may install tensorflow for M1 machines with this pip install command: pip install tensorflow-macos – Shahar Gino Mar 28 '22 at 11:56
  • Thanks for sharing this. Worked for me and wheel on google drive can be trusted :smile – Pardeep Singh Apr 30 '22 at 13:16
  • Thanks a lot, I literally spent 3 days trying to resolve this issue. This us the only solution that worked for me. – nickinade Apr 24 '23 at 07:18
7

Python3 is shipped with 2 architectures in M1.

$ file $(which python3)
# If you installed python through Homebrew or Anaconda, deactivate your conda env, then run this line instead:
# $ file $(which /usr/bin/python3)
/usr/bin/python3: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
/usr/bin/python3 (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python3 (for architecture arm64e): Mach-O 64-bit executable arm64e

Here, it's very important to specify which one to use. You can do this by installing the script like this:

arch -arm64 bash install_venv.sh my_tf_env

if you have multiple python installations, use:

arch -arm64 bash install_venv.sh --python=/usr/bin/python3 my_tf_env

You can replace my_tf_env with any other name/path you choose.

dedede
  • 197
  • 11
  • 3
    When I run `$ file $(which python3)` I only get one option: `/opt/homebrew/Caskroom/miniconda/base/envs/svc-pdf-reader/bin/python3: Mach-O 64-bit executable x86_64` Could this be a homebrew related issue? – Zaki Aziz May 04 '21 at 18:00
  • First, deactivate your anaconda env if it's active. Then, change the line to `file $(which /usr/bin/python3)` and run it. – dedede May 21 '21 at 20:55
  • 1
    There's more than one `which` available in MacOS -- the one built into zsh, and the one shipped as an external binary. They're quite unlike each other; I generally don't recommend using `which` at all. – Charles Duffy May 21 '21 at 21:00
  • 6
    With all due respect, your comment has not added anything useful to the conversation at all. – dedede May 21 '21 at 21:02
  • Does this answer really mean that merely choosing the other python executable makes tensorflow seamlessly run as is? – matanster Nov 29 '21 at 18:15
6

I had the same issue

This is because of M1 chip. Now there is a pre-release that delivers hardware-accelerated TensorFlow and TensorFlow Addons for macOS 11.0+. Native hardware acceleration is supported on M1 Macs and Intel-based Macs through Apple’s ML Compute framework.

You need to install the TensorFlow that supports M1 chip Simply pull this tensorflow macos repository and run the ./scripts/download_and_install.sh

Kaushik J
  • 962
  • 7
  • 17