Aditya Mukherji
Aditya Mukherji

Reputation: 9256

Using a gem without installing it

I need to run a bunch of ruby scripts that I have written on a server that I don't have sudo access to.
On my own machine, I have installed a bunch of gems using 'sudo gem install ..' and used them in my code..
Is there any mechanism which would let me use these gems without formally installing them on a remote machine?

Upvotes: 9

Views: 5096

Answers (1)

Sarah Mei
Sarah Mei

Reputation: 18484

You can, but it's tricky.

First, install them using the --install-dir option, i.e.:

gem install gem_name --install-dir /some/directory/you/can/write/to

Second, make sure you have a .gemrc file in your home directory that looks something like this:

gemhome: /some/directory/you/can/write/to
gempath:
 - /some/directory/you/can/write/to
 - /usr/local/lib/ruby/gems/1.8

gemhome is where gems should look first when seeking a gem. gempath is all the paths it should check in when seeking a gem. So in the .gemrc above, I'm telling my code to look first in the local directory, and if not found, check the system gem directory.

Third, be aware that some code - even code within gems - can make assumptions about where gems are located. Some code may programmatically alter gempath or gemhome. You may need to "alter it back" in your own code.

There's not a lot (read: no) documentation on how to do that - the best way to figure it out is to read the tests that are included with the RubyGems source. Here's how I hack the gem paths in a rake task to point to my frozen version of capistrano:

  Gem.use_paths(Gem.dir, ["#{RAILS_ROOT}/vendor/gems"])
  Gem.refresh # picks up path changes

Upvotes: 7

Related Questions