How to Install a Specific Verson for a Gem in Ruby

Stewart Nguyen Feb 02, 2024
  1. Use the gem install to Install a Gem in Ruby
  2. Use the Patterns to Install a Specific Gem Version in Ruby
How to Install a Specific Verson for a Gem in Ruby

Gems are libraries written in the Ruby programming language. We can use gems to extract the project’s common functionality and reuse it elsewhere.

Use the gem install to Install a Gem in Ruby

Syntax:

gem install <gem_name>

To install a specific gem version, we must first add the -v flag, which specifies the gem’s version.

Syntax:

gem install <gem_name> -v <version>

Example:

gem install rails -v 6.0.0

This command will install the exact version 6.0.0 of rails.

Use the Patterns to Install a Specific Gem Version in Ruby

Alternatively, we can use >, <, >=, <= to tell the gem installer not to use versions that aren’t compatible with the comparison.

gem install rails -v ">= 6.0.0"

This command will never install anything lower than version 6.0.0.

It’s useful when a gem version causes your system to crash, and you don’t want it to be installed accidentally.

~> is another syntax to install a specific gem version.

If the gem uses Semantic Versioning, this pattern is useful. It instructs the installer not to install the version with break changes.

gem install rails -v "~> 6.0.0"

This command is equivalent to gem install rails -v ">= 6.0.0, < 7.0.0"