setoya-blog

システム開発技術、データ分析関連でお勉強したことや、山奥生活を綴る、テンション低めなブログです。

macOS Big Surでpyenvでpythonをインストールするとエラーが発生する

% pyenv install 3.8.5

とかやると、以下のようなエラーが発生して困っていた。

 % pyenv install 3.8.5
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.5.tar.xz...
-> https://www.python.org/ftp/python/3.7.1/Python-3.8.5.tar.xz
Installing Python-3.8.5...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 11.3.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/r_/8tv1tt8d37d5v3pp5y3lhcsr0000gn/T/python-build.20210506153411.88786
Results logged to /var/folders/r_/8tv1tt8d37d5v3pp5y3lhcsr0000gn/T/python-build.20210506153411.88786.log

Last 10 log lines:
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/var/folders/r_/8tv1tt8d37d5v3pp5y3lhcsr0000gn/T/python-build.20210506153411.88786/Python-3.8.5':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.

で、調べていくと、おそらく、以下の問題と同じことが発生している。

github.com

issueの途中( https://github.com/pyenv/pyenv/issues/1643#issuecomment-695350501

)で指摘されているように、以下のように必要なパッケージをインストール後に、コンパイルに必要な環境変数を定義してからインストールを実行することで、一応インストールはできた(対処療法的な解決策だとは思うが…)。

自分はIntel Macでやったけど、Apple silicon Macの場合は、/usr/localではなく、/usr/homebrewを使うのではないかと思う。Intel Mac以外でこの問題が発生するのか知らないけど。

#!/bin/bash
echo "Installing Dependencies"
brew install zlib
brew install sqlite
brew install bzip2
brew install libiconv
brew install libzip
echo "Done!"
echo
echo -e "Setting Environment Variables"
export SYSTEM_VERSION_COMPAT=1
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/zlib/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/zlib/include"
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/sqlite/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/sqlite/include"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /usr/local/opt/zlib/lib/pkgconfig"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /usr/local/opt/sqlite/lib/pkgconfig"
echo "Done!"
echo
echo "Installing Python 3.8.5"
pyenv install 3.8.5
echo "Done!"
exit 0