2020-06-23 00:09:42 +05:30
---
stage: Package
group: Package
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
---
2021-01-03 14:25:43 +05:30
# Composer packages in the Package Registry
2020-06-23 00:09:42 +05:30
2020-10-24 23:57:45 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/15886) in [GitLab Premium](https://about.gitlab.com/pricing/) 13.2.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Publish [Composer ](https://getcomposer.org/ ) packages in your project's Package Registry.
Then, install the packages whenever you need to use them as a dependency.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Only Composer 1.x is supported. Consider contributing or even adding support for
[Composer 2.0 in the Package Registry ](https://gitlab.com/gitlab-org/gitlab/-/issues/259840 ).
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
## Create a Composer package
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
If you do not have a Composer package, create one and check it in to
a repository. This example shows a GitLab repository, but the repository
can be any public or private repository.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
1. Create a directory called `my-composer-package` and change to that directory:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```shell
mkdir my-composer-package & & cd my-composer-package
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
1. Run [`composer init` ](https://getcomposer.org/doc/03-cli.md#init ) and answer the prompts.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
For namespace, enter your unique [namespace ](../../../user/group/index.md#namespaces ), like your GitLab username or group name.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
A file called `composer.json` is created:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```json
{
"name": "< namespace > /composer-test",
"description": "Library XY",
"type": "library",
"license": "GPL-3.0-only",
"authors": [
{
"name": "John Doe",
"email": "john@example.com"
}
],
"require": {}
}
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
1. Run Git commands to tag the changes and push them to your repository:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```shell
git init
git add composer.json
git commit -m 'Composer package test'
git tag v1.0.0
git remote add origin git@gitlab.example.com:< namespace > /< project-name > .git
git push --set-upstream origin master
git push origin v1.0.0
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
The package is now in your GitLab Package Registry.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
## Publish a Composer package by using the API
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Publish a Composer package to the Package Registry,
so that anyone who can access the project can use the package as a dependency.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Prerequisites:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
- A package in a GitLab repository.
- A valid `composer.json` file.
- The Packages feature is enabled in a GitLab repository.
- The project ID, which is on the project's home page.
- A [personal access token ](../../../user/profile/personal_access_tokens.md ) with the scope set to `api` .
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
NOTE: **Note:**
[Deploy tokens ](./../../project/deploy_tokens/index.md ) are
[not yet supported ](https://gitlab.com/gitlab-org/gitlab/-/issues/240897 ) for use with Composer.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
To publish the package:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
- Send a `POST` request to the [Packages API ](../../../api/packages.md ).
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
For example, you can use `curl` :
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```shell
curl --data tag=< tag > "https://__token__:< personal-access-token > @gitlab.example.com/api/v4/projects/< project_id > /packages/composer"
```
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
- `<personal-access-token>` is your personal access token.
- `<project_id>` is your project ID.
- `<tag>` is the Git tag name of the version you want to publish.
To publish a branch, use `branch=<branch>` instead of `tag=<tag>` .
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
You can view the published package by going to **Packages & Registries > Package Registry** and
selecting the **Composer** tab.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
## Publish a Composer package by using CI/CD
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
You can publish a Composer package to the Package Registry as part of your CI/CD process.
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
1. Specify a `CI_JOB_TOKEN` in your `.gitlab-ci.yml` file:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```yaml
stages:
- deploy
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
deploy:
stage: deploy
script:
- 'curl --header "Job-Token: $CI_JOB_TOKEN" --data tag=< tag > "https://gitlab.example.com/api/v4/projects/$CI_PROJECT_ID/packages/composer"'
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
1. Run the pipeline.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
You can view the published package by going to **Packages & Registries > Package Registry** and selecting the **Composer** tab.
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
### Use a CI/CD template
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
A more detailed Composer CI/CD file is also available as a `.gitlab-ci.yml` template:
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
1. On the left sidebar, click **Project overview** .
1. Above the file list, click **Set up CI/CD** . If this button is not available, select **CI/CD Configuration** and then **Edit** .
1. From the **Apply a template** list, select **Composer** .
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
CAUTION: **Warning:**
Do not save unless you want to overwrite the existing CI/CD file.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
## Install a Composer package
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Install a package from the Package Registry so you can use it as a dependency.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Prerequisites:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
- A package in the Package Registry.
- The group ID, which is on the group's home page.
- A [personal access token ](../../../user/profile/personal_access_tokens.md ) with the scope set to, at minimum, `read_api` .
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
NOTE: **Note:**
[Deploy tokens ](./../../project/deploy_tokens/index.md ) are
[not yet supported ](https://gitlab.com/gitlab-org/gitlab/-/issues/240897 ) for use with Composer.
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
To install a package:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
1. Add the Package Registry URL to your project's `composer.json` file, along with the package name and version you want to install:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
- Connect to the Package Registry for your group:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```shell
composer config repositories.< group_id > composer https://gitlab.example.com/api/v4/group/< group_id > /-/packages/composer/
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
- Set the required package version:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```shell
composer require < package_name > :< version >
```
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
Result in the `composer.json` file:
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
```json
{
...
"repositories": {
"< group_id > ": {
"type": "composer",
"url": "https://gitlab.example.com/api/v4/group/< group_id > /-/packages/composer/"
},
...
},
"require": {
...
"< package_name > ": "< version > "
},
...
}
```
You can unset this with the command:
```shell
composer config --unset repositories.< group_id >
```
- `<group_id>` is the group ID.
- `<package_name>` is the package name defined in your package's `composer.json` file.
- `<version>` is the package version.
1. Create an `auth.json` file with your GitLab credentials:
```shell
composer config gitlab-token.< DOMAIN-NAME > < personal_access_token >
```
Result in the `auth.json` file:
```json
{
...
"gitlab-token": {
"< DOMAIN-NAME > ": "< personal_access_token > ",
...
}
}
```
You can unset this with the command:
```shell
composer config --unset --auth gitlab-token.< DOMAIN-NAME >
```
- `<DOMAIN-NAME>` is the GitLab instance URL `gitlab.com` or `gitlab.example.com` .
- `<personal_access_token>` with the scope set to `read_api` .
1. If you are on a GitLab self-managed instance, add `gitlab-domains` to `composer.json` .
```shell
composer config gitlab-domains gitlab01.example.com gitlab02.example.com
```
Result in the `composer.json` file:
```json
{
...
"repositories": [
{ "type": "composer", "url": "https://gitlab.example.com/api/v4/group/< group_id > /-/packages/composer/" }
],
"config": {
...
"gitlab-domains": ["gitlab01.example.com", "gitlab02.example.com"]
},
"require": {
...
"< package_name > ": "< version > "
},
...
}
```
You can unset this with the command:
```shell
composer config --unset gitlab-domains
```
NOTE: **Note:**
On GitLab.com, Composer uses the GitLab token from `auth.json` as a private token by default.
Without the `gitlab-domains` definition in `composer.json` , Composer uses the GitLab token
as basic-auth, with the token as a username and a blank password. This results in a 401 error.
Output indicates that the package has been successfully installed.
2020-06-23 00:09:42 +05:30
CAUTION: **Important:**
2021-01-03 14:25:43 +05:30
Never commit the `auth.json` file to your repository. To install packages from a CI/CD job,
2020-06-23 00:09:42 +05:30
consider using the [`composer config` ](https://getcomposer.org/doc/articles/handling-private-packages-with-satis.md#authentication ) tool with your personal access token
stored in a [GitLab CI/CD environment variable ](../../../ci/variables/README.md ) or in
2021-01-03 14:25:43 +05:30
[HashiCorp Vault ](../../../ci/secrets/index.md ).