如何在Mac M1上安装numba?已装llvmlite但pip安装失败
问题
我已通过一篇帖子成功安装llvmlite,但执行pip install numba时持续失败。请问是否有办法在Mac M1上安装numba?相关错误信息如下:
numba/core/typeconv/typeconv.cpp:30:19: error: expected expression bin.push_back({key, val}); ^ 1 error generated. numba/_dispatcher.cpp:1104:37: warning: offset of on non-POD type 'Dispatcher' [-Winvalid-offsetof] {(char*)"_can_compile", T_BOOL, offsetof(Dispatcher, can_compile), 0, NULL }, ^ ~~~~~~~~~~~ /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/stddef.h:104:24: note: expanded from macro 'offsetof' #define offsetof(t, d) __builtin_offsetof(t, d) ^ ~ 1 warning generated. error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/opt/homebrew/opt/zlib/include -I/opt/homebrew/opt/bzip2/include -I/opt/homebrew/opt/openblas/include -I/Users/kotchwane/.pyenv/versions/3.10.6/lib/python3.10/site-packages/numpy/core/include -I/Users/kotchwane/.pyenv/versions/3.10.6/include/python3.10 -c numba/core/typeconv/typeconv.cpp -o build/temp.macosx-10.16-arm64-3.10/numba/core/typeconv/typeconv.o" failed with exit status 1
解决方案
在Mac M1上安装Numba的可行方案
方法1:用conda安装(优先推荐)
M1架构下conda的依赖适配更完善,先确保安装的是支持arm64的Miniforge3(而非x86版本的Anaconda),然后执行:
conda install numba
conda会自动匹配适配M1的llvmlite和Numba版本,跳过本地编译环节,避免报错。
方法2:指定兼容版本并设置编译参数
如果坚持用pip,先确认Python版本和Numba版本兼容(Python3.10对应Numba0.56及以上),然后设置C++标准环境变量后安装:
export CXXFLAGS="-std=c++17" pip install numba==0.57.0
指定版本是因为该版本修复了M1架构下的编译问题,设置CXXFLAGS强制clang使用C++17标准,解决初始化列表的语法错误。
方法3:更新Command Line Tools
你的clang版本为13.1.6,较旧版本对M1的支持不足,更新到最新版本:
xcode-select --install
更新完成后重新执行pip install numba即可。
方法4:确保Python为arm64架构
如果你用pyenv管理环境,确认安装的是原生arm64的Python,而非转译的x86版本:
env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.10.6
重新激活环境后再安装Numba,避免架构不匹配导致的编译失败。
内容的提问来源于stack exchange,提问作者kotchwane
相关产品推荐
相关产品推荐

