Ruby build fails with "Your Ruby version is X, but your Gemfile specified Y"

Issue

When deploying your Ruby application to Heroku, the build fails with the following message:

Your Ruby version is X, but your Gemfile specified Y

E.g., The following build output implies the local environment is using Ruby 2.3.1, but the Gemfile has been changed to use Ruby 2.3.3:

Counting objects: 594, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (530/530), done.
Writing objects: 100% (594/594), 3.07 MiB | 840.00 KiB/s, done.
Total 594 (delta 251), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.3.1
remote: -----> Installing dependencies using bundler 1.13.6
remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote: Your Ruby version is 2.3.1, but your Gemfile specified 2.3.3
remote: Bundler Output: Your Ruby version is 2.3.1, but your Gemfile specified 2.3.3
remote: !
remote: ! Failed to install gems via Bundler.
remote: !
remote: ! Push rejected, failed to compile Ruby app.
remote: 
remote: ! Push failed
remote: Verifying deploy...

Resolution

This happens when there is a difference between the version of Ruby you are using locally, and the versions specified in your Gemfile and Gemfile.lock.

You will need to update your local Ruby version to same as your Gemfile, and then run bundle update --ruby to ensure your Gemfiles and Gems are the correct versions, then commit all the changes and re-deploy to Heroku.

You can get more information in the Ruby version troubleshooting section Your Ruby version is <X>, but your Gemfile specified <Y>.