M1 Macでrbenv install 3.2.0を実行するとBUILD FAILEDがでる問題と解消方法

## 忙しい人向け

rbenvでruby 3.2.0のビルドエラーに詰まった場合は以下を実行する。

```sh
brew install libyaml
rbenv install 3.2.0
```

## 動作確認環境

* M1 Max MacBook Pro 2021
* macOS Ventura 13.1

```sh
$ sw_vers 
ProductName:            macOS
ProductVersion:         13.1
BuildVersion:           22C65
```

## 事象

M1 Macにて、 `rbenv install 3.2.0` を実行するとビルドエラーが発生した。

```sh
$ rbenv install 3.2.0
To follow progress, use 'tail -f /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130113635.15578.log' or pass --verbose
Downloading openssl-3.0.7.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/83049d042a260e696f62406ac5c08bf706fd84383f945cf21bd61e9ed95c396e
Installing openssl-3.0.7...
Installed openssl-3.0.7 to /Users/notakaos/.rbenv/versions/3.2.0

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

BUILD FAILED (macOS 13.1 using ruby-build 20230124)

Inspect or clean up the working tree at /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130113635.15578.prAclB
Results logged to /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130113635.15578.log

Last 10 log lines:
        Check ext/psych/mkmf.log for more details.
*** Fix the problems, then remove these directories and try again if you want.
Generating RDoc documentation
/private/var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130113635.15578.prAclB/ruby-3.2.0/lib/yaml.rb:3: warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
uh-oh! RDoc had a problem:
cannot load such file -- psych

run with --debug for full backtrace
make: *** [rdoc] Error 1
```

## 試行錯誤

https://kenzoblog.vercel.app/posts/m1-chip

この記事を参考に、 `RUBY_CFLAGS=-W` を付けて実行してみる。

```sh
$ RUBY_CFLAGS="-w" rbenv install 3.2.0
To follow progress, use 'tail -f /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130114013.87331.log' or pass --verbose
Downloading openssl-3.0.7.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/83049d042a260e696f62406ac5c08bf706fd84383f945cf21bd61e9ed95c396e
Installing openssl-3.0.7...
Installed openssl-3.0.7 to /Users/notakaos/.rbenv/versions/3.2.0

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

BUILD FAILED (macOS 13.1 using ruby-build 20230124)

Inspect or clean up the working tree at /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130114013.87331.7iXEZE
Results logged to /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130114013.87331.log

Last 10 log lines:
        Check ext/psych/mkmf.log for more details.
*** Fix the problems, then remove these directories and try again if you want.
Generating RDoc documentation
/private/var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130114013.87331.7iXEZE/ruby-3.2.0/lib/yaml.rb:3: warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
uh-oh! RDoc had a problem:
cannot load such file -- psych

run with --debug for full backtrace
make: *** [rdoc] Error 1
```

うーん、だめか。

エラーメッセージをよくみてみると、ヒントらしき文言があった。

```
warning: It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
uh-oh! RDoc had a problem:
cannot load such file -- psych
```

`rbenv cannot load such file psych` でGoogle検索すると、以下のページがヒットした。

https://github.com/rbenv/ruby-build/discussions/2118

libyamlをインストールしなおすとよさそう？

上のページ内のコメントに従ってHomebrew経由でlibyamlを導入してみる。

```sh
# libyamlの内容確認
$ brew info libyaml
==> libyaml: stable 0.2.5 (bottled)
YAML Parser
https://github.com/yaml/libyaml
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/libyaml.rb
License: MIT
==> Dependencies
Build: autoconf ✔, automake ✘, libtool ✔
==> Analytics
install: 93,220 (30 days), 286,369 (90 days), 1,100,604 (365 days)
install-on-request: 20,505 (30 days), 55,644 (90 days), 203,652 (365 days)
build-error: 9 (30 days)

# libyamlのインストール
$ brew install libyaml
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).

You have 63 outdated formulae and 5 outdated casks installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Fetching libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/blobs/sha256:11239e8f5066c6d0
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:1
######################################################################## 100.0%
==> Pouring libyaml--0.2.5.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 351.3KB
==> Running `brew cleanup libyaml`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```

libyamlのインストールが完了したらruby 3.2.0のインストールを実行。

```sh
$ rbenv install 3.2.0
To follow progress, use 'tail -f /var/folders/tm/b75vw5997ld_vk72x2lm2qcd_jcwqg/T/ruby-build.20230130115111.63472.log' or pass --verbose
Downloading openssl-3.0.7.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/83049d042a260e696f62406ac5c08bf706fd84383f945cf21bd61e9ed95c396e
Installing openssl-3.0.7...
Installed openssl-3.0.7 to /Users/notakaos/.rbenv/versions/3.2.0

Downloading ruby-3.2.0.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.0.tar.gz
Installing ruby-3.2.0...
ruby-build: using readline from homebrew
ruby-build: using gmp from homebrew
Installed ruby-3.2.0 to /Users/notakaos/.rbenv/versions/3.2.0


NOTE: to activate this Ruby version as the new default, run: rbenv global 3.2.0
```

お！ビルド成功した！

rubyのバージョン確認をする。

```sh
$ rbenv shell 3.2.0
$ ruby --version
ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22]
```

ruby 3.2.0が動作した。めでたしめでたし。


## 参考

* https://github.com/rbenv/ruby-build/discussions/2118