Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a gem without installing it

Tags:

ruby

rubygems

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?

like image 875
Aditya Mukherji Avatar asked May 26 '09 22:05

Aditya Mukherji


People also ask

What is the difference between gem and plugin?

The basic difference is a gem is something that needs to be installed on the system running your Rails application, whereas a plugin is deployed along with your application. More specifically, plugins live in vendor/plugins whereas gems need to be install using rake gem install gem_name.

How do I know if a Ruby gem is installed?

Using gem search -r , you can search RubyGems' repository. For instance, gem search -r rails will return a list of Rails-related gems. With the --local ( -l ) option, you would perform a local search through your installed gems.

How does gem file work?

A Gemfile describes the gem dependencies required to execute associated Ruby code. Place the Gemfile in the root of the directory containing the associated code. For instance, in a Rails application, place the Gemfile in the same directory as the Rakefile .


1 Answers

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
like image 121
Sarah Mei Avatar answered Oct 28 '22 18:10

Sarah Mei