macのBigSurでpyenvを使用してpythonの3.8がインストールできない

目次

事象

pyenv install 3.8.2を実行すると、以下のようなエラーメッセージが出てインストールできない。

pyenv install 3.8.2 python-build: use openssl@1.1 from homebrew python-build: use readline from homebrew Downloading Python-3.8.2.tar.xz... -> https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz Installing Python-3.8.2... python-build: use readline from homebrew python-build: use zlib from xcode sdk BUILD FAILED (OS X 11.2.3 using python-build 2.3.0-1-g867f34b5) Inspect or clean up the working tree at /var/folders/qf/k41gj1k502g3l6k5ylrlk_p00000gn/T/python-build.20220504185112.22251 Results logged to /var/folders/qf/k41gj1k502g3l6k5ylrlk_p00000gn/T/python-build.20220504185112.22251.log Last 10 log lines: clang -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -DPy_BUILD_CORE -o Modules/main.o Modules/main.c clang -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -DPy_BUILD_CORE -o Modules/gcmodule.o Modules/gcmodule.c clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/hayato/.pyenv/versions/3.8.2/include -DPy_BUILD_CORE_BUILTIN -DPy_BUILD_CORE_BUILTIN -I./Include/internal -c ./Modules/posixmodule.c -o Modules/posixmodule.o ./Modules/posixmodule.c:9197:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration] ret = sendfile(in, out, offset, &sbytes, &sf, flags); ^ 1 error generated. make: * [Modules/posixmodule.o] Error 1 make: * Waiting for unfinished jobs.... 1 warning generated.

環境

  • MacOS BigSur(ver11.2.3)
  • pyenv 2.3.0

解決方法(想定)

結論から言うと1.のコマンドを実行したところインストールすることができた。しかし、それまでに色々試してしまっており複合的に解決した可能性も否定できないので、試したことを記載しておく。

1. 以下のコマンドを実行する

pyenv install --patch 3.8.0の部分を3.8.2など任意のバージョンに変更する。

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.8.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

▼参考

https://github.com/pyenv/pyenv/issues/1799#issuecomment-772994050

2. brew update

homebrewをupdateする

$ brew update

3. xcode 再インストール

$ xcode-select --install