How to use Composer 2 in PHP applications

Goal

With the release of Composer 2, you might want to use it in your PHP application.

Assumptions

You already have a Platform.sh project set up and a .platform.app.yaml that is configured accordingly for PHP.

Problems

Platform.sh PHP images ship with Composer 1.

Steps

Since you can declare additional dependencies via your .platform.app.yaml, Composer 2 can be required like this:

dependencies:
    php:
        composer/composer: '^2.0'

Conclusion

After you commit and push your changes, with the next build and redeploy, the application container will get Composer 2 (both during build and runtime):

web@app.0:~$ composer --version
Composer 2.0.2 2020-10-25 23:03:59
3 Likes

The build phase still uses composer 1:

dependencies:
    php:
        'composer/composer': '^2.0.7'
        'drush/drush': '^10.3.6'

build:
    flavor: composer

The build log still says:

  Installing build dependencies...
    Installing php build dependencies: composer/composer, drush/drush
    W: Changed current directory to /app/.global
    W: Loading composer repositories with package information
    W: Warning from https://repo.packagist.org: You are using an outdated version of Composer. Composer 2.0 is now available and you should upgrade. See https://getcomposer.org/2
    W: Updating dependencies (including require-dev)
    W: Package operations: 53 installs, 0 updates, 0 removals