M1 Mac(macOS 13 Ventura、Xcode 14)で`rbenv install 3.1.2`に失敗する

# TL;DR
```bash
brew upgrade ruby-build
brew install openssl@3 readline libyaml gmp
RUBY_CONFIGURE_OPTS="--disable-install-doc --disable-shared --with-openssl-dir=$(brew --prefix openssl@3)" rbenv install 3.1.2
```

# 環境
- M1 MacBook Air
- macOS Ventura 13.3.1
- Xcode Version 14.3 (14E222b)
- Homebrew 4.0.16
- rbenv 1.2.0

# 現象
`rbenv install 3.1.2`を実行すると下記のように`BUILD FAILED`となりインストールに失敗する

```bash
$ rbenv install 3.1.2
To follow progress, use 'tail -f /var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502191006.36912.log' or pass --verbose
Downloading openssl-3.0.8.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/6c13d2bf38fdf31eac3ce2a347073673f5d63263398f1f69d0df4a41253e4b3e
Installing openssl-3.0.8...
Installed openssl-3.0.8 to /Users/xxx/.rbenv/versions/3.1.2

Downloading ruby-3.1.2.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.2.tar.gz
Installing ruby-3.1.2...
ruby-build: using readline from homebrew
ruby-build: using gmp from homebrew

BUILD FAILED (macOS 13.3.1 using ruby-build 20230428)

Inspect or clean up the working tree at /var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502191006.36912.BlRzeE
Results logged to /var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502191006.36912.log

Last 10 log lines:
	from ./tool/rbinstall.rb:899:in `block in install'
	from ./tool/rbinstall.rb:713:in `no_write'
	from ./tool/rbinstall.rb:899:in `install'
	from ./tool/rbinstall.rb:1068:in `block (2 levels) in <main>'
	from ./tool/rbinstall.rb:1043:in `foreach'
	from ./tool/rbinstall.rb:1043:in `block in <main>'
	from ./tool/rbinstall.rb:1127:in `block in <main>'
	from ./tool/rbinstall.rb:1124:in `each'
	from ./tool/rbinstall.rb:1124:in `<main>'
make: *** [do-install-all] Error 1
```

ログファイルの内容を参照すると下記のようなエラーログが出力されている
```log
...
dyld[36317]: tried: '/private/var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502210049.17269.vAsgpq/ruby-3.1.2/libruby.3.1.dylib' (mach-o file, but is an incompatible architecture (have
 'arm64', need 'arm64e')), '/System/Volumes/Preboot/Cryptexes/OS/private/var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502210049.17269.vAsgpq/ruby-3.1.2/libruby.3.1.dylib' (no such fil
e), '/private/var/folders/8y/_tfhyy5x72l7sjd889snr6j80000gn/T/ruby-build.20230502210049.17269.vAsgpq/ruby-3.1.2/libruby.3.1.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need '
arm64e'))
...
```

# 調査結果
- 本現象（`incompatible architecture (have 'arm64', need '
arm64e')`）はM1かつXcode 14の環境下で`rbenv install 3.1.x`を実行した際に発生する
    - Ruby 3.1.2と3.1.3は本稿のワークアラウンドで回避できた（その他の3.1.0/3.1.1などは未確認）
- macOS 13(Ventura)にアップデートしていない場合はXcodeを13にダウングレードすることで回避できる模様（2023/5/3現在、参考リンク先ではMontereyの場合はXcode 14にアップデートしないことが推奨されている）
- Venturaの場合はXcode 13にダウングレードできないため本稿のワークアラウンドで回避する必要がある

# 対策
[ruby-buildのWiki](https://github.com/rbenv/ruby-build/wiki)と、Wikiから参考としてリンクされている[ワークアラウンドのスレッド](https://github.com/rbenv/ruby-build/discussions/1961#discussioncomment-4031745)に記載されている内容に倣う。

- ruby-buildを更新する
	- `brew upgrade ruby-build`
		> If you have trouble installing a Ruby version, first try to update ruby-build to get the latest bug fixes and Ruby definitions.
		> 
		> First locate it on your system:
		> 
		> ```bash
		> which ruby-build
		> ls "$(rbenv root)"/plugins
		> ```
		> If it's in /opt/homebrew/bin or /usr/local/bin on a Mac, you've probably installed it via Homebrew:
		> ```bash
		> brew upgrade ruby-build
		> ```
        https://github.com/rbenv/ruby-build/wiki#updating-ruby-build
- Ruby 3.1以上はOpenSSL 3を必要とするためHomebrewでインストールする
	- `brew install openssl@3 readline libyaml gmp`
		> Ruby 3.1 and above requires OpenSSL 3:
		> 
		> ```bash
		> brew install openssl@3 readline libyaml gmp
		> export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
		> ```
        https://github.com/rbenv/ruby-build/wiki#macos
- M1のエラーを回避するためにオプションを付けてrbenv installを実行する
	- `RUBY_CONFIGURE_OPTS="--disable-install-doc --disable-shared --with-openssl-dir=$(brew --prefix openssl@3)" rbenv install 3.1.2`
		> Disable building the shared library with RUBY_CONFIGURE_OPTS=--disable-shared
		>
		> For example, the full invocation you can try to link to Homebrew OpenSSL:
		> 
		> ```bash
		> RUBY_CONFIGURE_OPTS="--disable-install-doc --disable-shared --with-openssl-dir=$(brew --prefix openssl@3)" rbenv install 3.1.2
		> ```
        https://github.com/rbenv/ruby-build/discussions/1961#discussioncomment-4031745

# 参考
- https://github.com/rbenv/ruby-build/wiki
- https://github.com/rbenv/ruby-build/discussions/1961#discussioncomment-4031745
- https://bugs.ruby-lang.org/issues/18912