175

I have a couple of gem files which I install via gem install xx.gem. Can I tell Bundler to use them? Or do I have to specify the source path?

Benjamin Oakes
  • 12,262
  • 12
  • 65
  • 83
ddayan
  • 4,052
  • 6
  • 27
  • 32

7 Answers7

303

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.)

David Hempy
  • 5,373
  • 2
  • 40
  • 68
pje
  • 21,801
  • 10
  • 54
  • 70
  • Also for some firewalls git protocol may be an issue. In such case try `gem "foreman", :git => "https://github.com/pje/foreman.git"` – kode Dec 11 '13 at 07:54
75

Seems bundler can't use .gem files out of the box. Pointing the :path to a directory containing .gem files doesn't work. Some people suggested to setup a local gem server (geminabox, stickler) for that purpose.

However, what I found to be much simpler is to use a local gem "server" from file system: Just put your .gem files in a local directory, then use "gem generate_index" to make it a Gem repository

mkdir repo
mkdir repo/gems
cp *.gem repo/gems
cd repo
gem generate_index

Finally point bundler to this location by adding the following line to your Gemfile

source "file://path/to/repo"

If you update the gems in the repository, make sure to regenerate the index.

Martin Thiede
  • 851
  • 6
  • 4
  • 7
    Thanks! Also, I needed a third slash to do "file:///Absolute/Path/to/repo" – Anna Oct 02 '12 at 19:42
  • 5
    Wouldn't it just be easier to run "gem server" on the command line and add source "http://localhost:8808/" to your Gemfile – Anna Oct 09 '12 at 21:19
  • This is really cool Martin! Thanks for sharing, is exactly was i was looking for since i didn't want to depend on running a `gem server` nor i wanted to host the entire project, just the .gem file. – Leo Gallucci Dec 16 '13 at 16:22
  • 6
    Bonus tip: You can get around the absolute path requirement by using File.expand_path like this `source "file://#{File.expand_path('.')}/chef-11.10.0.dev.2"` – Leo Gallucci Dec 16 '13 at 16:22
  • Doesn't work for me. Mac OS X. Bundler v1.11.2: `Could not fetch specs from file://Users/nakilon/....../` – Nakilon Feb 10 '16 at 00:43
  • 1
    @Nakilon I had the same issue, but then realized I hadn't put my gem into the `gems` folder of the `repo` folder, but only into the `repo` folder. Have you created a `gems` folder? – Asier Mar 04 '16 at 16:33
  • This is a great solution for me as I work behind a corporate firewall that `gem` cannot penetrate. So I download gem files manually from rubygems.org and build my own repo. – Robert Brown Apr 11 '16 at 03:33
  • Thanks a lot. This worked for me since `gem server` was returning 404s when bundler was trying to install gems (even though I could see the repo page in localhost and `gem install --source localhost...` worked). – Samuel Garratt Mar 22 '18 at 02:58
  • 1
    Super trick! here is small addition for that. How to verify that gems are now available locally: gem list -r --clear-sources -s file:/repo – Алексей Лещук Oct 09 '19 at 14:50
52

I would unpack your gem in the application vendor folder

gem unpack your.gem --target /path_to_app/vendor/gems/

Then add the path on the Gemfile to link unpacked gem.

gem 'your', '2.0.1', :path => 'vendor/gems/your'
Satishakumar Awati
  • 3,604
  • 1
  • 29
  • 50
edap
  • 1,084
  • 1
  • 10
  • 16
30

By default Bundler will check your system first and if it can't find a gem it will use the sources specified in your Gemfile.

JHurrah
  • 1,988
  • 16
  • 12
  • True. but what if I haven't installed it before?(when im deploying to a server) Could not find Imlib2-Ruby-0.5.2 in any of the sources command finished – ddayan Apr 12 '11 at 11:26
  • 187
    you can point to a local directory with gem "gemname", :path => "~/some/local/path" – JHurrah Apr 12 '11 at 11:43
  • 5
    I think that :path requires the gem folder with gemspec file. I only have .gem files i wish to install. – ddayan Apr 12 '11 at 11:45
  • Yes looks like I needed to specify the version, I wonder why it always told me what version it was looking for. for an example "Could not find ruby-vips-0.1.0 in any of the sources" now I specified gem 'ruby-vips' '=0.1.0', :path =>.... and it works. Thanks. – ddayan Apr 13 '11 at 00:04
  • 1
    Ok so cap deploy passed, but the gem wasn't installed "No such file to load -- vips" :/ – ddayan Apr 13 '11 at 00:19
  • 4
    That's definitely not true, at least any longer. Bundler does *not* look at your system gems, and only goes by what's in the Gemfile. One of reasons it's so terrible. – bioneuralnet Feb 03 '12 at 04:57
  • @ddayan, any solution to your "No such file to load" issue? I am running into the same one myself. `bundle install` works fine and dandy, but once you call `require 'yourgame'`, it fails to include it. – garbagecollector Aug 17 '16 at 23:26
  • > That's definitely not true, at least any longer. Bundler does not look at your system gems, and only goes by what's in the Gemfile. One of reasons it's so terrible. Agreed. This answer isn't correct (anymore? maybe it was once). – jmoney Jan 27 '19 at 02:57
6

You can force bundler to use the gems you deploy using "bundle package" and "bundle install --local"

On your development machine:

bundle install

(Installs required gems and makes Gemfile.lock)

bundle package

(Caches the gems in vendor/cache)

On the server:

bundle install --local

(--local means "use the gems from vendor/cache")

Henry Collingridge
  • 1,950
  • 2
  • 14
  • 22
  • What if I can't install anything on the server? In this case, I need to build with rake but server doesn't have a gem I use for building; but I have no problem checking in the dependencies. – Henrik Feb 08 '12 at 16:22
  • "bundle install" didn't work for me if the gem wasn't included in my existing Gemfile "source. – Anna Oct 02 '12 at 19:45
  • Just remove the gem file temporarily form the gem file before doing `bundle install` After that, copy the gemfile to _vendor/cache_, add the gem back to the gemfile and execute `bundle install --local` – bert bruynooghe Aug 28 '13 at 13:00
  • 1
    `bundle package` is a great suggestion, even now, three years later. If your version of Bundler is older than 2.0 (not yet released at this writing), you will need to use `bundle package --all` if you want to include gems with `path` or `git` dependencies. It will give you a helpful reminder if it detects such dependencies, though. – L2G Jun 24 '14 at 15:56
  • 1
    FYI: this may not work as well as you'd hope if you develop on a different platform than you deploy to (e.g. develop on macOS, deploy to Linux). As per the `--local` docs: "Note that if a appropriate platform-specific gem exists on rubygems.org it will not be found." – Liam Dawson Mar 04 '19 at 01:14
4

Adding .gem to vendor/cache seems to work. No options required in Gemfile.

Victor Moroz
  • 9,167
  • 1
  • 19
  • 23
0

I found it easiest to run my own gem server using geminabox

See these simple instructions.

Dave Sag
  • 13,266
  • 14
  • 86
  • 134