How to use Composer 2 in PHP applications
Jonas Kroeger
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'
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
0
Comments
The build phase still uses composer 1:
The build log still says:
Indeed as of the date of writing of this answer, the flavor provided is still on composer 1 (that may change in the future).
That means that when composer is installing composer 2 it will display the
You are using an outdated version of Composerwarning.Subsequent installs in the hooks should use composer 2.
Important to note is also that not all plugins are (yet) compatible with composer 2.
A symptom for that incompatible plugins can also be when the following message is displayed:
With an incompatible plugin, composer 2 will fallback to composer 1.
To verify if it’s indeed a specific plugin that does cause that message to be displayed, run
grep '"composer/composer":' composer.lock:If the output doesn’t include
composer ^2.0.0, as in the 3rd line of the following output, then the linked plugin probably needs an update.More information regarding the “caret version range operator” can be found in composer’s documentation below:
https://getcomposer.org/doc/articles/versions.md#caret-version-range-
Take care about this method: it will male Composer 2 available, but it will add to the php include_path a set of Symfony components with specific versions. Those will take by default precedence over the versions of the same components that you install in your app… This can cause problems if the versions are not the same.
It might be fixed by altering php’s include path after having run composer - but then it might be just easier to install composer 2 as phar binary via a shell script
Please sign in to leave a comment.