Blog

Thoughts from my daily grind

Installing & runing a specific version of a Ruby gem

Posted by Ziyan Junaideen |Published: 14 April 2022 |Category: Ruby Language
Default Upload |

Sooner or later, you will end up with more than one version of a Ruby gem in your system. RubyGems, by default will use the latest version of a gem unless specified in a Gemfile.

This article covers installing a specific version of a Ruby gem and using it in the command line. As an example, we will use the popular Rails gem. As of today, the latest version of Rails is v7.0.2.3. Let's install Rails v6.1.0 and generate a new rails v6.1.0 project.

Install a specific version of a gem

You can install a specific version of a Ruby gem using the -v option provided to the gem install command. Since we are interested in installing v6.1.0, we will add -v 6.1.0.

gem install rails -v 6.1.0

You can access more details using gem install --help.

⇒  gem install --help
Usage: gem install [options] GEMNAME [GEMNAME ...] -- --build-flags [options]

  Options:
        --platform PLATFORM          Specify the platform of gem to install
    -v, --version VERSION            Specify version of gem to install
        --[no-]prerelease            Allow prerelease versions of a gem
                                     to be installed. (Only for listed gems)


  Deprecated Options:
    -u, --[no-]update-sources        Update local source cache

...

Check for installed versions of a gem

Before installing a gem, you may want to check if a gem is already installed. You can do that using gem list. This command will list all available gems. You can filter by providing a regex.

⇒  gem list rails

*** LOCAL GEMS ***

cells-rails (0.1.4)
...
minitest-spec-rails (6.1.0)
rails (7.0.2.3, 7.0.2, 7.0.0, 6.1.4.1, 6.1.3.1, 6.0.4.7, 6.0.0, 5.2.0, 5.1.7, 5.1.0)
rails-dom-testing (2.0.3)
...

As you can see, I have ten versions of the Rails installed in my system.

Use a specific version of a Ruby gem

Now we need to run the generator. We would usually run the rails command as rails new my_project. That will use the latest version of the gem. Since we need to use an old version of the gem, we will specify the required version.

rails _6.1.0_ new my_app

You specify the version using the format _[version]_ after the command's name and before its command-line arguments. RubyGems checks the first argument in the ARGV array, and if it is a version, it will change the load path accordingly and use the correct version of the gem.

Conclusion

Software developers require to work with different versions of the same library. Sometimes we need to install an older version of a library. I generate new rails projects to test ruby gems I author and generate examples for articles and blog posts.

You can install a specific gem version with gem install [name] -v [version] and use it as [name] _[version]_ [command] [line] [args].

About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments