`require': cannot load such file -- openssl (LoadError) というエラーが出てRuby 2.7.6がインストールできない場合の対処法

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

```
$ rbenv install 2.7.6
/opt/homebrew/bin/ruby-build: line 193: needs_openssl_101_111: command not found
Downloading ruby-2.7.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.6.tar.bz2
Installing ruby-2.7.6...
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.20220729175045.94263.4iI0Kk
Results logged to /var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220729175045.94263.log

Last 10 log lines:
	from ./tool/rbinstall.rb:846:in `block (2 levels) in install_default_gem'
	from ./tool/rbinstall.rb:279:in `open_for_install'
	from ./tool/rbinstall.rb:845:in `block in install_default_gem'
	from ./tool/rbinstall.rb:835:in `each'
	from ./tool/rbinstall.rb:835:in `install_default_gem'
	from ./tool/rbinstall.rb:799:in `block in <main>'
	from ./tool/rbinstall.rb:950:in `block in <main>'
	from ./tool/rbinstall.rb:947:in `each'
	from ./tool/rbinstall.rb:947:in `<main>'
make: *** [do-install-all] Error 1
```

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

```
generating arm64-darwin21-fake.rb
arm64-darwin21-fake.rb updated
./miniruby -I./lib -I. -I.ext/common  ./tool/runruby.rb --extout=.ext  -- --disable-gems -r./arm64-darwin21-fake ./tool/rbinstall.rb --make="make" --dest-dir="" --extout=".ext" --mflags="" --make-flags="" --data-mode=0644 --prog-mode=0755 --installed-list .installed.list --mantype="doc" --rdoc-output=".ext/rdoc" --html-output=".ext/html"
installing binary commands:         /Users/jnito/.rbenv/versions/2.7.6/bin
installing base libraries:          /Users/jnito/.rbenv/versions/2.7.6/lib
installing arch files:              /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/2.7.0/arm64-darwin21
installing pkgconfig data:          /Users/jnito/.rbenv/versions/2.7.6/lib/pkgconfig
installing extension objects:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/2.7.0/arm64-darwin21
installing extension objects:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/site_ruby/2.7.0/arm64-darwin21
installing extension objects:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/vendor_ruby/2.7.0/arm64-darwin21
installing extension headers:       /Users/jnito/.rbenv/versions/2.7.6/include/ruby-2.7.0/arm64-darwin21
installing extension scripts:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/2.7.0
installing extension scripts:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/site_ruby/2.7.0
installing extension scripts:       /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/vendor_ruby/2.7.0
installing extension headers:       /Users/jnito/.rbenv/versions/2.7.6/include/ruby-2.7.0/ruby
installing rdoc:                    /Users/jnito/.rbenv/versions/2.7.6/share/ri/2.7.0/system
installing html-docs:               /Users/jnito/.rbenv/versions/2.7.6/share/doc/ruby
installing capi-docs:               /Users/jnito/.rbenv/versions/2.7.6/share/doc/ruby
installing command scripts:         /Users/jnito/.rbenv/versions/2.7.6/bin
installing library scripts:         /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/2.7.0
installing common headers:          /Users/jnito/.rbenv/versions/2.7.6/include/ruby-2.7.0
installing manpages:                /Users/jnito/.rbenv/versions/2.7.6/share/man (man1, man5)
installing default gems from lib:   /Users/jnito/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0 (build_info, cache, doc, extensions, gems, specifications)
                                    benchmark 0.1.0
/private/var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220729075029.41558.k9QOlH/ruby-2.7.6/lib/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- openssl (LoadError)
        from /private/var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220729075029.41558.k9QOlH/ruby-2.7.6/lib/rubygems/core_ext/kernel_require.rb:83:in `require'
        from /private/var/folders/r1/fsj20_kx4tj5b9why4z15nmw0000gn/T/ruby-build.20220729075029.41558.k9QOlH/ruby-2.7.6/lib/rubygems/specification.rb:2430:in `to_ruby'
        from ./tool/rbinstall.rb:846:in `block (2 levels) in install_default_gem'
        from ./tool/rbinstall.rb:279:in `open_for_install'
        from ./tool/rbinstall.rb:845:in `block in install_default_gem'
        from ./tool/rbinstall.rb:835:in `each'
        from ./tool/rbinstall.rb:835:in `install_default_gem'
        from ./tool/rbinstall.rb:799:in `block in <main>'
        from ./tool/rbinstall.rb:950:in `block in <main>'
        from ./tool/rbinstall.rb:947:in `each'
        from ./tool/rbinstall.rb:947:in `<main>'
make: *** [do-install-all] Error 1
```

``ruby-2.7.6/lib/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- openssl (LoadError)``というメッセージが見えるので、おそらく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 2.7.6                             
/opt/homebrew/bin/ruby-build: line 193: needs_openssl_101_111: command not found
Downloading ruby-2.7.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.6.tar.bz2
Installing ruby-2.7.6...
ruby-build: using readline from homebrew
Installed ruby-2.7.6 to /Users/jnito/.rbenv/versions/2.7.6
```

## Tips

2.7.6をインストールしたあと、`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 3.0.4をインストールしたときもよく似たエラーが発生しました。

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