Apple Silicon (M1) MacにM1 nativeなnumpyをインストールする

2 min read読了の目安(約2300字

はじめに

M1 Macにpyenvを使ってM1 nativeなPython 3.9.1をインストールできたものの、numpyをインストール(pip3 install numpy)しようとするとインストールに失敗する。
GitHubのnumpyのIssuesに対応策が書いてあったのでそのまとめ。

インストール

$ brew install openblas
$ pip3 install cython
$ OPENBLAS="$(brew --prefix openblas)" pip3 install numpy --no-use-pep517

動作確認

動いている模様。

$ python3
Python 3.9.1 (default, Jan 14 2021, 10:29:29)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.array([1, 2])
array([1, 2])
>>>

*.soはarm64になっている。

$ file $(ag -g \\.so .pyenv/versions/3.9.1/lib/python3.9/site-packages/numpy)
.pyenv/.../numpy/core/_operand_flag_tests.cpython-39-darwin.so: Mach-O 64-bit bundle arm64
.pyenv/.../numpy/core/_multiarray_umath.cpython-39-darwin.so:   Mach-O 64-bit bundle arm64
.pyenv/.../numpy/core/_rational_tests.cpython-39-darwin.so:     Mach-O 64-bit bundle arm64
.pyenv/.../numpy/core/_umath_tests.cpython-39-darwin.so:        Mach-O 64-bit bundle arm64
.pyenv/.../numpy/core/_struct_ufunc_tests.cpython-39-darwin.so: Mach-O 64-bit bundle arm64
.pyenv/.../numpy/core/_multiarray_tests.cpython-39-darwin.so:   Mach-O 64-bit bundle arm64
.pyenv/.../numpy/linalg/lapack_lite.cpython-39-darwin.so:       Mach-O 64-bit bundle arm64
.pyenv/.../numpy/linalg/_umath_linalg.cpython-39-darwin.so:     Mach-O 64-bit bundle arm64
.pyenv/.../numpy/fft/_pocketfft_internal.cpython-39-darwin.so:  Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/bit_generator.cpython-39-darwin.so:     Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/mtrand.cpython-39-darwin.so:            Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_generator.cpython-39-darwin.so:        Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_pcg64.cpython-39-darwin.so:            Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_sfc64.cpython-39-darwin.so:            Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_mt19937.cpython-39-darwin.so:          Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_philox.cpython-39-darwin.so:           Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_bounded_integers.cpython-39-darwin.so: Mach-O 64-bit bundle arm64
.pyenv/.../numpy/random/_common.cpython-39-darwin.so:           Mach-O 64-bit bundle arm64