LoginSignup
2
1

More than 3 years have passed since last update.

Ruby 2.5.2 から2.7.1にバージョンアップでやったこと

Last updated at Posted at 2020-12-21

はじめに

AWS上で動いているWebサイトのRubyのバージョンが2.5.2であり、このバージョンのEOL(End Of Life)が2020/12/25で、日付が近づいてきた。
そのため、Rubyをバージョンアップすることにした。
(以下 AWS上の警告の抜粋)

 ###### WARNING:
remote: 
remote:        Potential EOL Ruby Version
remote:        
remote:        You are using a Ruby version that has either reached its End of Life (EOL)
remote:        or will reach its End of Life on December 25th of this year.
remote:        
remote:        We suggest you upgrade to Ruby 2.6.x or later
remote:        
remote:        Once a Ruby version becomes EOL, it will no longer receive
remote:        security updates from Ruby core and may have serious vulnerabilities.
remote:        
remote:        Please upgrade your Ruby version.
remote:        
remote:        For a list of supported Ruby versions see:
remote:          https://devcenter.heroku.com/articles/ruby-support#supported-runtimes

前提条件

 ・ AWS Cloud9で動作しているRubyのバージョンアップ
 ・ Ruby 2.5.2から2.7.1にバージョンアップ
 ・ Rails 5.2.4.3
・ AWS上のバージョンアップなので関係ないと思うが、MacOS 10.15.7(Catalina)でAWSにアクセスして作業する

バージョンアップ

インストール可能なRubyのversionを調べる

$ rbenv install --list

インストールしたいRubyのversionを指定してバージョンアップを実行する

$ rbenv install 2.7.1

インストールしたRubyのversionを確認する

$ rbenv versions 
 * 2.7.1 (set by /home/ec2-user/environment/eventorg/.ruby-version)

環境全体にRubyバージョンを反映したい時

$ rbenv global 2.7.1

特定のプロジェクトのみ反映したい時

$ rbenv local 2.7.1

次に、Gemfile内でRubyのバージョンを2.7.1に指定して、rubyのバージョンをコマンドで確認すると、

$ ruby -v
  Your Ruby version is 2.5.2, but your Gemfile specified 2.7.1

とエラーが出る。

rvm install "ruby-2.7.1" を実行した後、ruby -vを実行すると、

$ ruby -v
 ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

と出力され、エラーが消える。

新しいRubyのバージョンにGemをインストールする

$ gem install bundler

Gemfile内のRubyのバージョンが2.7.1となっている上で、下記を実行する

$ bundle install --path=vendor/bundle

新しいバージョンのRubyをインストールすると、railsコマンドが使えなくなってしまう

$ rails --version
rbenv: rails: command not found

そのため、gem をアップデートする

$ gem update --system

bundlerをインストールする

$ gem install bundler

railsをインストールする

$ gem install rails

Railsのバージョンを確認する

$ rails --version
Rails 5.2.4.3

以上でrailsコマンドが使えるようになる。 $rails s を実行して、アプリにアクセスできたので、バージョンアップは完了とする

まとめ

バージョンアップはRuby->Gem->Railsの3つの順番で作業は流れていきます。この3つの塊を意識すると、バージョンアップのイメージは沸きやすいと思いました。
ご参考になれば幸いです。

参考記事

rubyのバージョンアップ
【Ruby】Your Ruby version is 2.6.3, but your Gemfile specified 2.5.8
Rubyのバージョン変更

以上

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1