M1Macでpyenv install

# 前書き
M1Macで`pyenv install`を使おうとしたところ，一筋縄ではいかずちょっと苦戦したので，解決に至るまでの道のりを記録しておきます．

# 環境
PC: MacBookPro M1Max
OS: Ventura 13.0
pyenv: 2.3.12(homebrew)


# 発生した問題
`$ pyenv install 3.8.8`を実行したときに，以下のようなエラーが発生した．
```
configure: error: Unexpected output of 'arch' on OSX
make: *** No targets specified and no makefile found.  Stop.
```

# 結論(うまくいった方法)
libffi(筆者の環境では3.4.4でした)が入っている状態で，以下のコマンドを実行．
```
$ arch -arch x86_64 env PATH="${PATH}:/opt/homebrew/bin" CC=gcc-11 LDFLAGS="-L$(xcrun --show-sdk-path)/usr/lib" CPPFLAGS="-I/opt/homebrew/opt/libffi/include" CONFIGURE_OPTS='--with-system-ffi' pyenv install 3.8.8
```

# 試行錯誤の記録
https://qiita.com/tomtsutom0122/items/52487730001247fdc2c5 を参考に `$ arch -arch x86_64 env PATH="${PATH}:/opt/homebrew/bin" pyenv install 3.8.8` を実行したらエラーが．

```
DYLD_LIBRARY_PATH=/var/folders/hg/t09jn5797rlf88_ljgtpyj_r0000gn/T/python-build.20230524103642.27119/Python-3.8.8 ./python.exe -E -S -m sysconfig --generate-posix-vars ;\
	if test $? -ne 0 ; then \
		echo "generate-posix-vars failed" ; \
		rm -f ./pybuilddir.txt ; \
		exit 1 ; \
	fi
dyld[37131]: symbol not found in flat namespace '_libintl_bindtextdomain'
/bin/sh: line 1: 37131 Abort trap: 6           DYLD_LIBRARY_PATH=/var/folders/hg/t09jn5797rlf88_ljgtpyj_r0000gn/T/python-build.20230524103642.27119/Python-3.8.8 ./python.exe -E -S -m sysconfig --generate-posix-vars
generate-posix-vars failed
make: *** [pybuilddir.txt] Error 1
```

https://github.com/pyenv/pyenv/issues/2611#issuecomment-1422432783 に従って， `$ brew install gcc@11`をした後， `$ arch -arch x86_64 env PATH="${PATH}:/opt/homebrew/bin" CC=gcc-11 pyenv install 3.8.8`を実行したらエラー．

```
zipimport.ZipImportError: can't decompress data; zlib not available
```

https://qiita.com/makaaso-tech/items/20f6a447831eed7d0e82 に従って `$ arch -arch x86_64 env PATH="${PATH}:/opt/homebrew/bin" CC=gcc-11 LDFLAGS="-L$(xcrun --show-sdk-path)/usr/lib" pyenv 
install 3.8.8` としたらエラーこそ出なかったものの，WARNINGが出て何やら嫌な予感．

```
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.8.tar.xz...
-> https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tar.xz
Installing Python-3.8.8...
patching file 'Misc/NEWS.d/next/Build/2021-10-11-16-27-38.bpo-45405.iSfdW5.rst'
patching file configure
patching file configure.ac
python-build: use readline from homebrew
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/USERNAME/.pyenv/versions/3.8.8/lib/python3.8/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
WARNING: The Python ctypes extension was not compiled. Missing the libffi lib?
Installed Python-3.8.8 to /Users/USERNAME/.pyenv/versions/3.8.8
```

`$ brew install libffi`をやってみたところ，すでに入っていたようで，以下が出力された．
```
For compilers to find libffi you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/libffi/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"

For pkg-config to find libffi you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"
```

https://github.com/pypa/pipenv/issues/4901#issuecomment-1046217930 に従って `$ arch -arch x86_64 env PATH="${PATH}:/opt/homebrew/bin" CC=gcc-11 LDFLAGS="-L$(xcrun --show-sdk-path)/usr/lib" CPPFLAGS="-I/opt/homebrew/opt/libffi/include" CONFIGURE_OPTS='--with-system-ffi' pyenv install 3.8.8` としたところ，できた!

# 参考
https://qiita.com/tomtsutom0122/items/52487730001247fdc2c5
https://github.com/pyenv/pyenv/issues/2611#issuecomment-1422432783
https://qiita.com/makaaso-tech/items/20f6a447831eed7d0e82
https://github.com/pypa/pipenv/issues/4901