Are you tired of running unsuccessful composer commands which throw PHP incompatibility errors and you are handling these errors by keep uninstalling/installing composer with different PHP versions?
The real problem:-
For example, you have multiple projects on your localhost or production server and some of them use composer as a dependency manager and the projects use different PHP versions. So whenever you install composer it asks for a specific PHP path. let’s say you gave it PHP 8.1 path. Now if you try to run any composer command in a project that is expecting a PHP version other than PHP 8.1. it will throw errors. Because while you were installing the composer you bound your composer to PHP 8.1 by giving its path.
So to handle these problems there is the finest solution that does not need composer installation.
You just need two things.
1. Composer.phar
file for each project that uses composer as a dependency manager. You can download it manually from here
2. Your desired PHP path
Now it’s time to get dirty your hands. Let’s do some real examples.
I am assuming that you have already installed multiple PHP(i.e 7.3, 7.4, 8.0, 8.1) versions on your server.
Go to your example project foo
and paste the downloaded Composer.phar
file here. Let’s say foo
runs on PHP7.4. So your command signature will be {path_to_php7.4} composer.phar {composer_command}
. Let’s assume your PHP7.4 path is /usr/bin/php74
and you want to run update
command, so open the terminal in the foo
directory and run the following command.
/usr/bin/php74 composer.phar update
Similarly, Go to your project bar
that runs on PHP8.1. Just paste your downloaded Composer.phar file into the bar
directory. Now here you want to install a dependency symfony/mailer
. So now open the terminal in the current directory and run the following command.
/usr/bin/php81 composer.phar require symfony/mailer
Wasn’t so easy? Please comment If you like it or if you have any questions