Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

depalmo's avatar

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 ...

0 likes
9 replies
Sinnbeck's avatar

First run which composer which will give you the path

Then run php8.1 /path/to/composer install

1 like
depalmo's avatar

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?

Sinnbeck's avatar

@depalmo oh didn't see it was forge. Don't know how to do it there. I'm sure there is a built in way

tisuchi's avatar

@depalmo TL;DR, you can give this a try with this approach to maintain the multiple PHP Version.

  1. Install the php-fpm package for the PHP version you want to use:
sudo apt-get install -y php7.4-fpm
  1. Install the php-fpm package for the other PHP version:
sudo apt-get install -y php8.1-fpm
  1. Run the update-alternatives command to configure the PHP versions
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set php /usr/bin/php8.1

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.

depalmo's avatar

@tisuchi Sorry I was not clear, I was asking specifically on how to do it through Forge, not on any standard hosting.

evelyn@logalt.net's avatar

Late reply but check out the meta tab on the site option -- you can set separate defaults for the cli PHP version

krytletters's avatar

Hi, this is what I did...not sure if Forge themselves can correct me.

  1. under the server install PHP
  2. under the site settings (meta tab is now settings)
  3. edit your deploy script to use the hard coded version (php8.2 /usr/local/bin/composer (install/update)
tmaeg's avatar

In my case, Edit Deployment Script (upper right of the site settings) solved the problem.

- $FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader
+ $FORGE_PHP $(which composer) install --no-interaction --prefer-dist --optimize-autoloader
1 like

Please or to participate in this conversation.