themirror
themirror

Reputation: 10287

Rails: How do I include gem dependencies?

This is a noob question:

How do I add gems to my rails app in a way that I can just copy my app's directory structure to a remote location and have it just work, with all the gems I depend on installed and available?

Upvotes: 1

Views: 1698

Answers (4)

Don Law
Don Law

Reputation: 1329

If you want to add gems in a non-standard location for rails 2.3, you can add lines like these to config/environment.rb:

$:.push("/home/_whatever_/ruby/gems")
ENV['GEM_PATH'] = '/home/_whatever_/ruby/gems:/usr/lib/ruby/gems/1.8'

This is useful for example if you have added gems to your rails installation on hostgator.com

To help your rake tasks work correctly, add these lines to .bashrc:

export GEM_HOME=/home/_whatever_/ruby/gems
export GEM_PATH=$GEM_HOME:/usr/lib/ruby/gems/1.8
export PATH=$GEM_HOME/bin:$PATH

Upvotes: 0

Jellicle
Jellicle

Reputation: 30206

As x1a4 said, Bundler is the way to go, but an alternative (easier in the short term) is to unpack (freeze) your gems.

In your config/enviroment.rb file, inside the Rails::Initializer.run do |config| block, define which gems you depend upon like so:

config.gem 'will_paginate', :version => '~> 2.3.11', :source => 'http://gemcutter.org'
config.gem 'nokogiri'

(The :version and :source attributes are optional.)

In the command line, go to your app root directory and enter:

rake gems:install
rake gems:unpack

Your gems should show up in a folder named vendor\gems in your app. (I believe) your app will automatically look there first for any gems it requires.

Unfortunately, if the gem you want requires a native extension, you can't unpack it into your app.

Upvotes: 0

Salil
Salil

Reputation: 47472

gems path in a directory is

RAILS_ROOT/vendor/gems

you have to freeze/unpack all the gems used in this directory

Rails gems and their dependancies path will be

RAILS_ROOT/vendor/rails

And all plugins path should be

RAILS_ROOT/vendor/plugins

By default Rails load the gems form the machine (or you can say local). to load gems from the gems directory you have to add following code in tour config/enviorment.rb

  config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do |dir| 
    File.directory?(lib = "#{dir}/lib") ? lib : dir
  end

   

Upvotes: 0

x1a4
x1a4

Reputation: 19475

The future-proof solution is to use Bundler, which is required in Rails 3 and can be used right now in Rails 2.

Upvotes: 1

Related Questions