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:
That installs exactly version 1.16.8 of the nokogiri gem into the current Ruby environment.
You can confirm the installed versions with:
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:
Then install with:
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:
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:
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:
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 installfor an application instead of pinning dependencies in aGemfile. - 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_NAMEto verify what is installed. - For applications, prefer pinning the version in a
Gemfileand runningbundle install. - Check your active Ruby environment before installing anything.
- Use
bundle execso the project runs with the versions you actually declared.

