pyenvでインストール時に「BUILD FAILED (OS X 12.6.3 using python-build 1.2.21)」が発生した時の対処メモ

Mac Monterey(12.6.3)環境にて、pyenvでPython3.9.0をインストールしたときにエラーが発生した際のメモです。

エラー内容

$ pyenv install 3.9.0
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.0.tar.xz...
-> https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
Installing Python-3.9.0...
python-build: use readline from homebrew
BUILD FAILED (OS X 12.6.3 using python-build 1.2.21)

Inspect or clean up the working tree at /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230220212815.24652

Results logged to /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230220212815.24652.log

Last 10 log lines:
checking for python3.9... python3.9
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "darwin"
checking for gcc... clang
checking whether the C compiler works... no

configure: error: in `/var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230220212815.24652/Python-3.9.0':
configure: error: C compiler cannot create executables
See `config.log' for more details
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
configure: error: C compiler cannot create executables
See `config.log' for more details
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

対処時の手順は下記の通り

1. brewをアップデート&アップグレード

2. CommandLineTools(xcode)の再インストール

3. pyenvの再インストール

4. 結局は。。。

1. brewをアップデート&アップグレード

MacOSをMontereyにしてから何もしてなかったので、先ずは最新状態にしておきます。 環境にもよると思いますが、私の場合、1時間程度かかりました。

$ brew update
$ brew upgrade

2. CommandLineToolsの再インストール

1. CommandLineTools(xcode)をアンインスール

$ sudo rm -rf /Library/Developer/CommandLineTools

2. macを再起動

3. CommandLineTools(xcode)をインスール

これも環境によると思いますが、数十分はかかるかと。。

$ xcode-select --install

とりあえず、インストールして様子を見てみましょう。

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

BUILD FAILED (OS X 12.6.3 using python-build 1.2.21)

Inspect or clean up the working tree at /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223193128.13744
Results logged to /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223193128.13744.log

Last 10 log lines:
checking for --with-cxx-main=<compiler>... no
checking for clang++... no
configure:

  By default, distutils will build C++ extension modules with "clang++".
  If this is not intended, then set CXX on the configure command line.
  
checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
make: *** No targets specified and no makefile found.  Stop.

BUILD FAILED (OS X 12.6.3 using python-build 1.2.21) は出ていますが、エラーは減っているようです。 clang++というのが少し引っかかりますね。

3. pyenvの再インストール

pyenvをアンインストールして再インストールしてみます。 手順は下記Githubを参考にしてます。

github.com

$ brew uninstall pyenv
$ brew install pyenv

次にZshに環境を設定します。

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
$ echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc

設定を有効化

$ source ~/.zshrc

以上で環境は整ったのではと思い、いざ、インストール!

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

BUILD FAILED (OS X 12.6.3 using python-build 1.2.21)

Inspect or clean up the working tree at /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223201003.30194
Results logged to /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223201003.30194.log

Last 10 log lines:
checking for --with-cxx-main=<compiler>... no
checking for clang++... no
configure:

  By default, distutils will build C++ extension modules with "clang++".
  If this is not intended, then set CXX on the configure command line.
  
checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
make: *** No targets specified and no makefile found.  Stop.

ダメじゃん _| ̄|○

clang++が気になっていたのでgccを指定して再トライ

$ brew install gcc
$ CC=/opt/homebrew/bin/gcc-11 pyenv install 3.9.0
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.0.tar.xz...
-> https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
Installing Python-3.9.0...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.6.3 using python-build 1.2.21)

Inspect or clean up the working tree at /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223201409.30860
Results logged to /var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223201409.30860.log

Last 10 log lines:
checking for python3.9... python3.9
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "darwin"
checking for gcc... /opt/homebrew/bin/gcc-11
checking whether the C compiler works... no
configure: error: in `/var/folders/qw/lk8d73j93t5f8ts_8lh1p7fh0000gn/T/python-build.20230223201409.30860/Python-3.9.0':
configure: error: C compiler cannot create executables
See `config.log' for more details
make: *** No targets specified and no makefile found.  Stop.

gccダメじゃん _| ̄|○

4. 結局は。。。

pyenvでインストール可能なPythonバージョンでは、

$ pyenv install -l
Available versions:
       :
  3.8.6
  3.9.0
  3.9-dev
  3.10-dev
       :

とあるので、ダメもとでPython 3.9-devをインストールしてみました。

$ pyenv install 3.9-dev
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Cloning https://github.com/python/cpython...
Installing Python-3.9-dev...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.9-dev to /Users/xxxxxxxxxxx/.pyenv/versions/3.9-dev

なんということでしょう、無事にインストールが完了したではないですかっ。

ちゃんとインストールされてます。

$ pyenv versions
  3.7.0
* 3.8.6 (set by /Users/xxxxxxxxxxx/.python-version)
  3.9-dev

バージョン切り替えもできてますね。

$ pyenv local 3.9-dev
$ python --version
Python 3.9.16+

ググってみても結論がでなかったし、インストール可能だけどインストールできなかったという記事も見かけるので、ここで手打ちとしましょう。 疲れた。。。

追記

ホームディレクトリで、localのバージョン変更するとglobalが反映されなくなるそうなので、"rm ~/.python-version"コマンドでバージョン設定ファイルを削除するとOKらしいです。

ホームディレクトリに移動して、localのバージョンを3.7.0に設定します。

$ cd ~/
$ pyenv local 3.7.0
$ pyenv versions 
  system
* 3.7.0 (set by /Users/xxxxxxxxxxx/.python-version)
  3.8.6
  3.9-dev

次に、globalのバージョンを設定しますが、3.7.0のまま変わりません。。。

$ pyenv global 3.9-dev
$ pyenv versions 
  system
* 3.7.0 (set by /Users/xxxxxxxxxxx/.python-version)
  3.8.6
  3.9-dev

"~/.python-version"を削除して、再度、globalのバージョンを設定するとちゃんと変わってますね (^^)

$ rm ~/.python-version
$ pyenv global 3.9-dev
$ pyenv versions 
  system
  3.7.0
  3.8.6
* 3.9-dev (set by /Users/xxxxxxxxxxx/.pyenv/version)

あとがき

環境は綺麗になって、Python3.9もインストールできたけど、なんかモヤっとした結果でした。 Macは好きなのですが、OSがアップグレードされた時の影響が大きいなぁ。。。