Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Installing a Gem on Heroku from a Private GitHub Repo

Installing a Gem on Heroku from a Private GitHub Repo

Sometimes you want to use a gem on Heroku that is in a private repository on GitHub.

Using git over http you can authenticate to GitHub using basic authentication. However, we don't want to embed usernames and passwords in Gemfiles. Instead, we can use authentication tokens.

This method does not add your OAuth token to Gemfile.lock. It uses bundle config to store your credentials, and allows you to configure Heroku to use environment variables when deploying.

  1. Generate an OAuth token from GitHub

  • You only need to check the repo scope when configuring the token permissions
  1. Update Gemfile to use your private repository

gem 'my_private_repo', git: ''
  1. Configure bundler with your OAuth token

bundle config <your_github_oauth_token>

Now bundle and if everything works locally you are ready to deploy to Heroku!

  1. Finally add BUNDLE_GITHUB__COM to your Heroku environment

$ heroku config:add BUNDLE_GITHUB__COM=<your_github_oauth_token>

You now have a private gem installed on Heroku!

Optional - configure bundler to use your local repo in development

bundle config has another option to work against your local git repository without updating your Gemfile The catch is that you need to specify the git branch when configuring the gem

gem 'my_private_repo', git: '', branch: 'master'

Then run bundle config local.ecto </path/to/your/local/repo>

More information here - see the section "Local git repositories" at the end

Copy link

simon0191 commented Feb 24, 2017

I created a buildpack to solve this problem using a custom ssh key stored as an environment variable:

Feedback is welcome :)

Copy link

rydena021 commented May 3, 2019

This was super helpful. Cheers!

Copy link

dineshvaitage commented Jul 6, 2022

This is awesome!!!

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