Skip to the content.

Composer (PHP)

Replace Packagist with the proxy. The examples use the upstream name packagist; replace it with the name your operator gave the upstream (what you need).

   
Upstream type composer
Package source https://proxy.example.com/v1/packagist
Clients composer
Tested Composer 2.11: in CI on every push and in the client lab.

Configure

Switch packagist.org off, and add the proxy. Composer asks a repository that is merely added first — and keeps asking packagist.org too. In the global configuration, for every project and without touching a composer.json:

composer config --global repositories.packagist.org false
composer config --global repositories.pullthrough composer https://proxy.example.com/v1/packagist

For a single project, the same two entries in its composer.json:

  "repositories": [
    { "type": "composer", "url": "https://proxy.example.com/v1/packagist" },
    { "packagist.org": false }
  ],

Redefining packagist is not enough. Setting repos.packagist to the proxy with composer config --global looks like a replacement of the default repository and is not one (Composer 2.11): Composer installs from the proxy and still contacts repo.packagist.org — visibly so in composer audit, which fails where that host is unreachable, and silently during require and update. The lab’s name server caught exactly this.

Authenticate

Only when the proxy has access control switched on. Composer keeps credentials per host in auth.json (next to the global configuration, or in the project — do not commit it). The proxy takes your proxy token either way:

composer config --global --auth http-basic.proxy.example.com pullthrough "<token>"
composer config --global --auth bearer.proxy.example.com "<token>"

Trust a private CA

composer config --global cafile /etc/ssl/corp-ca.pem

Check that it works

composer require --prefer-dist --no-progress psr/log:3.0.0
composer audit

composer audit doubles as the check that nothing but the proxy is asked. composer config --global --list shows the repositories; -vvv prints every URL Composer downloads.

Good to know

On the proxy

upstreams:
  packagist:
    type: composer
    url: https://repo.packagist.org

packages.json and the v2 metadata are served live with every URL rewritten to the proxy (which is why server.publicUrl must be set); dist archives are cached immutably under a URL that names package, version and reference. Details: backends: composer.