numpy build fail in M1 Big sur 11.1

Python 3.xMacosNumpy

Python 3.x Problem Overview


I am using MacBook m1 which is running Big Sur 11.1 ,and I have installed Xcode-commandline-tools version 12.3 and it has installed python3.8.5 and pip3 .python3 and pip are native apps,that is they run in ARM

pip3 version 20.3.3

python3 version 3.8.5

setuptools version 51.0.0

wheel version 0.36.2

when I type python3 -m pip install numpy

The output is :

Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
  Using cached numpy-1.19.4.zip (7.3 MB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
    Preparing wheel metadata: started
    Preparing wheel metadata: finished with status 'done'
Building wheels for collected packages: numpy
  Building wheel for numpy (PEP 517): started
sandeep@Sandeeps-Air ~ % cat a
Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
  Using cached numpy-1.19.4.zip (7.3 MB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
    Preparing wheel metadata: started
    Preparing wheel metadata: finished with status 'done'
Building wheels for collected packages: numpy
  Building wheel for numpy (PEP 517): started
  Building wheel for numpy (PEP 517): finished with status 'error'
Failed to build numpy

and with a very large list of errors

Is numpy still not supported in M1 or am I doing it wrong ?

Python 3.x Solutions


Solution 1 - Python 3.x

I had exactly the same issue with my brand new Mac Mini. After spending an afternoon reading this issue on github, I finally succeeded. However, it only seems to work with v1.20.0rc2 or above...

Long story short, these were the steps I've taken to get it working:

pip3 install Cython
git clone https://github.com/numpy/numpy.git 
cd numpy
pip3 install . --no-binary :all: --no-use-pep517

After that, I could cd into my virtualenv and import numpy:

>>> import numpy as np
>>> np.__version__
'1.21.0.dev0+402.gc4ae3ce64'
>>> 

Next up: pandas.

UPDATE: After some more searching, I found this interesting article, which states that you can easily start Terminal in Rosatta mode. That also solved the issues.

However, I am working with a headless Mac Mini, so I still have to find out whether that solution also is feasable in that particular setup.

UPDATE 2

I now have pandas working as well, thanks to this answer on SO.

Solution 2 - Python 3.x

Yes, numpy still seems to be not supported in stable, but if you are ready to work with unstable version try this command:

pip3 install numpy --compile --pre

This will recompile all the necessary code from unstable branch.

Worked for me.

Solution 3 - Python 3.x

Since NumPy 1.21.0, it has released universal2 wheels for Python 3.8 and Python 3.9 on Mac. It becomes installable on Mac M1.

I have tested it on my M1 with Python 3.9.2, macOS Big Sur (11.5.1), and pip 21.1.3. Just use pip/pip3 to install it.

release note: https://github.com/numpy/numpy/releases/tag/v1.21.0

Solution 4 - Python 3.x

Note that the only reliable build environment for the full data-science suite for python at the moment (Feb 2021) on M1 ARM architecture is via miniforge.

e.g.

brew install --cask miniforge
conda init zsh
conda activate
conda install numpy scipy scikit-learn

Solution 5 - Python 3.x

What it worked for me was to run iTerm with the option "open using Rosetta" enabled and upgrade pip before installing numpy.

enter image description here

Instead of enabling this option you could also run the pip install and python selecting a different architecture with arch -x86_64. Example using a virtual environment:

$ python3 -m venv ~/myenv
$ source ~/myenv/bin/activate
(myenv)$ python3 -m pip install --upgrade pip
Collecting pip
  Using cached pip-21.0.1-py3-none-any.whl (1.5 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.3
    Uninstalling pip-20.2.3:
      Successfully uninstalled pip-20.2.3
Successfully installed pip-21.0.1

(myenv)$ arch -x86_64 pip install numpy
Collecting numpy
  Using cached numpy-1.20.1-cp39-cp39-macosx_10_9_x86_64.whl (16.1 MB)
Installing collected packages: numpy
Successfully installed numpy-1.20.1

(myenv)$ arch -x86_64 python3
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.20.1'

Solution 6 - Python 3.x

First of all numpy support is not there for m1 arm based Macs using python 3.8 version. The method to workaround with this is to update your python version to 3.9. This is the latest version of python and can be a bit unstable. If you want to work with tensorflow then do not update to python 3.9 version as they don't support it and support will come up with version 2.5 (estimated). So, if you don't want to use tensorflow and want to use numpy by using pip install then go ahead install python3.9 and make it your default python interpreter.

I will recommend you to use Homebrew for installation of python 3.9 as it as easy as running a simple command.

Solution 7 - Python 3.x

I was trying to install numpy without rosetta 2, but the solutions listed here failed for me. Brew saved the day. I installed it with

brew install numpy

Solution 8 - Python 3.x

Try this as I was able to install numpy using this method.

  1. brew install openblas (use the native homebrew instead of running it under Rosetta 2)
  2. OPENBLAS="$(brew --prefix openblas)" pip3 install numpy

Solution 9 - Python 3.x

A solution for pipenv users:

git clone https://github.com/numpy/numpy.git --recursive
cd numpy
pip3 install build
python3 -m build
cd dist
cp numpy-1.22.0.dev0+97.g4635fc852-cp39-cp39-macosx_11_0_arm64.whl ../../ # filename is current sample, next folder to your project directory

Add into Pipfile:

[packages]
numpy = { path = "numpy-1.22.0.dev0+97.g4635fc852-cp39-cp39-macosx_11_0_arm64.whl" }
pipenv install

then your numpy will be installed.

Solution 10 - Python 3.x

As mentioned in a previous answer, the installation went smooth when I opened the terminal using Rosetta.

However for anyone who is using Pycharm terminal, Do the following to get the terminal running using Rosetta.

  • Go to Preferences -> Tools -> Terminal
  • Update the script path to "env /usr/bin/arch -x86_64 /bin/zsh --login"

I tried a lot of other options but so far this was the simplest solution I found.

Solution 11 - Python 3.x

After trying a ton of suggested methods I finally found a way to use numpy on my new macbook pro with an M1 chip.

Use this docker preview for the M1 chip.

E.g. here's a Dockerfile that successfully installs numpy.

FROM python

RUN python3 -m pip install numpy

Build it with:

docker build -t numpytest:latest .

Then run it with:

docker run -it --rm numpytest:latest

And you can verify that numpy is installed and works correctly:

Python 3.9.1 (default, Jan 12 2021, 13:13:57) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.arange(15).reshape(3, 5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> 

Solution 12 - Python 3.x

Well I guess it's fixed in unreleased upstream. A pip install of 1.20.2, the latest as of this writing, failed. But I cloned [email protected]:numpy/numpy.git, then did the normal "python setup.py install" in my venv (which is Python 3.9.5 installed via Homebrew) and it seems to work. Haven't thoroughly tested it yet.

Solution 13 - Python 3.x

CrepeGoat's comment worked for me:

pip3 install --no-binary :all: --no-use-pep517 numpy

Solution 14 - Python 3.x

faced the same problem in monterey too.

upgrading pip version to 21.3.1 from 20.2.3 solved the issue.

source env/bin/activate
pip install -U pip
pip install numpy

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzshView Question on Stackoverflow
Solution 1 - Python 3.xBart BarnardView Answer on Stackoverflow
Solution 2 - Python 3.xMikhail KaravashkinView Answer on Stackoverflow
Solution 3 - Python 3.xGrimmer KangView Answer on Stackoverflow
Solution 4 - Python 3.xclimatebradView Answer on Stackoverflow
Solution 5 - Python 3.xBaleatoView Answer on Stackoverflow
Solution 6 - Python 3.xAmriteshView Answer on Stackoverflow
Solution 7 - Python 3.xFoldagerView Answer on Stackoverflow
Solution 8 - Python 3.xVarun ReddyView Answer on Stackoverflow
Solution 9 - Python 3.xjiho leeView Answer on Stackoverflow
Solution 10 - Python 3.xManu mathewView Answer on Stackoverflow
Solution 11 - Python 3.xG. ShandView Answer on Stackoverflow
Solution 12 - Python 3.xMicah YoderView Answer on Stackoverflow
Solution 13 - Python 3.xreflexiveprophecyView Answer on Stackoverflow
Solution 14 - Python 3.xnaamView Answer on Stackoverflow