Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
Last active April 28, 2025 23:49
Show Gist options
  • Save jaydorsey/7810580dac0a289581e7ba8168b843e8 to your computer and use it in GitHub Desktop.
Save jaydorsey/7810580dac0a289581e7ba8168b843e8 to your computer and use it in GitHub Desktop.
Setting up a github/bundle token for privately hosted repos

If your Gemfile has a privately hosted package referenced similar to this:

gem 'sekret', git: 'https://github.com/my-private-group/sekret.git', branch: 'main'

You may see a prompt when running bundle install, or other bundler commands, to enter your github username & password.

To resolve this, you need to generate a token and add it to your system.

Generating a token

  1. Log in with your Github account and visit the tokens page
  2. Add a meaningful name, set your expiration, and grant repo access
  3. Copy the token for use later

Setting the token

To successfully run bundle install locally, you need to either add:

  1. A token to your local repo with bundle config --local github.com <token>
    1. Confirm with cat .bundle/config and see your token
    2. NOTE: Be careful using this. Some teams keep a shared/committed .bundle/config file
  2. A token to your global bundle config with bundle config --global github.com <token>
    1. Confirm with cat ~/.bundle/config and see your token
    2. NOTE: Be careful using this if you store your ~/.bundle/config file in a github repo
  3. To your environment with an environment variable export BUNDLE_GITHUB__COM=<token>
    1. Confirm with env | grep BUNDLE_GITHUB__COM and see your token
    2. Protip: Use mise-en-place or direnv to securely set these locally per-folder/repo

Caveats

  • For CI/CD you probably don't want to use a personal token. Use a "bot" account (Github suggests this) with limited access
  • Each engineer should/could use their own token locally
  • You can also use dotenv or anything else
  • There are reasons to use each of the above ways of setting a token
    • Some repos have ./bundle/config checked into the git repository so local + CI/CD uses the same bundle config settings. You can use your global config, unless...
    • Some people (like me) commit their global ~/.bundle/config into a public dotfiles repo so I keep my bundle configs the same across machines. Don't store secrets in here if you do this
    • Using environment variables is recommended if you can't use the bundle config file. Use direnv, mise-en-place, or another tool for managing environment variables per-folder/repo if you need to use the same environment variable with different values in different folders
@stefanjcollier
Copy link

Worked a treat, thanks! 🏆

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