1033

Using the command-line gem tool, how can I install a specific version of a gem?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
mjs
  • 63,493
  • 27
  • 91
  • 122

8 Answers8

1340

Use the -v flag:

$ gem install fog -v 1.8
mjs
  • 63,493
  • 27
  • 91
  • 122
  • 72
    If, like me, you have previously installed a later version of your gem, then you must uninstall the later version with e.g. `gem uninstall fog`. It will ask you which version to uninstall if you have more than one. – Dizzley Jan 26 '14 at 18:18
  • 4
    i have 3 versions of rake: `gem list | grep rake` = `rake (10.1.1, 10.1.0, 0.8.7)`.. i got a rails 2.3.5 project and another that's 3.0 that i'm working on at the same time.. how do i use a specific version of rake for each project (ie on command line?) – abbood Mar 13 '14 at 08:40
  • @abbood `rake _10.1.1_ ...` should work, for whoever wants to know :) – Koen. Jan 29 '17 at 10:00
  • 1
    Worth noting that this does not seem to affect executable binaries. Uninstalling the previous version worked for binaries though. – Ciro Santilli OurBigBook.com Feb 21 '19 at 08:16
  • Once again, wishing I could do multiple upvotes. Maybe if they had to be separated by at least a year. :) – Don Branson May 21 '20 at 14:48
  • 2
    I probably search and find this once a year lol, that's how often I use it but great! – lacostenycoder Feb 23 '21 at 20:14
250

Use the --version parameter (shortcut -v):

$ gem install rails -v 0.14.1
…
Successfully installed rails-0.14.1

You can also use version comparators like >= or ~>

$ gem install rails -v '~> 0.14.0'
…
Successfully installed rails-0.14.4

With newer versions of rubygems you can tighten up your requirements:

$ gem install rails -v '~> 0.14.0, < 0.14.4'
…
Successfully installed rails-0.14.3

Since some time now you can also specify versions of multiple gems:

$ gem install rails:0.14.4 rubyzip:'< 1'
…
Successfully installed rails-0.14.4
Successfully installed rubyzip-0.9.9

But this doesn't work for more than one comparator per gem (yet).

schmijos
  • 8,114
  • 3
  • 50
  • 58
  • This worked for ~>, but what if you have '< 0.9, >= 0.7' type of situation? I tried applying two v arguments and it appears as though the first was ignored. Using the a comma separator wouldn't even parse. I ended up removing the greater than requirement and it happened to install an acceptable version, but having a formal solution would be nice. – Joseph Coco Jul 14 '15 at 00:25
  • What does version comparator `~>` stand for? – Dragas Aug 16 '17 at 05:48
  • It means that the last digit is allowed to grow (https://stackoverflow.com/questions/5170547/what-does-tilde-greater-than-mean-in-ruby-gem-dependencies) – schmijos Aug 16 '17 at 08:35
  • 1
    Mael Stor's answer tells us that Joseph Coco's suggested two v arguments from six years earlier now works. – Martin Dorey Apr 24 '21 at 17:31
106

for Ruby 1.9+ use colon.

gem install sinatra:1.4.4 prawn:0.13.0
user7610
  • 25,267
  • 15
  • 124
  • 150
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
106

For installing gem install gemname -v versionnumber

For uninstall gem uninstall gemname -v versionnumber

Emjey
  • 2,038
  • 3
  • 18
  • 33
27

As others have noted, in general use the -v flag for the gem install command.

If you're developing a gem locally, after cutting a gem from your gemspec:

$ gem install gemname-version.gem

Assuming version 0.8, it would look like this:

$ gem install gemname-0.8.gem
myconode
  • 2,518
  • 1
  • 26
  • 28
  • 3
    Great answer. I suggest you add another code block for the flag "-v" option though. As my eyes read the answer they immediately went to the code blocks and not the text surrounding it. If others do the same they'll miss the "-v" portion. – CamHart Jun 22 '16 at 18:57
14

You can use the -v or --version flag. For example

gem install bitclock -v '< 0.0.2'

To specify upper AND lower version boundaries you can specify the --version flag twice

gem install bitclock -v '>= 0.0.1' -v '< 0.0.2'

or use the syntax (for example)

gem install bitclock -v '>= 0.0.1, < 0.0.2'

The other way to do it is

gem install bitclock:'>= 0.0.1'

but with the last option it is not possible to specify upper and lower bounderies simultaneously.

[gem 3.0.3 and ruby 2.6.6]

Mael Stor
  • 141
  • 1
  • 3
  • How to make gem to **not upgrade dependencies**, `gem install "asciidoctor:2.0.10" "asciidoctor-diagram:2.0.5"`, always upgrade asciidoctor to a later version than 2.0.10, I've tried to use the options `--minimal-deps` and `--conservative` ? I've worked around this with `gem install --no-document --ignore-dependencies "asciidoctor-diagram:2.0.5"` but `asciidoctor-diagram` only has a single dep, how to handle that more elegantly. – bric3 Nov 06 '20 at 13:48
  • @Brice I couldn't reproduce this behaviour with the command line `gem install 'asciidoctor:2.0.10' 'asciidoctor-diagram:2.0.5'`. It installs exactly these versions. Which gem version is reported when you type `gem --version`? – Mael Stor Nov 09 '20 at 11:29
  • It happens with `gem` 3.1.4, in the `registry.fedoraproject.org/fedora-minimal` image. Using this command for example `docker run -it --rm registry.fedoraproject.org/fedora-minimal bash -c "microdnf install -y ruby; gem install 'asciidoctor:2.0.10' 'asciidoctor-diagram:2.0.5'"`, I see that first asciidoctor 2.0.10 is installed, then asciidoctor 2.0.12. – bric3 Nov 10 '20 at 14:40
7

Linux

To install different version of ruby, check the latest version of package using apt as below:

$ apt-cache madison ruby
      ruby |    1:1.9.3 | http://ftp.uk.debian.org/debian/ wheezy/main amd64 Packages
      ruby |        4.5 | http://ftp.uk.debian.org/debian/ squeeze/main amd64 Packages

Then install it:

$ sudo apt-get install ruby=1:1.9.3

To check what's the current version, run:

$ gem --version # Check for the current user.
$ sudo gem --version # Check globally.

If the version is still old, you may try to switch the version to new by using ruby version manager (rvm) by:

rvm 1.9.3

Note: You may prefix it by sudo if rvm was installed globally. Or run /usr/local/rvm/scripts/rvm if your command rvm is not in your global PATH. If rvm installation process failed, see the troubleshooting section.


Troubleshooting:

  • If you still have the old version, you may try to install rvm (ruby version manager) via:

    sudo apt-get install curl # Install curl first
    curl -sSL https://get.rvm.io | bash -s stable --ruby # Install only for the user.
    #or:# curl -sSL https://get.rvm.io | sudo bash -s stable --ruby # Install globally. 
    

    then if installed locally (only for current user), load rvm via:

    source /usr/local/rvm/scripts/rvm; rvm 1.9.3
    

    if globally (for all users), then:

    sudo bash -c "source /usr/local/rvm/scripts/rvm; rvm 1.9.3"
    
  • if you still having problem with the new ruby version, try to install it by rvm via:

    source /usr/local/rvm/scripts/rvm && rvm install ruby-1.9.3 # Locally.
    sudo bash -c "source /usr/local/rvm/scripts/rvm && rvm install ruby-1.9.3" # Globally.
    
  • if you'd like to install some gems globally and you have rvm already installed, you may try:

    rvmsudo gem install [gemname]
    

    instead of:

      gem install [gemname] # or:
      sudo gem install [gemname]
    

Note: It's prefered to NOT use sudo to work with RVM gems. When you do sudo you are running commands as root, another user in another shell and hence all of the setup that RVM has done for you is ignored while the command runs under sudo (such things as GEM_HOME, etc...). So to reiterate, as soon as you 'sudo' you are running as the root system user which will clear out your environment as well as any files it creates are not able to be modified by your user and will result in strange things happening.

kenorb
  • 155,785
  • 88
  • 678
  • 743
0

Prior to installing, you can check the available versions with the list command.

gem list ^[gemname]$ --remote --all
gem install [gemname] -v [version]
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68