tags:

views:

352

answers:

2

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?

+3  A: 

You can install them in a user directory: http://docs.rubygems.org/read/chapter/3#page83

This looks like it lets you install the gems anywhere you want.

Joe Beda
+2  A: 

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
Sarah Mei
I actually do this all the time (all my gems install in ~/.gem) and have yet to encounter any misbehaving code, nor find it that difficult. But YMMV.
rampion
Interesting, I wonder if ~/.gem is a special case. I'm usually trying to reference gems installed in an application.
Sarah Mei