---
title: Allow composer to install private Github repository
url: https://calvin.my/posts/allow-composer-to-install-private-github-repository
published: 2025-08-07
updated: 2026-09-16
category: Operations
tags:
- GitHub
- composer
- Laravel
summary: The post explains how to let Composer access private GitHub repositories in local and deployment environments. For local use, it recommends creating a fine-grained personal access token limited to a specific repository with read-only contents access, then storing it in Composer authentication settings. For remote servers, it uses a dedicated SSH key added as a GitHub deploy key, plus SSH and Git configuration to route repository access through that key.
---

# Allow composer to install private Github repository

This article shows the step to setup composer to install private Github repository.

* * *
## Case 1 - For Local Machine

### Get token from Github

1. Go to settings -\> Developer Settings -\> Personal Access Tokens -\> [Fine-grained tokens](https://github.com/settings/personal-access-tokens)

2. Clicks on the "Generate new token" button.

3. Select an expiry period

   ![](https://camy-pub.s3.ap-southeast-1.amazonaws.com/2fbeb711-91a8-41e9-9f68-74871dfa2bb2.png)

4. Select a specific repo.

   ![](https://camy-pub.s3.ap-southeast-1.amazonaws.com/cacdedf2-da06-428b-b477-b5400a5951c5.png)

5. Provide "Contents" read-only access.

   ![](https://camy-pub.s3.ap-southeast-1.amazonaws.com/3049db6d-3056-4124-9ac5-7da32437d00f.png)

6. Generate and copy the token.

### Use the token with composer

1. Setup composer auth config by using the following command.

   ```bash
   php composer.phar config -g github-oauth.github.com <token>
   ```

2. Or from the auth.json directly.

   ```json
   # Global
   $ pico ~/.composer/auth.json
   {
     "github-oauth": {
       "github.com": "github_pat_xxxxx"
     }
   }

   # Or, Per project (Recommended)
   $ pico /path/to/app/auth.json
   {
     "github-oauth": {
       "github.com": "github_pat_xxxxx"
     }
   }
   ```

* * *

## Case 2 - For Remote Deployment Server

1. Create a key-pair for the repository.

   ```bash
   ssh-keygen -t ed25519 -C "For repo2"
   ```

2. Setup SSH Config.

   ```bash
   $ pico /etc/ssh/ssh_config.d/repo2.conf

   Host repo2
   HostName github.com
   User git
   IdentityFile ~/.ssh/key2
   ```

3. Add key2.pub as deployment key on Github.

4. Setup git Config to rewrite the host

   ```bash
   pico ~/.gitconfig

   [url "git@repo2:org-name/ori-name.git"]
     insteadOf = git@github.com:org-name/ori-name.git
   ```
