Install & use Composer for dynamic PHP version

313 views Asked by At

On my local machine I have MAMP PRO running, which allows the change of PHP version per project independently.

Composer is installed on a specific PHP version, but my project aren't.

How would I use composer so that it will either use the PHP version of the local environment, or so that I can easily tell Composer that I am using PHP "x.x.x".

Thank you in advance!

2

There are 2 answers

0
Denis O. On BEST ANSWER

I believe you're looking for this thing: https://getcomposer.org/doc/06-config.md#platform

{
    "config": {
        "platform": {
            //"php": "/path/to/php or php version"
            "php": "7.0.3"
        }
    }
}
3
DarkShade On

You can do this using the composer.phar file downloaded for each project, rather than the global installation of composer. For example

php7.4 composer.phar install or php8.2 composer.phar install or etc.

For downloading composer.phar

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

ps: don't forget to add composer.phar to gitignore.