Search code examples
pythonscikit-learnscipyapple-m1

Installing scipy and scikit-learn on apple m1


The installation on the m1 chip for the following packages: Numpy 1.21.1, pandas 1.3.0, torch 1.9.0 and a few other ones works fine for me. They also seem to work properly while testing them. However when I try to install scipy or scikit-learn via pip this error appears:

ERROR: Failed building wheel for numpy

Failed to build numpy

ERROR: Could not build wheels for numpy which use PEP 517 and cannot be installed directly

Why should Numpy be build again when I have the latest version from pip already installed?

Every previous installation was done using python3.9 -m pip install ... on Mac OS 11.3.1 with the apple m1 chip.

Maybe somebody knows how to deal with this error or if its just a matter of time.


Solution

  • UPDATE: scikit-learn now works via pip ✅

    Just first brew install openblas - it has instructions for different processors (wikipedia)

    brew install openblas
    export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)
    export CFLAGS="-falign-functions=8 ${CFLAGS}"
    # ^ no need to add to .zshrc, just doing this once.
    pip install scikit-learn
    

    Worked great on Apple Silicon M1 🎉

    Extra details about how Pip works

    Pip downloaded the source from Pipy, then built the wheel targeting MacOS X 12.0, and arm64 (apple silicon): scikit_learn-1.0.1-cp38-cp38-macosx_12_0_arm64.whl.

    Building wheels for collected packages: scikit-learn
      Building wheel for scikit-learn (pyproject.toml) ... done
      Created wheel for scikit-learn: filename=scikit_learn-1.0.1-cp38-cp38-macosx_12_0_arm64.whl size=6364030 sha256=0b0cc9a21af775e0c8077ee71698ff62da05ab62efc914c5c15cd4bf97867b31
    Successfully built scikit-learn
    Installing collected packages: scipy, scikit-learn
    Successfully installed scikit-learn-1.0.1 scipy-1.7.3
    

    Note on Pipy: we usually download either a pre-built wheel (yay, this is excellent for reliable distribution and ensuring compatability). Or, if no prebuilt wheel exists (sad) then we download a tar.gz and build it ourselves. This happens because the authors don't publish a prebuilt wheel to Pipy, but more and more people are adding this to their CI (github actions) workflow. Building the wheel ourselves takes more cpu time, and is generally less reliable but works in this case.

    Here we are downloading a pre-built wheel that has very few limitations: it works for any version of python 3, for any os, for any architecture (like amd64 or arm64): click-8.0.3-py3-none-any.whl

    Collecting click>=7.0
      Downloading click-8.0.3-py3-none-any.whl
    

    Here apparently we had no wheel available, so we have to build it ourselves with setuptools running setup.py.

    Collecting grpcio>=1.28.1
      Downloading grpcio-1.42.0.tar.gz (21.3 MB)
         |████████████████████████████████| 21.3 MB 12.7 MB/s
      Preparing metadata (setup.py) ... done
    
    ## later in the process it installs using setuptools 
    Running setup.py install for grpcio ... done
    

    Good luck and happy piping.