77

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).

jtlz2
  • 7,700
  • 9
  • 64
  • 114
David Sénéchal
  • 937
  • 1
  • 7
  • 4

13 Answers13

75

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
Marijn
  • 1,109
  • 8
  • 9
  • 3
    I had to `brew install openblas gfortran` before numpy... numpy needed openblas at least – Anentropic Mar 08 '21 at 23:03
  • 12
    Unfortunately the `SciPy` compilation failed with `error: Command "ar rcs build/temp.macosx-10.14.6-arm64-3.8/libsuperlu_src.a build/temp.macosx-10.14.6-arm64-3.8/scipy/sparse/linalg/dsolve/SuperLU/SRC/dlaqgs.o [………]" failed with exit status 1` :( – MattiSG Mar 17 '21 at 12:27
  • 4
    This answer worked for me on OSX `11.2.3 (20D91)` with a MacBook Pro (13-inch, M1, 2020). I did not get any compilation failed like @MattiSG did – Greg Hilston Apr 01 '21 at 15:54
  • This worked for me, but subsequently scikit-learn (`pip install sklearn`) did not. I bring it up because the notes at https://scikit-learn.org/stable/install.html#installing-on-apple-silicon-m1-hardware and the github link there suggest (to my noob eyes) that sklearn should install, given a successful install of scipy. Before starting a new question, I thought folks who got scipy working might also have advice on this related issue. – user1071516 May 27 '21 at 15:55
  • 12
    I needed pip install pythran – Golf Monkey Jul 13 '21 at 14:17
  • 4
    Why should we use the --no-use-pep517 forced legacy mode? – 0-_-0 Aug 31 '21 at 15:13
  • 1
    It worked after installing python 3.9, wouldn't work with 3.8 – Marcelo Bielsa Sep 16 '21 at 01:19
  • I got this when trying to install scipy: `libraries mkl_rt not found in [...]` – Daniel Hasegan Oct 29 '21 at 00:59
  • 1
    I have a M1 pro Mac (Darwin 21.6.0) and I followed the steps above but got this error - ` File "/opt/homebrew/Caskroom/miniforge/base/lib/python3.10/site-packages/Cython/Distutils/old_build_ext.py", line 157, in __getattr__ return _build_ext.build_ext.__getattr__(self, name) File "/opt/homebrew/Caskroom/miniforge/base/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 105, in __getattr__ raise AttributeError(attr) AttributeError: fcompiler. Did you mean: 'compiler'? [end of output]` i would really appreciate any help – randomseed42 Aug 28 '22 at 07:59
65

This one worked for me after wasting hours:

pip install --pre -i https://pypi.anaconda.org/scipy-wheels-nightly/simple scipy
Jumshud
  • 1,385
  • 1
  • 13
  • 19
  • 2
    Yes.. Indeed. After countless hours. This worked on MacBook Air (M1, 2020) with BigSur Version 11.6 (20G165). If I understand correctly, is this basically from anaconda servers providing most stable version of SciPy? Not sure, how it ended up working well with Appl Silicon? Would highly appreciate your answer. – Dhruv Patel Oct 28 '21 at 01:06
  • 4
    Best answer, works easy! – Tendai Nov 07 '21 at 13:22
  • @DhruvPatel This seems to be the "nightly" version of Scipy, which means it's their latest development version. Apparently they do build for ARM for their upcoming 1.8.0 version (which they didn't do for <1.8.0 which is currently released on PyPI). Since it is a development version I would definitely not qualify this as the most stable version, but fortunately this distribution makes it easier for ARM64 users to use. – ThomasKlont Nov 08 '21 at 08:31
  • 4
    This nightly build method also worked for scikit-learn on my Apple M1, but I needed to add `--extra-index` to the command: `pip install --pre --extra-index https://pypi.anaconda.org/scipy-wheels-nightly/simple scikit-learn`. Source: https://scikit-learn.org/stable/developers/advanced_installation.html – Chris Lindgren Nov 27 '21 at 16:10
  • 1
    What for people who don't use anaconda distribution? Is there an alternative? – Edmondo Dec 15 '21 at 12:05
  • `pip3` instead of `pip` worked for me. – Celuk Jan 07 '22 at 01:59
  • This works but will build against an OpenBlas that is around 5x slower than Accelerate. – knite May 09 '22 at 16:22
  • 3
    This command worked for me beautifully for long time, but now I get errors: `Looking in indexes: https://pypi.anaconda.org/scipy-wheels-nightly/simple ERROR: Could not find a version that satisfies the requirement scipy (from versions: none) ERROR: No matching distribution found for scipy` – Lecromine Sep 14 '22 at 08:44
  • @Lecromine I'll answer to my own comment since I solved the problem. I was running on an old version of OS and support for it in scipy was removed. Updating the OS solved it. – Lecromine Sep 15 '22 at 07:47
46

This solution worked on my M1 machine with pyenv:

brew install openblas
OPENBLAS="$(brew --prefix openblas)" pip install numpy scipy
dwolfeu
  • 1,103
  • 2
  • 14
  • 21
  • 7
    This worked for me. I was also able to install `scikit-learn` using the same approach: `OPENBLAS="$(brew --prefix openblas)" pip install numpy scikit-learn` – Vladimir Ivanov Aug 01 '21 at 15:26
12

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

