TIL to run specific versions of a gem executable

Today, I needed a specific version of bundler to build a gem, which sent me looking for how to: 1) Install a specific version of Bundler. 2) Activate this specific version to run commands.

If you find yourself in a similar situation, here’s what you can do:

gem install a specific version of Bundler

If you need to install a version of Bundler that is earlier than version 2, you can use Gemfile-like syntax to perform a fuzzy match on the version. Use the following command:

$ gem install bundler -v '< 2.0'
Successfully installed bundler-1.17.3
Parsing documentation for bundler-1.17.3
Done installing documentation for bundler after 0 seconds
1 gem installed

Running the executable for a specific gem version

Now, you can specify the desired version number after the executable, enclosed in underscores, to use that particular version. For example:

$ bundle _1.17.3_ install

This approach also works for other gems! If you want to see what versions of Rails you have installed, you can use the gem list command as follows:

$ gem list --exact rails

*** LOCAL GEMS ***

rails (,,,,, 4.0.13)

Then, you can use an older version of Rails for the rails new command, like this:

$ rails _4.0.13_ new
Tagged Ruby TIL