Faraaz Khan
Faraaz Khan

Reputation: 91

How to install gem across all gemsets when using RVM

Is there a way to install a gem across all rubies and gemsets (not just the default or the system ruby) in the system when using RVM?

Upvotes: 9

Views: 6251

Answers (4)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

TL;DR

The previous answers are outdated. I'm not sure when this functionality was introduced, but it's certainly available under RVM 1.29.11 (I tested this under 1.29.12-next, which is the current RVM head), and probably much earlier. You can check out the RVM source code if you need to know exactly when the feature was introduced.

You can install one or more gems to all your @global gemsets for each Ruby, or explicitly to all gemsets for a given Ruby, or even for all Rubies. This is (perhaps a little opaquely) documented in the Set Actions -> Do -> Usage & Selectors sections of the RVM documentation.

Invocation

As a practical example, to add the new(ish) rbs and typeprof gems to the global gemset for all your compatible Rubies, simply type:

rvm all do rvm @global do gem install rbs typeprof

This is usally the right thing to do, as it installs the gems you specify into the global gemset for each Ruby where it is available to all of that Ruby's gemsets, rather than installing multiple copies into each individual gemset for each Ruby. If you really want to do it in all gemsets rather than in @global though (including @default), then you certainly can. For example:

rvm all-gemsets do gem install rbs typeprof

I don't personally recommend the latter approach as it makes removal a lot harder if you add a gem with a lot of dependencies, but the choice is yours.

Caveats for Incompatible Rubies

JRuby is currently an example of an incompatible Ruby for these particular gems, and may throw warnings or errors. Sometimes it will provide an example of how to install the gem for that particular Ruby version. If so, you can then try that. For example, JRuby will complain:

ERROR:  Error installing rbs:
        The last version of rbs (>= 0) to support your Ruby & RubyGems was 1.0.0.
        Try installing it with `gem install rbs -v 1.0.0`
        rbs requires Ruby version >= 2.6. The current ruby version is 2.5.0.

ERROR:  Error installing typeprof:
        There are no versions of rbs (>= 1.3.1) compatible with your Ruby & RubyGems. Maybe try installing an older version of the gem you're looking for?
        rbs requires Ruby version >= 2.6. The current ruby version is 2.5.0.

In this case, you can install an earlier version of RBS, but you still won't be able to install typeprof. Because JRuby often runs behind MRI (at the time of this writing, JRuby currently targets Ruby 2.5.7), your mileage with particular gems will vary. This is going to be true of other Rubies as well, so caveat emptor.

Upvotes: 0

jmarceli
jmarceli

Reputation: 20162

You can execute:

rvm @global do gem install [gem_name]

to install gem globally (per ruby version).

It is not possible to install gem globally for every ruby version.

According to https://rvm.io/gemsets/initial you can define automatically installed gems for every ruby version in file ~/.rvm/gemsets/global.gems. In this file you need to define required gems (one per line) e.g.

bundler
zeus

These gems will be installed each time you add new ruby version to the RVM.

Upvotes: 9

Joc
Joc

Reputation: 1059

You can install to a default, global gemset per ruby interpreter as mentioned here:

https://rvm.io/gemsets/global/

and also this page mentions default gem sets

https://rvm.io/gemsets/using/

And it wouldn't be useful to install a gem to all ruby interpreters due to incompatibilities between rubies.

Upvotes: 6

jsinger
jsinger

Reputation: 807

Looking through the RVM docs, I don't see a way to do this specifically. However, you might be able to approximate it via a combination of @global gemsets and gemset copying copying.

Global gemsets provide gems that are available to all gemsets of a given ruby. E.g.:

rvm install 1.9.3
rvm --create [email protected]
rvm --create [email protected]
rvm use 1.9.3@global
gem install sqlite3

would create two different gemsets for the 1.9.3 ruby, and the global sqlite3 would be available in both.

Then, once you've done this, you can copy the global gemset to other rubies, and they'd have it available themselves. For example, as a continuation of the above:

rvm install 1.8.7
rvm gemset copy 1.9.3@global 1.8.7@global

and now the sqlite3 gem is available to all 1.8.7 gemsets (assuming I've got the arguments in the right order - I may not, in which case switch 1.9.3 and 1.8.7). It's not quite ideal, since updates to one ruby don't automatically become visible in the others, you'd have to copy to each ruby individually, and I imagine you'd possibly end up clobbering any ruby-dependant global gems you've set up in each. If that last point isn't an issue you foresee being a problem, though, you could probably write up a script to avoid the tediousness implied by the first two points.

Disclaimer: This is put together strictly from reading through the RVM docs, and I haven't actually tried this out. If I got something wrong, I'll be glad to edit the answer to fix it up.

Upvotes: 0

Related Questions