How to install and uninstall gem in Ruby Programming| Ruby on Rails by Example

This post explains about to install and uninstall gem with versions in the Ruby application.

It also includes a specific range of versions

Ruby gem install with a specific version

gem install command installs the library. You can use either a specific version or version comparator

  • Specific version using -v or --version You can include -v or --version to install a specific version
gem install library -v versionnumber
or
gem install library --version versionnumber

For example, you want to install the package with version 1.2.0

gem install package -v 1.2.0

With ruby version 1.19.0 onwards, You can use the colon

gem install package1:1.0.1
  • Version Comparator.

Version comparator does not give a specific version, Instead, It gives version comparison such as greater or less than. The symbols being used are >= or ~>.

For example,

gem install package -v '~> 1.1.0'

It installs versions greater than 1.1.0.

gem install package -v '~> 1.1.0, < 1.2.0'

The package version selected is greater than 1.1.0 and less than 1.2.0

You can also specify multiple gem packages with different versions

gem install package1:1.0.1 package2:'> 2'

Ruby gem uninstall with version

`gem uninstall command removes library. you can also include the version number.

Here is a command

gem uninstall library -v versionnumber
or
gem uninstall library --version versionnumber