Rails 6 requires Ruby 2.5 or newer

Vishal Telangre

By Vishal Telangre

on April 9, 2019

This blog is part of our  Rails 6 series.

As per rails/rails#34754, a Rails 6 app requires Ruby version 2.5 or newer.

Let's discuss what we need to know if we are dealing with Rails 6.

Ensuring a valid Ruby version is set while creating a new Rails 6 app

While creating a new Rails 6 app, we need to ensure that the current Ruby version in the shell is set to 2.5 or newer.

If it is set to an older version then the same version will be used by the rails new command to set the Ruby version in .ruby-version and in Gemfile respectively in the created Rails app.

1\$ ruby -v
2ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
3
4\$ rails new meme-wizard
5create
6create README.md
7create Rakefile
8create .ruby-version
9create config.ru
10create .gitignore
11create Gemfile
12[...] omitted the rest of the output
13
14\$ cd meme-wizard && grep -C 2 -Rn -a "2.3.1" .
15./.ruby-version:1:2.3.1
16--
17--
18./Gemfile-2-git_source(:github) { |repo| "https://github.com/#{repo}.git" }
19./Gemfile-3-
20./Gemfile:4:ruby '2.3.1'
21./Gemfile-5-
22./Gemfile-6-# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'

An easy fix for this is to install a Ruby version 2.5 or newer and use that version prior to running the rails new command.

1\$ ruby -v
2ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
3
4\$ rbenv local 2.6.0
5
6\$ ruby -v
7ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]
8
9\$ rails new meme-wizard
10
11\$ cd meme-wizard && grep -C 2 -Rn -a "2.6.0" .
12./.ruby-version:1:2.6.0
13--
14--
15./Gemfile-2-git_source(:github) { |repo| "https://github.com/#{repo}.git" }
16./Gemfile-3-
17./Gemfile:4:ruby '2.6.0'
18./Gemfile-5-
19./Gemfile-6-# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'

Upgrading an older Rails app to Rails 6

While upgrading an older Rails app to Rails 6, we need to update the Ruby version to 2.5 or newer in .ruby-version and Gemfile files respectively.

What else do we need to know?

Since Ruby 2.5 has added Hash#slice method, the extension method with the same name defined by activesupport/lib/active_support/core_ext/hash/slice.rb has been removed from Rails 6.

Similarly, Rails 6 has also removed the extension methods Hash#transform_values and Hash#transform_values! from Active Support in favor of the native methods with the same names which exist in Ruby. These methods were introduced in Ruby 2.4 natively.

If we try to explicitly require active_support/core_ext/hash/transform_values then it would print a deprecation warning.

1
2> > require "active_support/core_ext/hash/transform_values"
3
4# DEPRECATION WARNING: Ruby 2.5+ (required by Rails 6) provides Hash#transform_values natively, so requiring active_support/core_ext/hash/transform_values is no longer necessary. Requiring it will raise LoadError in Rails 6.1. (called from irb_binding at (irb):1)
5
6=> true

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.