Installing scipy and scikit-learn on apple m1

We Are Going To Discuss About Installing scipy and scikit-learn on apple m1. So lets Start this Python Article.

Installing scipy and scikit-learn on apple m1

  1. How to solve Installing scipy and scikit-learn on apple m1

    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 # ==0.24.1 if you want
    Worked great on M1 Pro πŸŽ‰
    Extra details
    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.

  2. Installing scipy and scikit-learn on apple m1

    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 # ==0.24.1 if you want
    Worked great on M1 Pro πŸŽ‰
    Extra details
    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.

Solution 1

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 # ==0.24.1 if you want

Worked great on M1 Pro πŸŽ‰

Extra details

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.

Original Author Under-qualified NASA Intern Of This Content

Solution 2

Finally managed to sort this using below steps:

  1. install python 3.9 using brew
 (brew install [email protected])
  2. setup PATH to pick brew python3 first (export PATH=opt/homebrew/bin:$PATH)
  3. Run below commands to install scipy and scikit-learn


    >> /opt/homebrew/bin/brew install openblas
    >> export OPENBLAS=$(/opt/homebrew/bin/brew --prefix openblas)
    >> export CFLAGS="-falign-functions=8 ${CFLAGS}"
    >> git clone https://github.com/scipy/scipy.git
    >> cd scipy
    >> git submodule update --init
    >> /opt/homebrew/bin/pip3 install .
    >> /opt/homebrew/bin/pip3 install scikit-learn

  1. go to the folder where virtual env needs to be created and run python3 -m venv --system-site-packages .venv

Original Author FossilBlade Of This Content

Solution 3

Please see this note of scikit-learn about

Installing on Apple Silicon M1 hardware

The recently introduced macos/arm64 platform (sometimes also known as macos/aarch64) requires the open source community to upgrade the build configuation and automation to properly support it.

At the time of writing (January 2021), the only way to get a working installation of scikit-learn on this hardware is to install scikit-learn and its dependencies from the conda-forge distribution, for instance using the miniforge installers:

https://github.com/conda-forge/miniforge

The following issue tracks progress on making it possible to install scikit-learn from PyPI with pip:

https://github.com/scikit-learn/scikit-learn/issues/19137

Original Author afsharov Of This Content

Solution 4

As of March 14 (pie day!) 2022, all I had to do was type the following in a conda environment:

conda install --channel=conda-forge scikit-learn

That was it. I am running a MacBook Pro (13″, M1, 2020) with MacOS 11.6, and python 3.8 in the conda environment.

Original Author BLimitless Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment