MacOS(M2)下用pyenv安装Python出现arm64架构链接错误
M2 Mac上pyenv安装Python失败的解决步骤
问题现象
执行pyenv install 3.12.4时编译失败,核心错误为:
ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
完整错误日志如下:
python-build: use openssl@3 from homebrew python-build: use readline from homebrew Downloading Python-3.12.4.tar.xz... -> https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tar.xz Installing Python-3.12.4... python-build: use tcl-tk from homebrew python-build: use readline from homebrew python-build: use ncurses from homebrew python-build: use zlib from xcode sdk BUILD FAILED (OS X 15.3 using python-build 20180424) Inspect or clean up the working tree at /var/folders/s3/gx0d1ysd07n3yfj0dss896l80000gn/T/python-build.20250203195555.56142 Results logged to /var/folders/s3/gx0d1ysd07n3yfj0dss896l80000gn/T/python-build.20250203195555.56142.log Last 10 log lines: __locale_localeconv in _localemodule.o __locale_localeconv in _localemodule.o __locale_localeconv in _localemodule.o __locale_localeconv in _localemodule.o "_libintl_textdomain", referenced from: __locale_textdomain in _localemodule.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [Programs/_freeze_module] Error 1 make: *** Waiting for unfinished jobs....
环境信息:M2芯片MacBook Pro,macOS 15.3,pyenv 2.5.1(Homebrew安装)。
已尝试但无效的操作
- 执行
AR=/usr/bin/ar pyenv install 3.12.4 - 在
~/.zshrc中添加环境变量:
export CC="clang" export CXX="$(CC)++" export LDFLAGS="-L/usr/local/opt/zlib/lib, -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib" export CPPFLAGS="-I/usr/local/opt/llvm/include, -I/usr/local/opt/llvm/include/c++/v1/"
- 移除并重新安装Xcode Command Line Tools:
sudo rm -rf /Library/Developer/CommandLineTools xcode-select --install
- 卸载Homebrew版pyenv并从GitHub重新安装
分步解决方案
步骤1:安装/更新gettext库
错误中的_libintl_textdomain属于gettext组件,需确保Homebrew安装的gettext被正确识别:
brew install gettext brew link gettext --force
步骤2:设置正确的环境变量
在~/.zshrc中添加以下配置(替换之前无效的环境变量):
export PATH="/opt/homebrew/opt/gettext/bin:$PATH" export LDFLAGS="-L/opt/homebrew/opt/gettext/lib" export CPPFLAGS="-I/opt/homebrew/opt/gettext/include" export PKG_CONFIG_PATH="/opt/homebrew/opt/gettext/lib/pkgconfig"
执行命令使配置生效:
source ~/.zshrc
步骤3:清理pyenv缓存
删除之前下载的Python安装包缓存,避免残留问题:
rm -rf ~/.pyenv/cache/*
步骤4:重新执行安装命令
pyenv install 3.12.4
备选方案:指定编译参数安装
如果以上步骤仍失败,尝试直接指定依赖库路径执行安装:
LDFLAGS="-L/opt/homebrew/opt/gettext/lib" CPPFLAGS="-I/opt/homebrew/opt/gettext/include" pyenv install 3.12.4
内容的提问来源于stack exchange,提问作者Victor
相关产品推荐
相关产品推荐

