Macでpyenvを使ってPythonをインストールしようとしたときにzlib not available
エラーが発生したときの対策をメモしておきます。使用したMacのバージョンはmacOS Mojave 10.14.2
です。
事象
こんなエラーが発生しました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pyenv install 3.7.1 | |
python-build: use openssl from homebrew | |
python-build: use readline from homebrew | |
Downloading Python-3.7.1.tar.xz... | |
-> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz | |
Installing Python-3.7.1... | |
python-build: use readline from homebrew | |
BUILD FAILED (OS X 10.14.2 using python-build 20180424) | |
Inspect or clean up the working tree at /var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581 | |
Results logged to /var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581.log | |
Last 10 log lines: | |
File "/private/var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581/Python-3.7.1/Lib/ensurepip/__main__.py", line 5, in <module> | |
sys.exit(ensurepip._main()) | |
File "/private/var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581/Python-3.7.1/Lib/ensurepip/__init__.py", line 204, in _main | |
default_pip=args.default_pip, | |
File "/private/var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581/Python-3.7.1/Lib/ensurepip/__init__.py", line 117, in _bootstrap | |
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths) | |
File "/private/var/folders/1f/ncvbfn9x4t38w69c60wk01q00000gn/T/python-build.20190101065945.83581/Python-3.7.1/Lib/ensurepip/__init__.py", line 27, in _run_pip | |
import pip._internal | |
zipimport.ZipImportError: can't decompress data; zlib not available | |
make: *** [install] Error 1 |
原因
XcodeのCommand Line ToolsにmacOS Mojave 10.14
用のmacOS SDKヘッダがインストールされていないからです。
pyenvのissueにも今回のエラーが載っていました。
対策
macOS SDKヘッダを追加する必要があるようです。具体的には、以下コマンドを実行すればOKです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / |
pyenvのwikiにも対策として上記コマンドが載っています。
改めてpyenvを使ってPythonをインストールすると、うまくいきました!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pyenv install 3.7.1 | |
python-build: use openssl from homebrew | |
python-build: use readline from homebrew | |
Downloading Python-3.7.1.tar.xz... | |
-> https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz | |
Installing Python-3.7.1... | |
python-build: use readline from homebrew | |
Installed Python-3.7.1 to /Users/donchan922/.pyenv/versions/3.7.1 |
まとめ
pyenv installで「zlib not available」エラーが発生したときの対策でした。
OSSライブラリのエラーについては、GitHubのissueやwikiを見るのが信頼性も高く、情報量も多いのでおすすめです。