How to use a private GitLab repository as a Composer dependency

The Composer documentation provides instructions for using a private GitHub or BitBucket repository as a dependency. This note covers GitLab.

Create a personal access token

Create a personal access token, as per the GitLab documentation. Set the token scope to “api”.

Add your personal access token to Composer

Register your personal access token with Composer:

composer config \
  --auth gitlab-token.gitlab.com "your-token" \
  --no-ansi \
  --no-interaction

If you prefer to do this manually, create ~/.composer/auth.json, with the following content:

{
  "gitlab-token": {
    "gitlab.com": "your-token"
  }
}

Tell Composer where to find your package

By default, Composer searches for packages in the official Packagist repository. Override this behaviour by adding the GitLab repository to your composer.json:

{
  "repositories": [
    {
      "type": "vcs",
      "url": "git@gitlab.com:project-name/repo-name.git"
    }
  ]
}

Require the package

Require the package in the usual way:

composer require project-name/repo-name

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.