rbenvで古いrubyが使えなくなったので、rbenv再インストールからやり直す

※Qiitaより引越

前提

  • Ruby on Railsで古いruby(2.3.0),rails(4.1.5)を使いたい。
  • MacOS Catalina 10.15.5
  • nokogiriのエラーから、rbenv再インストールとかやってたら、rbenv: gem: command not foundになった。
  • rbenv install 2.3.0が実行できず、opensslが必要と言われている所からです。

※ 思い出しながらなので少し曖昧です。

内容

opensslがインストールできないのを解消

rbenv install 2.3.0
The Ruby openssl extension was not compiled.
BUILD FAILED

↑みたいに怒られる。

brew install openssl
brew reinstall openssl

brew reinstall opensslするように促されるので実行。

Warningで以下のエラー。

Error: An exception occurred within a child process:
 FormulaUnavailableError: No available formula with the name "/usr/local/opt/php@7.1/.brew/php@7.1.rb".

brew doctorでも↓が出てる。

Warning: Some installed kegs have no formulae!
This means they were either deleted or installed with `brew diy`.
You should find replacements for the following formulae:
 php@7.1

PHPの環境を作ってるわけでもなく、 元々PHP依存の何かを使っていた訳でもない。 おそらく適当にコピペでコマンドを試してしまった(絶対ダメ)のだろうと↓を実行。

brew uninstall php@7.1

brew doctorのWarningも消え、 brew reinstall opensslが通りました。

rbenv install 2.3.0できないことの解消

この時点で、 rbenv install 2.3.0がコケる。 rbenv install 3.0.2とかは大丈夫でした。

ruby2.4以下は、openssl@1.1ではビルド失敗するらしい。

なので↓のコマンドを使い、openssl@1.1を使ってビルドしていることをまず確認。

echo $RUBY_CONFIGURE_OPTS

次に↓でopenssl@1.0に変更、変更できた事を確認。

$echo 'RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/opt/openssl@1.0"' >> .bash_profile
source ~/.bash_profile
echo $RUBY_CONFIGURE_OPTS

openssl@1.0がインストールされていない場合、 インストールが必要です。

bundle install〜rails s

gem install bundler

新しいbundlerが指定されるので、 古いrubyではコケる。

gem install bundler -v 1.17.3

でbundlerをインストール完了。

bundle install --path vendor/bundle
bin/rails s

でサーバー起動完了。

感想

  • nokogiriやらopenssl,therubyracer,tzinfo-data....のエラーは頻繁に出会うけどすぐ解消できるイメージで雑にやってしまいました。
  • そのため、MacRails使いたいのにubuntuの人とか、PHPの人とかのコマンドを適当にコピペしてしまった。無駄に時間を使った気がします。
  • 恐れすぎもよくないけど、自分の中では論理的だと思える仮説を作ってから実施した方が良いですね。

参考サイト

https://www.kasacchiful.net/blog/2020/03/27/rbenv-openssl-error/ https://stackoverflow.com/questions/64816796/ruby-install-aborted-due-to-missing-extensions-openssl-readline-zlib-compilat