Installing a gem from a private GitHub repo (Heroku)

frodsan
2 min readFeb 29, 2016

--

This is a little guide on how to install private gems hosted in GitHub on the Heroku platform.

0. Getting started

To follow this guide, you need Bundler +1.11.

$ bundle --version Bundler version 1.11.2

1. Get your OAuth Token from GitHub

To authenticate Bundler to GitHub, you will need an OAuth token. You can follow this guide to create a new one.

2. Setting up your credentials

To set up your credentials, use bundle-config:

$ bundle config GITHUB__COM myoauthtoken:x-oauth-basic

If you want to apply this configuration only to your current project, do this instead:

$ bundle config --local GITHUB__COM myoauthtoken:x-oauth-basic

3. Install your dependencies

Add your private gem to the Gemfile:

gem "mygem", git: "https://github.com/user/mygem.git"

Install the gem dependencies using bundle install:

$ bundle install
Fetching https://github.com/user/mygem.git
Fetching gem metadata from https://rubygems.org/.........
Fetching version metadata from https://rubygems.org/..
Using mygem 1.0.0 from https://github.com/user/mygem.git (at master@bc4adfb)
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.

Notice that your credentials aren’t stored in the Gemfile or the generated Gemfile.lock.

4. Setting up Heroku

At the moment of writting, Heroku’s default Ruby buildpack uses an older version of Bundler (1.9.7), but you can use the latest version of the buildpack with:

UPDATE: Heroku now uses Bundler 1.11, you can skip this step.

$ heroku buildpacks:remove heroku/ruby 
$ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-ruby

Then, add your GitHub credentials to Heroku:

$ heroku config:add BUNDLE_GITHUB__COM=myoauthtoken:x-oauth-basic

Now, run git push heroku master to create a new release.

$ git push heroku master
# ...
remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote: Using bundler 1.11.2
remote: Using mygem 1.0.0 from https://github.com/user/mygem.git (at master@bc4adfb)
remote: Bundle complete! 1 Gemfile dependency, 2 gems now installed.

--

--