conda install numpy scipy matplotlib
isuruf
  • 2,323
  • 12
  • 14
  • I did. This produces a segfault whenever I import a submodule of scipy. Numpy and matplotliib are fine, though. – David Sénéchal Jan 17 '21 at 10:46
  • 5
    You can also install miniforge using brew: `brew install --cask miniforge` – climatebrad Feb 09 '21 at 16:39
  • 1
    Thank you! Installation worked for me, however I got stuck with importing the installed packages in virtual environments, where my local packages were ignored. The solution is documented here: https://stackoverflow.com/questions/66768259/how-to-install-and-import-scipy-numpy-numexpr-and-others-on-m1-apple-silicon/66768260#66768260 – MattiSG Mar 23 '21 at 21:21
  • Thank you. I think this is the best solution for someone that uses tensorflow and miniforge3 on MacBook m1 – Korpel Sep 18 '22 at 11:54
8

For me the easiest solutions:

brew install scipy

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

arkhein
  • 360
  • 3
  • 10
  • 3
    Note that if you do this, brew installs python3.9 at this location `/opt/homebrew/Cellar/python@3.9/3.9.6/bin/python3`, and you will only be able to import scipy if you run this python executable – cmoses Jul 08 '21 at 14:42
  • 1
    Do NOT install a Python package with brew. Use an env to isolate things. This is a recipe for disaster. See my answer for how it should be done for SciPy. – tupui Jan 23 '23 at 10:41
  • seconding @tupui on this – Lucas Azevedo Mar 23 '23 at 12:52
5

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}"
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
hobozero
  • 63
  • 1
  • 8
3

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).

  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29774662) – Eladtopaz Sep 09 '21 at 18:52
2

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

Danieledu
  • 391
  • 1
  • 4
  • 19
2

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
Fernando Nogueira
  • 1,302
  • 1
  • 14
  • 22
2

The following worked for me.

I'm currently using Python 3.10.8, installed using brew.

And currently, when installing numpy==1.23.4, setuptools < 60.0.0 is required.

I'm using (brew --prefix)/bin/python3 -m pip for explicitly calling the pip from python 3.10 installed by brew.

Here are the versions I've just installed.

# python         3.10.8

# pip            22.3
# setuptools     59.8.0
# wheel          0.37.1

# numpy          1.23.4
# scipy          1.9.3
# pandas         1.5.1
# scikit-learn   1.1.3
# seaborn        0.12.1
# statsmodels    0.13.2

# gcc            12.2.0
# openblas       0.3.21
# gfortran       12
# pybind11       2.10.0
# Cython         0.29.32
# pythran        0.12.0

Here are the steps I followed:

# setuptools < 60.0.0 is required for numpy==1.23.4 in Python 3.10.8
$(brew --prefix)/bin/python3 -m pip install --upgrade pip==22.3 setuptools==59.8.0 wheel==0.37.1

# uninstall numpy and pythran first
$(brew --prefix)/bin/python3 -m pip uninstall -y numpy pythran

# uninstall scipy
$(brew --prefix)/bin/python3 -m pip uninstall -y scipy

# install prerequisites (with brew)
brew install gcc
brew install openblas
brew install gfortran

# set environment variables for compilers to find openblas
export LDFLAGS="-L/opt/homebrew/opt/openblas/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openblas/include"

# install the prerequisites (with pip)
$(brew --prefix)/bin/python3 -m pip install pybind11
$(brew --prefix)/bin/python3 -m pip install Cython

# install numpy
$(brew --prefix)/bin/python3 -m pip install --no-binary :all: numpy

# install pythran after installing numpy, before installing scipy
$(brew --prefix)/bin/python3 -m pip install pythran

# install scipy
export OPENBLAS="$(brew --prefix)/opt/openblas/lib/"
$(brew --prefix)/bin/python3 -m pip install scipy

# install pandas
$(brew --prefix)/bin/python3 -m pip install pandas

# install scikit-learn
$(brew --prefix)/bin/python3 -m pip install scikit-learn

# install seaborn
$(brew --prefix)/bin/python3 -m pip install seaborn

# install statsmodels
$(brew --prefix)/bin/python3 -m pip install statsmodels
1

According to this Github issue, Scipy doesn't work on MacOS 11 (Big Sur). If none of these solutions are working for you I'd suggest updating your OS.

Digglit
  • 576
  • 1
  • 4
  • 11
1

SciPy maintainer here.

SciPy is now distributed for M1, on both conda and pip. You need at least macOS 12.0 (Monterey) and Python >= 3.8. Our support for macOS is good, meaning you should not need to do anything special or compile yourself unless you want to use a development version or older macOS versions.

If you need to compile, only use our guides (e.g. https://scipy.github.io/devdocs/dev/contributor/building.html#building-from-sources). There are tons of moving pieces and doing something else might work today, but not tomorrow.

Avoid using pip to compile SciPy. Use our developer script python dev.py build after having followed the guides I linked to setup system dependencies for your platform.

Do NOT use brew to install a Python package, use an environment. brew is great to install system dependencies or other dev tools, but should not be used to install Python packages. Down the road this is a recipe for trouble.

tupui
  • 5,738
  • 3
  • 31
  • 52
0

I use conda install scipy to resolve this problem. Conda have a custom version of scipy for Apple M1. Update macOS to 12 if you don't want to use Conda.

Hieu Tran
  • 1
  • 1