Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

If you are getting message below while trying to install private repositories with composer and want to avoid manual interaction, you can use this example.


Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+clone-php+2019-05-31+2137
to retrieve a token. It will be stored in "/root/.composer/auth.json" for future use by Composer.
Token (hidden):

Setup


You can omit "Token" and "Environment" steps below if you have previously completed both steps for any other projects.


composer.json


Assume that this is your composer.json file.


{
"repositories": [
{
"type": "git",
"url": "git@github.com:myprivate/repo.git",
"no-api": true #This is not compulsory
}
]
}

Obtaining token


  1. Navigate to GitHub's Personal Access Tokens page.

  2. Hit "Generate new token" button.

  3. Type something meaningful "Note", select "repo" as scope and hit "Generate token" button.

  4. Take a note of the token.

Environment preparation


Run command below on local PC.


export COMPOSER_AUTH='{"github-oauth":{"github.com":"AB8cd4cab23a9d5399934a7d7698d3fa74e9cfAB"}}'

docker-composer.yaml


Add entry below to docker-compose.yaml file as an environmental variable.


COMPOSER_AUTH: "${COMPOSER_AUTH}"

Test


If you run composer instal/update/require commands, you should not be asked to enter token.