How to install SciPy on Apple Silicon (ARM / M1)

PythonScipyArmApple SiliconApple M1

Python Problem Overview


I have successfully installed python 3.9.1 with Numpy and Matplotlib on a new Mac mini with Apple Silicon. However, I cannot install SciPy : I get compilation errors when using

python3 -m pip install scipy

I also tried installing everything from brew, and import scipy works, but using it gives a seg fault. I have installed ARM versions of lapack and openblas, but this does not fix the problem.

Has anyone succeeded? (I am interested in running it natively, not through Rosetta).

Python Solutions


Solution 1 - Python

It's possible to install on regular arm64 brew python, you need to compile it yourself.

If numpy is already installed (from wheels) you'll need to uninstall it:

pip3 uninstall -y numpy pythran

I had to compile numpy, which requires cython and pybind11:

pip3 install cython pybind11

Then numpy can be compiled:

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

Scipy needs pythran (this should happen after installing numpy):

pip3 install pythran

Then we need to compile scipy itself, it depends on fortran and BLAS/LACK:

brew install openblas gfortran

Tell scipy where it can find this library:

export OPENBLAS=/opt/homebrew/opt/openblas/lib/

Then finally compilescipy:

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

Solution 2 - Python

This one worked for me after wasting hours:

pip install --pre -i https://pypi.anaconda.org/scipy-wheels-nightly/simple scipy

Solution 3 - Python

This solution worked on my M1 machine with pyenv:

brew install openblas
OPENBLAS="$(brew --prefix openblas)" pip install numpy scipy

Solution 4 - Python

You can install miniforge from https://github.com/conda-forge/miniforge#miniforge3 and then install those packages with,

conda install numpy scipy matplotlib

Solution 5 - Python

For me the easiest solutions:

brew install scipy

Probably good idea to edit the PATH, so the homebrew version will be the default.

Solution 6 - Python

I managed to get scipy installed on Apple Silicon. I mostly followed the instructions by lutzroeder here: https://github.com/scipy/scipy/issues/13409

Those instructions weren't successful for me, but running 'pip3 install scipy' worked afterwards. I think this fixed the problem for me:

/opt/homebrew/bin/brew install openblas

export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)

export CFLAGS="-falign-functions=8 ${CFLAGS}"

Solution 7 - Python

For those who need it for short-term purposes and don't want too much hustle - it seems to work with python 3.6.4 and scipy 1.5.4 out of the box (Big Sur 11.5.2, M1 chip).

Solution 8 - Python

In addition, if someone has this error message>

########### CLIB COMPILER OPTIMIZATION ###########
Platform      :
  Architecture: aarch64
  Compiler    : clang

CPU baseline  :
  Requested   : 'min'
  Enabled     : none
  Flags       : none
  Extra checks: none

CPU dispatch  :
  Requested   : 'max -xop -fma4'
  Enabled     : none
  Generated   : none
CCompilerOpt.cache_flush[809] : write cache to path 

I found this solution before compile numpy and scipy

Analysis of reasons: From the above error message, you can see that the last error shows that clang has an error, so it is speculated that it should be an error caused by the compiler, because the new version of the xcode command tool uses the arm version of the compilation method by default, and if we want to use For x86 architecture, we need to manually set the specific architecture through environment variables.

export ARCHFLAGS="-arch x86_64"

example:

3c790c45799ec8c598753ebb22/build/temp.macosx-10.14.6-arm64-3.8/ccompiler_opt_cache_clib.py
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/daniel_edu/Projects/PERSONAL/great_expectation_demo/.env/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-install-y8alaej_/numpy_3d813a3c790c45799ec8c598753ebb22/setup.py'"'"'; __file__='"'"'/private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-install-y8alaej_/numpy_3d813a3c790c45799ec8c598753ebb22/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/zb/c_b9kh2x1px7vl5683rwz8fr0000gn/T/pip-record-q9vraevr/install-record.txt --single-version-externally-managed --compile --install-headers /Users/daniel_edu/Projects/PERSONAL/great_expectation_demo/.env/include/site/python3.8/numpy Check the logs for full command output.
(.env) ➜  great_expectation_demo git:(master) ✗ export ARCHFLAGS="-arch x86_64"
(.env) ➜  great_expectation_demo git:(master) ✗ pip install --no-binary :all: --no-use-pep517 numpy
Collecting numpy
  Using cached numpy-1.21.5.zip (10.7 MB)
  Preparing metadata (setup.py) ... done
Skipping wheel build for numpy, due to binaries being disabled for it.
Installing collected packages: numpy
    Running setup.py install for numpy ... done
Successfully installed numpy-1.21.5

Solution 9 - Python

What version of scipy you're trying to install?

To me running on Macbook air M1, I needed to increase the version from scipy==1.5.1 to scipy==1.7.3, so I guess you should use 1.7.3 version or above it and all will be fine...

 pip install -Iv scipy==1.7.3

Or just add in your file requirements.txt this line:

scipy==1.7.3

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
QuestionDavid SénéchalView Question on Stackoverflow
Solution 1 - PythonMarijnView Answer on Stackoverflow
Solution 2 - PythonJumshudView Answer on Stackoverflow
Solution 3 - PythondwolfeuView Answer on Stackoverflow
Solution 4 - PythonisurufView Answer on Stackoverflow
Solution 5 - PythonarkheinView Answer on Stackoverflow
Solution 6 - PythonhobozeroView Answer on Stackoverflow
Solution 7 - PythonJulie KlimentovaView Answer on Stackoverflow
Solution 8 - PythonDanieleduView Answer on Stackoverflow
Solution 9 - PythonFernando NogueiraView Answer on Stackoverflow