Use different PHP version with CLI / composer on Forge
I have a hosting where I need two PHP versions: 7.4 and 8.1.
The 7.4 is the default version (multiple websites are using it, so can't change the default).
I have changed version from 7.4 to 8.1 for one of my existing websites, but hit a snag, the composer is not aware that I have changed the PHP's version and it still tries to use 7.4, so the composer is failing with the following message:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires php ^8.1 but your php version (7.4.32) does not satisfy that requirement.
I tried manually running the composer with php8.1 composer update, but had the same error. For the moment I fixed this by changing the CLI's version to 8.1, ran composer update and deployed the site and then switched back to 7.4. But I'm sure there's a more elegant solution to this problem, than switching version manually every time I deploy.
Can anyone give me a hint on how to "tell" the Forge to use the PHP's version set for a website that has been set under Meta? I assume this is possible ...
Shouldn't the deployment variable $FORGE_COMPOSER take care of that on it's own? Or do I need to update my deployment script with the command you suggested?
This will install the php-fpm packages for both PHP versions, and configure them using the update-alternatives command. You can then switch between the PHP versions by running the update-alternatives command with the appropriate version number.