Question
I want to use Ruby's command-line gem tool to install a particular version of a gem instead of the latest one. How can I specify the exact gem version during installation?
Short Answer
By the end of this page, you will understand how to install a specific version of a Ruby gem from the command line, why version pinning matters, what command syntax to use, and how this fits into real Ruby projects and dependency management.
Concept
In Ruby, gems are reusable packages that add functionality to your application or development environment. When you run gem install some_gem, RubyGems usually installs the latest available version.
Sometimes, however, you need a specific version instead of the newest one. This is common when:
- your project depends on an older version
- a newer release introduced breaking changes
- you need to match a production environment
- you are debugging version-specific behavior
RubyGems lets you choose a version explicitly with the --version option.
gem install GEM_NAME --version VERSION
Example:
gem install rails --version 7.0.8
This tells RubyGems to install exactly that version if it is available.
This matters because software versions affect compatibility. Even small changes between gem versions can change APIs, behavior, performance, or dependency requirements. Installing a specific version makes your environment more predictable and reproducible.
Mental Model
Think of a gem like a textbook that gets new editions over time.
gem install railsmeans: “Give me the newest edition.”gem install rails --version 7.0.8means: “Give me exactly edition 7.0.8.”
If your class assignments were written for edition 7.0.8, using edition 8.0.0 might cause confusion because page numbers and content may have changed. Gem versions work the same way: the code in your project may expect a particular version.
Syntax and Examples
The core syntax is:
gem install GEM_NAME --version VERSION
You can also use the short form:
gem install GEM_NAME -v VERSION
Example 1: Install an exact version
gem install bundler --version 2.4.22
This installs version 2.4.22 of bundler.
Example 2: Short version flag
gem install rake -v 13.1.0
This does the same thing using the shorter -v option.
Example 3: Check installed versions
After installing, you can list installed gems:
gem list rake
Possible output:
rake (13.1.0, 13.0.6)
This means multiple versions may exist on your system.
Example 4: Install without documentation
gem install rspec -v 3.12.0 --no-document
This installs the chosen version and skips local documentation generation, which can make installation faster.
Step by Step Execution
Consider this command:
gem install nokogiri -v 1.15.5
Here is what happens step by step:
- The
gemcommand starts the RubyGems command-line tool. installtells RubyGems you want to install a package.nokogiriis the gem name.-v 1.15.5tells RubyGems to look for that specific version.- RubyGems checks available gem versions from the configured source, usually RubyGems.org.
- If version
1.15.5exists and is compatible with your Ruby environment, RubyGems downloads it. - RubyGems also installs any required dependencies.
- The gem becomes available in your Ruby environment.
You can then verify it with:
gem list nokogiri
If you see output like this:
nokogiri (1.15.5)
then the installation worked.
If the version does not exist, RubyGems will report an error instead of silently installing a different version.
Real World Use Cases
Installing a specific gem version is common in real development work.
Maintaining older applications
A legacy Rails app may only work with an older version of rails, devise, or sidekiq.
gem install rails -v 6.1.7
Matching production environments
If production uses a specific Bundler version, developers often install that same version locally.
gem install bundler -v 2.3.26
Reproducing bugs
If a bug happens only in one gem version, you may install that version to test it.
gem install pg -v 1.4.6
CI and deployment scripts
Build scripts sometimes install exact tool versions for predictable results.
Safe upgrades
Teams often test a new gem version gradually rather than always jumping to the latest release.
Real Codebase Usage
In real projects, developers usually do not manually install every application dependency one by one. Instead, they use Bundler with a Gemfile to declare versions.
Example Gemfile:
gem 'rails', '7.0.8'
gem 'pg', '~> 1.5'
gem 'puma'
Then they run:
bundle install
Still, gem install -v is commonly used for:
- installing a specific version of
bundler - installing CLI gems globally
- testing a gem version outside a project
- setting up tooling on CI servers
Common patterns in codebases
- Version pinning: lock exact versions for stability
- Compatible ranges: allow safe updates with constraints like
~> - Environment matching: install the same version across local, CI, and production
- Debugging: compare behavior across gem versions
Guarding against environment problems
Teams often check tool versions before running scripts:
Common Mistakes
1. Forgetting the version flag
Broken command:
gem install rails 7.0.8
Why it is wrong:
- RubyGems does not treat the extra value as the version in this form.
Correct command:
gem install rails -v 7.0.8
2. Assuming the latest version will always work
Beginners often install the newest version and expect compatibility with older code.
gem install some_gem
Avoid this when a project requires a known version.
3. Confusing gem install with Bundler
gem install installs gems into your Ruby environment.
bundle install installs dependencies from a Gemfile for a project.
If you are working inside an application, check whether the project expects Bundler instead.
4. Using a version that does not exist
gem install rake -v 99.99.99
This fails because that version is not published.
You can inspect available versions with:
Comparisons
| Task | Command | Best Use Case |
|---|---|---|
| Install latest gem version | gem install rails | Quick install when version does not matter |
| Install exact gem version | gem install rails -v 7.0.8 | Reproducible setup and compatibility |
| Install project dependencies | bundle install | Application development with a Gemfile |
| See installed versions | gem list rails | Verify what is already installed |
| See remote versions | gem search '^rails$' --remote --all | Find available versions before installing |
gem install vs
Cheat Sheet
# Install an exact version
gem install GEM_NAME -v X.Y.Z
# Same using long flag
gem install GEM_NAME --version X.Y.Z
# Example
gem install rails -v 7.0.8
# Skip documentation
gem install rails -v 7.0.8 --no-document
# List installed versions
gem list rails
# Search available remote versions
gem search '^rails$' --remote --all
Quick rules
- Use
-vor--versionto specify a version. - Use an exact version for predictable installs.
- Multiple gem versions can be installed on one machine.
- In application projects, prefer Bundler and a
Gemfilefor dependency management. - If installation fails, check whether the version exists and whether your Ruby version is compatible.
Most important command
gem install GEM_NAME -v VERSION
FAQ
How do I install a particular version of a Ruby gem?
Use:
gem install GEM_NAME -v VERSION
Example:
gem install rake -v 13.1.0
Can I install multiple versions of the same gem?
Yes. RubyGems can keep multiple versions installed on the same system.
How do I see which gem versions are available?
Use:
gem search '^GEM_NAME$' --remote --all
Example:
gem search '^rails$' --remote --all
What is the difference between -v and --version?
They mean the same thing. -v is just the short form.
Should I use gem install or bundle install?
Use gem install for direct or global installs. Use bundle install when working inside a Ruby project with a Gemfile.
Mini Project
Description
Create a small command-line setup checklist for a Ruby project. The goal is to practice installing an exact gem version, verifying it, and understanding the difference between direct gem installation and project dependency management.
Goal
Install a specific version of a Ruby gem, confirm that it is installed, and inspect available versions from the command line.
Requirements
- Install a specific version of a gem such as
rake. - Verify the installed version using a RubyGems command.
- Search the remote repository for available versions of that gem.
- Install the same gem with
--no-documentor use the short-vsyntax. - Write down which command installs gems directly and which tool is usually used inside projects.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.