---
title: Use gems from private GitHub repos
author: Victor Cobos
tags: Rails, GitHub, Ruby
published_at: '2024-04-02T22:39:00+02:00'
updated_at: '2024-07-24T11:25:10+02:00'
canonical_url: https://www.dotruby.com/articles/use-gems-from-private-github-repos
---

# Use gems from private GitHub repos

Learn how to seamlessly integrate private gems from GitHub into your Ruby projects. This guide covers setting up your Gemfile, configuring local credentials, and using GitHub Actions for authentication. Perfect for developers looking to enhance their workflow with private repositories.

This brief guide walks you through the steps to install private gems hosted on GitHub.

### GitHub personal access token
To authenticate bundler to GitHub, you will need a personal access token. Follow this [guide](https://help.github.com/articles/creating-an-access-token-for-command-line-use/) to create one.

### Setup Gemfile
Like any other gems, your private gem needs to be listed in your `Gemfile`. Add your private gem to the Gemfile:

```
gem "<gem>", git: "https://github.com/<owner>/<repo>.git", branch: "<branch>"
```

Use `https` link,  it's important for the authentication part.

### Setting up your credentials locally
This step may not always be necessary, depending on how you run the project locally and the permissions you have.
To set up your credentials, use `bundle-config`:

```bash
bundle config --local github.com "<personal access token>"
```

Install the gem dependencies using `bundle install` as usual.

### Github Actions
You can specify `env` variables when running Github Actions, and that's what we'll use for authentication. Go to your repo's secrets page `https://github.com/[user]/[repo]/settings/secrets/actions` and create a repository secret with the personal access token. You can use it in your workflow file like so:

```yaml
name: your_job

jobs:
  your_job:
    runs-on: ubuntu-latest
    env:
      BUNDLE_GITHUB__COM: "x-access-token:${{ secrets.PERSONAL_ACCESS_TOKEN }}"
```

### Deploying
Just add the same `BUNDLE_GITHUB__COM` env variable to your server:

```bash
BUNDLE_GITHUB__COM=x-access-token:<personal access token>
```

And you are ready to deploy 🚀
