How do I specify local .gem files in my Gemfile?
This isn't strictly an answer to your question about installing .gem
packages, but you can specify all kinds of locations on a gem-by-gem basis by editing your Gemfile.
Specifying a :path
attribute will install the gem from that path on your local machine.
gem "foreman", path: "/Users/pje/my_foreman_fork"
Alternately, specifying a :git
attribute will install the gem from a remote git repository.
gem "foreman", git: "git://github.com/pje/foreman.git"
# ...or at a specific SHA-1 ref
gem "foreman", git: "git://github.com/pje/foreman.git", ref: "bf648a070c"
# ...or branch
gem "foreman", git: "git://github.com/pje/foreman.git", branch: "jruby"
# ...or tag
gem "foreman", git: "git://github.com/pje/foreman.git", tag: "v0.45.0"
(As @JHurrah mentioned in his comment.)
How can I specify a local gem in my Gemfile?
I believe you can do this:
gem "foo", path: "/path/to/foo"
How to tell the Gem File to use a specific local copy of a gem
Try, in your Gemfile
:
gem "mygem", :path => "/MyPath/MyGem.gem"
Note that it's probably best to use a relative link in there, like:
gem "mygem", :path => "vendor/MyPath/MyGem.gem"
How can I install a local gem?
Yup, when you do gem install
, it will search the current directory first, so if your .gem file is there, it will pick it up. I found it on the gem reference, which you may find handy as well:
gem install will install the named
gem. It will attempt a local
installation (i.e. a .gem file in the
current directory), and if that fails,
it will attempt to download and
install the most recent version of the
gem you want.
Specify path to Gemfile
You can use the --gemfile
option:
--gemfile= The location of the Gemfile(5) which Bundler should use. This defaults to a Gemfile(5) in the current working
directory. In general, Bundler will assume that the location of the
Gemfile(5) is also the project's root and will try to find
Gemfile.lock and vendor/cache relative to this location.
Source: Bundler manual
How to reference a local gem from a ruby script?
Make sure that your gem name as same as in Gemfile (e.g. custom_gem
)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
Don't forget to actually install this gem using bundler
bundle install
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something
Specify gem installation directory
You can add the following to your config.ru file:
ENV['GEM_HOME']="#{ENV['HOME']}/projects/shared/gems/ruby/1.8/gems"
ENV['GEM_PATH']="#{ENV['GEM_HOME']}:/var/lib/ruby/gems/1.8"
require 'rubygems'
Gem.clear_paths
This will tell your rack app where to look for gems.
Also configure your server .bashrc:
export GEM_HOME="$HOME/projects/shared/gems/ruby/1.8/gems"
export GEM_PATH="$GEM_HOME:/var/lib/ruby/gems/1.8"
Related Topics
Understanding the Fibonacci Sequence
How to Upgrade to the Current Version of Ruby (2.2.3) on Os X V10.6.8
Error Installing Gem Capybara-Webkit
Rails 4 Error R14 on Heroku (Memory Quota Exceeded)
Rails - Invalid Authenticity Token After Deploy
How to Get Parent Node in Capybara
Include Params/Request Information in Rails Logger
Rails 4.0.1 on Heroku, Can't Create Database
Parsing Atom & Rss in Ruby/Rails
How to Use Ruby for Shell Scripting
Upgrading from Rails 3 to Rails 3.1
Ruby: Is There an Opposite of Include? for Ruby Arrays
Ruby: How to Concatenate Array of Arrays into One
Ruby/Rails - Models Named with Two Words (Naming Convention Issues)
Rails-Like Database Migrations