Is it possible to run a Rails 4.2 app on Ruby 2.4?

Andrew Grimm picture Andrew Grimm · Dec 23, 2016 · Viewed 9k times · Source

I want to try out a Rails 4.2 app on Ruby 2.4.

However, when I try doing it, I get errors about the json gem version 1.8.3 failing to install.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/agrimm/.rbenv/versions/2.4.0-rc1/lib/ruby/gems/2.4.0/gems/json-1.8.3/ext/json/ext/generator
/Users/agrimm/.rbenv/versions/2.4.0-rc1/bin/ruby -r ./siteconf20161223-91367-cql0ne.rb extconf.rb
creating Makefile

current directory: /Users/agrimm/.rbenv/versions/2.4.0-rc1/lib/ruby/gems/2.4.0/gems/json-1.8.3/ext/json/ext/generator
make "DESTDIR=" clean

current directory: /Users/agrimm/.rbenv/versions/2.4.0-rc1/lib/ruby/gems/2.4.0/gems/json-1.8.3/ext/json/ext/generator
make "DESTDIR="
compiling generator.c
generator.c:861:25: error: use of undeclared identifier 'rb_cFixnum'
    } else if (klass == rb_cFixnum) {
                        ^
generator.c:863:25: error: use of undeclared identifier 'rb_cBignum'
    } else if (klass == rb_cBignum) {
                        ^
2 errors generated.
make: *** [generator.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/agrimm/.rbenv/versions/2.4.0-rc1/lib/ruby/gems/2.4.0/gems/json-1.8.3 for inspection.
Results logged to /Users/agrimm/.rbenv/versions/2.4.0-rc1/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-14/2.4.0-static/json-1.8.3/gem_make.out

An error occurred while installing json (1.8.3), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.3'` succeeds before bundling.

which I assume is due to the unification of Fixnum and Bignum into Integer.

If I add to the Gemfile a constraint that json has to be version 2.0.0, then it complains that Rails 4.2 requires json ~> 1.7, which forbids 2.0.0.

Am I out of luck unless the maintainers of Rails decide to make a change to a non-5.x version of Rails, or the maintainers of the json gem decide to make a new non-2.x version of their gem?

Answer

James Healy picture James Healy · Dec 26, 2016

The json gem has a fix on the 1.8 branch. Hopefully it'll be released as a gem soon, but in the meantime you can use it directly in your Gemfile:

gem 'json', github: 'flori/json', branch: 'v1.8'

Rails has also merged a fix to the 4-2-stable branch about a week after the latest official 4.2.x gem release. Hopefully they'll release a new gem soon too, but this might help in your Gemfile:

gem 'rails', github: 'rails/rails', branch: '4-2-stable'

Finally, you may need to load arel from a git source as well:

gem 'arel', github: 'rails/arel', branch: '6-0-stable'

With those three changes I was able to boot our app and query the database, but then ran into other Integer unification related issues. Seems like this change to going to cause some waves.