438

I'd like Bundler to load a local gem. Is there an option for that? Or do I have to move the gem folder into the .bundle directory?

gotqn
  • 42,737
  • 46
  • 157
  • 243
picardo
  • 24,530
  • 33
  • 104
  • 151

6 Answers6

658

I believe you can do this:

gem "foo", path: "/path/to/foo"
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Jimmy
  • 35,686
  • 13
  • 80
  • 98
  • 13
    A hard-coded path is fine for a quick hack session, but bloudermilk's local gem solution is more effective for projects under version control. It lets you keep checking in both projects - gem and project using the gem - such that others can don't have to check out the gem source or share the same paths. – mahemoff Jun 12 '14 at 08:42
  • 4
    You can make this slightly cleaner by using a .gitignored symlink to your local gem in your project directory -- that way you can use source control on both projects separately and others can do the same without having an identical directory structure. – Dan May 27 '15 at 08:01
  • 10
    Something to watch out for might be Spring. If you are using a local path for your gem you could notice cached versions of your local gem like I did in rails console. If it doesn't seem like your local gem changes are being picked up try `spring stop` to see if it is indeed the issue. – Jason R Sep 10 '16 at 18:25
  • 1
    Adding the local gem path to config/spring.rb seems to help pickup your local gem changes after restarting rails console/server. – Jason R Sep 10 '16 at 18:52
  • 6
    Way better way of doing it this here: https://rossta.net/blog/how-to-specify-local-ruby-gems-in-your-gemfile.html – Cyzanfar Oct 24 '16 at 16:13
  • 4
    This is a perfectly fine way of doing it if you are testing local development of a gem on a larger project. In this case, this is the "better" way to do it then having to push to a repository to test local changes. – Andy Baird Nov 20 '16 at 19:35
  • 1
    **NOTE:** You cannot specify the `branch` to use when using `path`, so make sure the correct branch is checked out in the local file system when doing this. – Joshua Pinter Feb 22 '19 at 17:04
277

In addition to specifying the path (as Jimmy mentioned) you can also force Bundler to use a local gem for your environment only by using the following configuration option:

$ bundle config set local.GEM_NAME /path/to/local/git/repository

This is extremely helpful if you're developing two gems or a gem and a rails app side-by-side.

Note though, that this only works when you're already using git for your dependency, for example:

# In Gemfile
gem 'rack', :github => 'rack/rack', :branch => 'master'

# In your terminal
$ bundle config set local.rack ~/Work/git/rack

As seen on the docs.

sibo
  • 5
  • 4
bloudermilk
  • 17,820
  • 15
  • 68
  • 98
  • 1
    I am having issues with this because I am using `BUNDLE_PATH` (building a package for distribution). When doing what you suggested, or Jimmy's answer, it only does a `using`, and not actually installing to my `BUNDLE_PATH` folder. I was not able to figure this out, any help? – Automatico Feb 13 '14 at 14:46
  • 3
    Note this won't work with a **gemspec**, per discussion [here](https://github.com/bundler/bundler/issues/2911). – davetapley Jul 10 '14 at 18:27
  • 47
    To disable the local override: `bundle config --delete local.GEM_NAME` – alxndr Nov 29 '14 at 21:56
  • I had to remove version directive from the gem line to get this to work. – Epigene Jan 27 '16 at 15:20
  • 4
    If you would like to use a different branch in development from production, you also have to set `bundle config disable_local_branch_check true` or Bundler will complain about the branch. Be careful with this though, as the checks are supposed to stop incorrect commits getting into `Gemfile.lock`. Docs here: http://bundler.io/v1.12/git.html – Leo May 31 '16 at 15:13
  • Note that the path provided in the `bundle` command should be the root directory of the other gem's source code – Benjineer Nov 19 '19 at 01:00
40

You can also reference a local gem with git if you happen to be working on it.

gem 'foo',
  :git => '/Path/to/local/git/repo',
  :branch => 'my-feature-branch'

Then, if it changes I run

bundle exec gem uninstall foo
bundle update foo

But I am not sure everyone needs to run these two steps.

Rimian
  • 36,864
  • 16
  • 117
  • 117
  • 5
    this is great, but is not that convenient if you're actively developing the gem... because you'll need to commit every change & `bundle uninstall && bundle install `, for every change you want reflected on your app – Ramses Nov 03 '17 at 18:17
18

In order to use local gem repository in a Rails project, follow the steps below:

  1. Check if your gem folder is a git repository (the command is executed in the gem folder)

    git rev-parse --is-inside-work-tree
    
  2. Getting repository path (the command is executed in the gem folder)

    git rev-parse --show-toplevel
    
  3. Setting up a local override for the rails application

    bundle config local.GEM_NAME /path/to/local/git/repository
    

    where GEM_NAME is the name of your gem and /path/to/local/git/repository is the output of the command in point 2

  4. In your application Gemfile add the following line:

    gem 'GEM_NAME', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    
  5. Running bundle install should give something like this:

    Using GEM_NAME (0.0.1) from git://github.com/GEM_NAME/GEM_NAME.git (at /path/to/local/git/repository) 
    

    where GEM_NAME is the name of your gem and /path/to/local/git/repository from point 2

  6. Finally, run bundle list, not gem list and you should see something like this:

    GEM_NAME (0.0.1 5a68b88)
    

    where GEM_NAME is the name of your gem


A few important cases I am observing using:

Rails 4.0.2  
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] 
Ubuntu 13.10  
RubyMine 6.0.3
  • It seems RubyMine is not showing local gems as an external library. More information about the bug can be found here and here
  • When I am changing something in the local gem, in order to be loaded in the rails application I should stop/start the rails server
  • If I am changing the version of the gem, stopping/starting the Rails server gives me an error. In order to fix it, I am specifying the gem version in the rails application Gemfile like this:

    gem 'GEM_NAME', '0.0.2', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
gotqn
  • 42,737
  • 46
  • 157
  • 243
2

You can reference gems with source:

source: 'https://source.com', git repository (:github => 'git/url') and with local path

:path => '.../path/gem_name'.

You can learn more about [Gemfiles and how to use them] (https://kolosek.com/rails-bundle-install-and-gemfile) in this article.

Nesha Zoric
  • 6,218
  • 42
  • 34
-3

If you want the branch too:

gem 'foo', path: "point/to/your/path", branch: "branch-name"
C.J.
  • 15,637
  • 9
  • 61
  • 77
  • This doesn't work, generates an error: `Only gems with a git source can specify a branch.` – KenB Jun 07 '19 at 20:20