How to force Composer to use a specific PHP-version

Sometimes you just want Composer to create a lock-file for a specific PHP-version.

Ralph J. Smit Laravel Software Engineer

This week I had a situation where I was running a different PHP-version locally than on the server. The reason for that isn't relevant, but I know this happens more often than you think. In this short tutorial I'll show you how to override your local PHP-version and force Composer to use a specific PHP-version.

Force Composer to use a specific PHP-version

The fix for this is actually pretty simple. To override the Composer PHP-version, go to your composer.json file. Under the key config, you should add a new array:

"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {},
+ "platform": {
+ "php": "8.0.5"
+ }
},

This will force Composer to use that specific PHP-version. It is a good idea to use SSH to go to your server and check the exact PHP-version, so that you can use that one.

The benefit of doing this is that the complete team will generate the same composer.lock files, so I'd recommend this for most (critical) apps.

Published by Ralph J. Smit on in Guides . Last updated on 31 December 2022 .