Adding a Private BitBucket repo to Composer
If you’re anything like me, you occasionally try to make life easier for yourself. But at the same time, you forget about making life easy for yourself and end up going round and round in circles. C’est la vie.
I’m a bit of a fan of Bitbucket, mainly because of the free private repository support. Most of the projects I work on are for either our business or a client and therefore closed source.
But there are times where things will work interchangeably and one project can form part of a larger one – not uncommon but can be tricky when dealing with things like Composer.
I hit a few snags when trying to add a private repo to a project through Composer, so hopefully, these few steps help you avoid the same issues.
Setting up OAuth Access
The way Composer will access Bitbucket is through oAuth. So first of all, we’ll need to grab a key and a secret.
When you’re logged into Bitbucket, click on your avatar bottom left, and select “All Workspaces”

This will list all the workspaces you have access to. You may only have one, or you could have quite a few, depending on the way you work.
Select “Manage” next to the workspace you want to use.

And then pick “OAuth Consumers” from the left hand menu under the Apps and Features heading.

Almost there! You just need to create a new consumer, so go ahead and do this here. Give them at least read access to the repositories, but you can give them any name you fancy and leave the other fields blank.
You should however, tick the This is a private consumer option.
And that’s that part done! Don’t close that tab quite yet though.
Setting up Composer Access
If you haven’t already, open up your project that you want to access your private repo. You need to do at the root of your project, where your composer.json file is kept.
Create a new file called auth.json. This is where you’ll save the key and secret for Bitbucket that we’ve just created. The auth file will consist of just a few lines, something like this:
{
"bitbucket-oauth": {
"bitbucket.org": {
"consumer-key": "Paste your consumer key here",
"consumer-secret": "Paste your consumer secret here"
}
}
}
Linking Composer to your Private Repo
So you’ve granted access to a new OAuth client, you’ve given Composer the credentials, we just now need to include it with your project.
There’re two ways to do this –
- Add it to your
composer.jsonfile manually - Ask Composer to add it for you
We’ll start with number 2, which would consist of something like the following. Bear in mind, you should use the HTTPS version here rather than the SSH, because of the way Composer accesses the files.
composer config repositories.vendor/package-name vcs https://username@bitbucket.org/vendor/package-name.git
The vendor/package-name should match the package name defined in the composer.json file of the private repo. But now, you’re good to go.
This just includes a few lines into your composer.json of your new project, identical to what you could have done manually (option 1).
"repositories": [
{
"name": "vendor/package-name",
"type": "vcs",
"url": "https://username@bitbucket.org/vendor/package-name.git"
}
]
That’s it!
Now just require your private repo as you normally would, and away you go!

Thank for great tutorial – simple & straightforward. One thing to add – composer requires also setting up a Callback URL when generating OAuth at Bitbucket. It can be any URL e.g. “http://anything.com”, just not blank field. Composer will not use it anyway.
————————————————————
Composer (2.3.10) output:
Loading composer repositories with package information
Invalid OAuth consumer provided.
This can have three reasons:
1. You are authenticating with a bitbucket username/password combination
2. You are using an OAuth consumer, but didn’t configure a (dummy) callback url
3. You are using an OAuth consumer, but didn’t configure it as private consumer
Could not fetch https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo?fields=-project%2C-owner, please create a bitbucket OAuth token to access private repos
Follow the instructions on https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/
to create a consumer. It will be stored in “D:\_projects\myproject\app/auth.json” for future use by Composer.
Ensure you enter a “Callback URL” (http://example.com is fine) or it will not be possible to create an Access Token (this callback url will not be used by composer)
Consumer Key (hidden): Access is denied.
No consumer key given, aborting.
You can also add it manually later by using “composer config –global –auth bitbucket-oauth.bitbucket.org ”
In AuthHelper.php line 183: Could not authenticate against bitbucket.org