How to upgrade your Rails app to Ruby 3.1 with rbenv

Ruby 3.1 release documentation

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

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

3. Set local Ruby version

rbenv local 3.1.0

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"
bundle install

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

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

Learn more in this video

6. New Features

40