LoginSignup
54
30

More than 3 years have passed since last update.

M1版とIntel版のHomebrewを併用するときpyenvがうまく動かない問題を解決する

Last updated at Posted at 2021-03-06

解決方法

以下のコマンドを実行すればPython 3.8.7をインストールできます。

arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.8.7

上記のコマンドに至るまで試したこと

普通にインストールしようとします。

pyenv install 3.8.7

すると以下の通りエラーが出ました。

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

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

Inspect or clean up the working tree at /var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/python-build.20210306152153.56051
Results logged to /var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/python-build.20210306152153.56051.log

Last 10 log lines:
checking size of _Bool... 1
checking size of off_t... 8
checking whether to enable large file support... no
checking size of time_t... 8
checking for pthread_t... yes
checking size of pthread_t... 8
checking size of pthread_key_t... 8
checking whether pthread_key_t is compatible with int... no
configure: error: Unexpected output of 'arch' on OSX
make: *** No targets specified and no makefile found.  Stop.

error: Unexpected output of 'arch' on OSX とあるので、pyenvはM1チップには対応していないみたいです。

x86 で実行するように指定してインストールしてみます。

arch -arch x86_64 pyenv install 3.8.7

しかし、これも失敗してしまいます。

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.7.tar.xz...
-> https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tar.xz
Installing Python-3.8.7...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


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

Inspect or clean up the working tree at /var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/python-build.20210306152407.59324
Results logged to /var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/python-build.20210306152407.59324.log

Last 10 log lines:
    fi
Looking in links: /var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/tmpbfvybuqp
Processing /private/var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/tmpbfvybuqp/setuptools-49.2.1-py3-none-any.whl
Processing /private/var/folders/hr/58r906ys4vx_tm4gkx01h9n00000gn/T/tmpbfvybuqp/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: The script easy_install-3.8 is installed in '/Users/username/.pyenv/versions/3.8.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts pip3 and pip3.8 are installed in '/Users/username/.pyenv/versions/3.8.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.2.3 setuptools-49.2.1

一番最後の行に Successfully と書いてありますが、騙されてはいけません。

WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

readlineopenssl のコンパイルに失敗してしまっているみたいです。そこで、ライブラリのパスを指定して上げれば解決するのではないかと思い、以下のコマンドを実行しました。

export CFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include"
export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix readline)/lib"
arch -arch x86_64 pyenv install 3.8.7
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.7.tar.xz...
-> https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tar.xz
Installing Python-3.8.7...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Installed Python-3.8.7 to /Users/username/.pyenv/versions/3.8.7

正常終了しますが、インタプリタを起動すると上矢印キーで履歴を遡れなかったり、Ctrl + U で文字を行頭まで消すといった操作ができなかったり不具合があります。

それもそのはずで、WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? と警告がある通り、文字操作のライブラリがコンパイルできていないのです。
use readline from homebrew と書いてあるからリンクされているはずです。しかし、ここで自分のMacに2つのHomebrewを入れていたことを思い出します。もしかしたら、M1版のHomebrew(/opt/homebrew/bin)とIntel版のHomebrew(/usr/local/bin)で重複が起きているかもしれないと思い、再インストールしてみることにしました。

一旦インストールしたものを消します。

pyenv uninstall 3.8.7
export CFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include"
export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix openssl)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix readline)/lib"
arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.8.7
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.7.tar.xz...
-> https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tar.xz
Installing Python-3.8.7...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.7 to /Users/username/.pyenv/versions/3.8.7

エラー・警告が無くなり正常にインストールできました🎉🎉🎉🎉
しかし、冒頭で紹介したコマンドでは $CFLAGS$LDFLAGS を指定していませんでした。実は、この部分の設定はpyenvが勝手にやってくれるため必要ありません。
以下のように実行するだけでPythonインタプリタのインストールができます。

arch -arch x86_64 env PATH=${PATH/\/opt\/homebrew\/bin:/} pyenv install 3.8.7
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.7.tar.xz...
-> https://www.python.org/ftp/python/3.8.7/Python-3.8.7.tar.xz
Installing Python-3.8.7...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.7 to /Users/username/.pyenv/versions/3.8.7
54
30
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
30