DEV Community

thomasvanholder
thomasvanholder

Posted on • Updated on

How to upgrade your Rails app to Ruby 3.1 with rbenv

Ruby 3.1 release documentation

  1. Upgrade rbenv
  2. Install Ruby 3.1.0
  3. Set local Ruby version
  4. Update Gemfile
  5. Debug gem
  6. New features

1. Upgrade rbenv

To include Ruby 3.1 as one of the available install options, first upgrade ruby-build:

brew update && brew upgrade ruby-build
Enter fullscreen mode Exit fullscreen mode

See all available versions with rbenv install --list.

Ruby 3.1.0 should return in the result.


2. Install Ruby 3.1.0

rbenv install 3.1.0
Enter fullscreen mode Exit fullscreen mode

3. Set local Ruby version

rbenv local 3.1.0
Enter fullscreen mode Exit fullscreen mode

You can verify that the .ruby-version file in the root of your folder now lists version 3.1.0.


4. Update Gemfile

# old
ruby "3.0.3"

# new
ruby "3.1.0"
Enter fullscreen mode Exit fullscreen mode

Tip: You can link to the ruby .ruby_version file instead to read the ruby version. A single source defines the ruby version and you avoid a discrepancy between the gemfile and .ruby_version file to occur.

ruby File.read('.ruby-version').strip
Enter fullscreen mode Exit fullscreen mode
bundle install
Enter fullscreen mode Exit fullscreen mode

You might run into the error cannot load such file -- net/smtp (LoadError). net-smtp has been removed from the default gems in ruby 3.1. As Action Mailbox depends on net/smtp temporarily add to your gemfile until the mail gem includes it as a dependancy.

gem "net-smtp", require: false
Enter fullscreen mode Exit fullscreen mode

5. Debug gem

The debug gem is the new preferred debugger. Alternative gems like pry are no longer necessary. Rails 7 includes the debug gem by default.

Gemfile:

group :development, :test do
  # old
  gem 'pry'
  # new
  gem "debug", ">= 1.0.0"
end
Enter fullscreen mode Exit fullscreen mode

Learn more in this video


6. New Features

Top comments (0)