"The Ruby openssl extension was not compiled."というエラーが出てRuby 3.0.4がインストールできない場合の対処法

## 発生した問題
Ruby 3.0.4をインストールしようとしたら以下のようなエラーが出ました。

```
$ rbenv install 3.0.4
/opt/homebrew/bin/ruby-build: line 193: needs_openssl_101_111: command not found
Downloading ruby-3.0.4.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.4.tar.gz
Installing ruby-3.0.4...
ruby-build: using readline from homebrew

BUILD FAILED (macOS 12.4 using ruby-build 20220324)

Inspect or clean up the working tree at /var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220730162159.15161.Qb26BR
Results logged to /var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220730162159.15161.log

Last 10 log lines:
installing bundled gem cache:       /Users/jnito/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/cache
The Ruby openssl extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
  --prefix=/Users/jnito/.rbenv/versions/3.0.4
  --enable-shared
  --with-readline-dir=/opt/homebrew/opt/readline
  CC=clang
  LDFLAGS=-L/Users/jnito/.rbenv/versions/3.0.4/lib 
  CPPFLAGS=-I/Users/jnito/.rbenv/versions/3.0.4/include 
```

ログを見るとこんな感じ（最後の方だけ抜粋）。

```
installing bundled gems:            /Users/jnito/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0
                                    minitest 5.14.2
                                    power_assert 1.2.0
                                    rake 13.0.3
                                    test-unit 3.3.7
                                    rexml 3.2.5
                                    rss 0.2.9
                                    typeprof 0.15.2
                                    rbs 1.4.0
installing bundled gem cache:       /Users/jnito/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/cache
The Ruby openssl extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
  --prefix=/Users/jnito/.rbenv/versions/3.0.4
  --enable-shared
  --with-readline-dir=/opt/homebrew/opt/readline
  CC=clang
  LDFLAGS=-L/Users/jnito/.rbenv/versions/3.0.4/lib 
  CPPFLAGS=-I/Users/jnito/.rbenv/versions/3.0.4/include 
```

``The Ruby openssl extension was not compiled.``というメッセージが見えるので、おそらくopenssl関連の問題が起きているものと思われます。

## 解決策

ruby-buildのwikiに従って、以下のコマンドを実行します。

https://github.com/rbenv/ruby-build/wiki#suggested-build-environment

```
brew install openssl@1.1 readline libyaml
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
```

再度インストールを試すとインストールできました！🎉

```
$ rbenv install 3.0.4
/opt/homebrew/bin/ruby-build: line 193: needs_openssl_101_111: command not found
Downloading ruby-3.0.4.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.4.tar.gz
Installing ruby-3.0.4...
ruby-build: using readline from homebrew
Installed ruby-3.0.4 to /Users/jnito/.rbenv/versions/3.0.4
```

## Tips

3.0.4をインストールしたあと、`brew upgrade rbenv ruby-build`を実行すると以下のようなメッセージが表示されました。

```
ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.

To link Rubies to Homebrew's OpenSSL 1.1 (which is upgraded) add the following
to your ~/.zshrc:
  export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Note: this may interfere with building old versions of Ruby (e.g <2.4) that use
OpenSSL <1.1.
```

`RUBY_CONFIGURE_OPTS`の環境変数は`~/.zshrc`に登録しておくのがお勧めみたいですね。

## 実行環境

- MacBook Pro (13-inch, M1, 2020)
- macOS Monterey 12.4

## 参考文献

https://github.com/rbenv/ruby-build/discussions/1940#discussioncomment-2230774

## あわせて読みたい
Ruby 2.7.6をインストールしたときもよく似たエラーが発生しました。

https://qiita.com/jnchito/items/eb9cf24cbd29e1471e45