Skip to content

Instantly share code, notes, and snippets.

@atc0005
Forked from zulhfreelancer/local-ruby-gem.md
Created November 17, 2022 22:31
Show Gist options
  • Save atc0005/ffafdb1c1f501f747dee6203b5166815 to your computer and use it in GitHub Desktop.
Save atc0005/ffafdb1c1f501f747dee6203b5166815 to your computer and use it in GitHub Desktop.
How to use a local Ruby gem in Rails project?

Situation

You are working on a Rails app that uses a gem named abc. This gem is hosted on RubyGems and the source code of the gem is available at https://github.com/your-username/abc.

You created a new branch locally for your gem (new-feature). You wanted to modify the gem and load it directly to your local Rails app. And, you don't want to push the gem changes to GitHub and publish the gem to RubyGems just yet.

You want all the changes that you made in your local gem directory get reflected immediately in your local Rails app without requiring you to run gem build and gem install command in the gem's local directory.

Steps

  1. Update Rails app's Gemfile:

    - gem "abc"
    + gem "abc", github: "your-username/abc", branch: "new-feature"
  2. Run bundle config command:

    $ cd /path/to/rails/app
    $ bundle config --local local.abc /full/path/to/local/abc/gem
    
  3. Confirm the bundle config:

    $ bundle config
    
    Settings are listed in order of priority. The top value will be used.
    local.abc
    Set for your local app (/Users/username/path-to-rails-app/.bundle/config): "/full/path/to/local/abc/gem"
    
  4. Bundle

    $ bundle install
    

    You will something like Using abc 1.4.4 from https://github.com/your-username/abc.git (at /full/path/to/local/abc/gem@git-hash) from the bundle outputs.

Done. Now you can modify the abc gem in the /full/path/to/local/abc/gem directory and it will get reflected in your Rails app.

Clean-up

When you don't need that local path anymore, simply run:

$ bundle config --delete local.YOUR_GEM_NAME

And, revert your Rails app's Gemfile:

- gem "abc", github: "your-username/abc", branch: "new-feature"
+ gem "abc"

Followed by bundle install command (if needed).

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment