ruby
Bundler 2.4.20 understands .ruby-version files
You can now DRY up your Ruby version declarations with Bundler’s new Gemfile syntax.
Version duplication
A minor annoyance of developing and deploying Ruby applications is having to declare and maintain a project’s Ruby version in two separate places.
3.2.2
Development tools like rbenv expect a
.ruby-version file.
source "https://rubygems.org"
ruby "3.2.2"
gem "rails"
Deployment platforms like Heroku expect a
ruby declaration in the Gemfile.
A better way
Starting with the release of Bundler 2.4.19 (and subsequently refined with a bug fix and asdf support in 2.4.20), your Gemfile can use the new ruby file: syntax.
source "https://rubygems.org"
ruby file: ".ruby-version"
gem "rails"
Bundler can now load the Ruby version from a
.ruby-version file. No more duplication! Asdf’s .tool-versions file is supported as well.
How to upgrade
Taking advantage of this new Bundler feature requires a few easy steps.
1. Install the latest version of Bundler
gem install bundler
2. Update your project to use the new version
bundle commands will use the version of Bundler that is encoded into your Gemfile.lock. In order to use the new Bundler version, you’ll need to run this command to update the lock file.
bundle update --bundler
3. Edit your Gemfile
You can now edit your Gemfile to take advantage of the new syntax!
ruby file: ".ruby-version" # or ".tool-versions"
References
- Pull request that introduced the
ruby file:syntax: rubygems#6876 - Bundler 2.4.19 release notes
- After 2.4.19 was released, a small bug in the
ruby file:implementation was subsequently identified and fixed: rubygems#6892 - Support for asdf’s
.tool-versionswas later added as well: rubygems#6898 - Bundler 2.4.20 release notes
Note: When this post was originally published in October 2023, it included a note about Heroku not yet supporting this feature. As of August 2024, Heroku has upgraded its version of Bundler and this is no longer an issue.