Install composer packages from private repository from GitLab

Syed Sirajul Islam Anik
2 min readJan 18, 2021

--

Image from: https://codereviewvideos.com/blog/wp-content/uploads/2015/06/composer.gif

I had to install a PHP package that’s not hosted on packagist. Rather, it stays under my organization’s private repository on Gitlab. So, I thought to write an article that can be easy for others if they’re going through the same.

So, to install a package from GitLab which is not a public repository will need a Personal Access Token. You can issue your PAT by going to Profile Icon > Settings > Access Token. Then to issue a new PAT, write a name and select a scope api/read_api, read_api is a minimum. api will work too. If you want to set an expiry, can put value in that. Then press the Create personal access token Button.

Generating PAT

Then you’ll be shown your PAT and you need to copy that token. It’s only shown once. If you close or refresh the tab, it’ll be gone.

Next, from the terminal. Run the following command.

composer config --global --auth gitlab-token.gitlab.com PAT_TOKEN

Replace the PAT_TOKEN with the token you found from the above process. And if you’re using a self-hosted GitLab, then you’ll need to change the URL gitlab.com to your self-hosted URL.

Then, in your composer.json file, add the following snippet.

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

If your composer.json file already contains the repository key, then add the object to it.

Finally,

composer require vendor-name/package-name

It’ll then use the VCS’s repository as a package. Then vendor-name/package-name is the name specified in the package’s composer.json file.

If you just want to add the package to your lock file and don’t install the package in your vendor directory. You can add --no-install flag. If you also want to avoid the post-install scripts, you can add the --no-scripts flag to the command.

For Github, it should be the same. Anyway, that’s all.

Happy coding. ❤

--

--

Syed Sirajul Islam Anik

software engineer with "Senior" tag | procrastinator | programmer | !polyglot | What else 🙄 — Open to Remote