Ruby
Ruby Gems
Programming
Software Installation
Version Control

Ruby How to install a specific version of a ruby gem?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you need a specific version of a Ruby gem, the direct command is simple: install the gem with the -v flag. That tells RubyGems exactly which release you want instead of taking the latest one.

The command is easy, but the surrounding workflow matters. Installing a version globally is different from pinning a version for one project in a Gemfile.

Installing a Specific Version with gem install

The basic command is:

bash
gem install nokogiri -v 1.16.8

That installs exactly version 1.16.8 of the nokogiri gem into the current Ruby environment.

You can confirm the installed versions with:

bash
gem list nokogiri

If multiple versions are installed, RubyGems can keep them side by side. Which one your application uses then depends on Bundler, the gem activation rules, and your project configuration.

Project Dependencies Should Usually Use Bundler

For application code, installing gems manually is often not enough. The better practice is to pin the version in a Gemfile:

ruby
1source "https://rubygems.org"
2
3gem "nokogiri", "1.16.8"
4gem "sinatra", "~> 4.0"

Then install with:

bash
bundle install

This approach is more reproducible because the chosen versions are recorded in Gemfile.lock. Teammates and deployment systems can install the same dependency set without guessing.

Using the Right Ruby Environment

A gem is installed into the currently active Ruby environment. If you use rbenv, asdf, or rvm, make sure the intended Ruby version is active first:

bash
ruby -v
which ruby
which gem

Those commands help verify that you are installing into the expected interpreter and gem path.

This matters because "I installed the right gem version" often turns out to mean "I installed it for a different Ruby version than the one my app is running."

Running a Project with the Pinned Version

After pinning versions in a Gemfile, run commands through Bundler:

bash
bundle exec ruby app.rb
bundle exec rake test

bundle exec ensures Ruby loads the versions declared for that project instead of whatever happens to be globally available.

That is especially important when you have multiple versions of the same gem installed.

Uninstalling or Switching Versions

If you want to remove a version later, use:

bash
gem uninstall nokogiri -v 1.16.8

If multiple versions remain installed, Bundler will still resolve according to your Gemfile. Without Bundler, RubyGems may activate a version based on dependency resolution rules that are less explicit than most application teams want.

If you are troubleshooting native-extension gems, also check whether the selected gem version supports your current Ruby version and platform. Sometimes the command is correct, but the install fails because the chosen release is incompatible with the active interpreter.

Common Pitfalls

  • Installing the right gem version into the wrong Ruby environment.
  • Relying on a global gem install for an application instead of pinning dependencies in a Gemfile.
  • Forgetting to use bundle exec, which can load a different installed version than expected.
  • Assuming only one version of a gem can exist at a time. RubyGems can keep several versions side by side.

Summary

  • Install a specific gem version with gem install GEM_NAME -v VERSION.
  • Use gem list GEM_NAME to verify what is installed.
  • For applications, prefer pinning the version in a Gemfile and running bundle install.
  • Check your active Ruby environment before installing anything.
  • Use bundle exec so the project runs with the versions you actually declared.

Course illustration
Course illustration

All Rights Reserved.