Tell Composer to use Different PHP Version

173,278

Solution 1

Ubuntu 18.04 case ... this run for me.

/usr/bin/php7.1 /usr/local/bin/composer update

Solution 2

Maybe this can't solve exactly your issue but probably it will help others who comes here from web search.

Just run the command to add below code to your composer.json file to set different PHP version:

$ composer config platform.php 8.0.7
"config": {
    "platform": {
        "php": "8.0.7"
    }
}

Solution 3

On xubuntu I had php 7.2 as default. But needed to run composer with php 5.6.

So this worked for me:

php5.6 /usr/bin/composer

Solution 4

If you just need to get composer to ignore the version requirements, you can bypass using:

composer install --ignore-platform-reqs

Solution 5

You always can use that way.

In project folder where you has composer.json file. Run the command

 php7.4 /usr/local/bin/composer install

or

php7.4 /usr/local/bin/composer update

where php7.4 your specific version can be(php7.0, php5.5, php7.3 etc...) where /usr/local/bin/composer path for system folder with composer

!!! you should have php7.4-cli

sudo apt-get install -y php7.4-cli

That way for linux/ubuntu user

Cheers ;-)

Share:
173,278
Boom
Author by

Boom

Knowledgeable in Java, JavaScript, C/C++, C#, GML, and TorqueScript.

Updated on February 03, 2022

Comments

  • Boom
    Boom about 2 years

    I've been stuck at this for a few days. I'm using 1and1 hosting, and they have their PHP set up a bit weird.

    If I use just php composer.phar install, then I'm using PHP 4.4.6, which is horribly insufficient. However, I can run php5.5 composer.phar install, get a little bit further, but it still fails because somewhere along the line, PHP is called again, but it fails, as it's using 4.4.6.

    Is there any way to tell Composer to use the php5.5 command? Has anyone successfully gotten Composer configured on 1and1 hosting?

    I'm trying to get Laravel up and running (which uses Composer). I've been able to do it on my GoDaddy domain, but not 1and1.

  • Boom
    Boom over 8 years
    I don't have write access to the home directory. 1and1 has littered this domain with red tape.
  • Boom
    Boom about 8 years
    I don't have a post-rootpackage-install` field. Should I add one?
  • Carlo Espino
    Carlo Espino about 8 years
    Is not needed, you can achieve same effect running cp .env.example .env, it just copies contents of .env.example to a new file called .env
  • Gregory Cosmo Haun
    Gregory Cosmo Haun over 4 years
    I guess this isn't really the answer the OP was looking for, but it matches the title of the question very well and might be of great help to people arriving here from web search as it was to me.
  • Pedro Lobito
    Pedro Lobito almost 4 years
    beware, this answer may break your system.
  • Bhavesh Prajapati
    Bhavesh Prajapati almost 4 years
    I have use sudo /opt/lampp/bin/php /usr/bin/composer install and it works for me Thank you +1
  • samerivertwice
    samerivertwice over 3 years
    Will this override the shebang at the start of the file?
  • Ibrahim.H
    Ibrahim.H over 3 years
    That's right, although it's not recommended to change the Shebang in the head of composer, you could break the original composer.phar file.
  • Sybille Peters
    Sybille Peters almost 3 years
    Isn't this the same thing the OP was doing which failed for him because "but it still fails because somewhere along the line, PHP is called again, but it fails, as it's using 4.4.6."?
  • Sybille Peters
    Sybille Peters almost 3 years
    How is this fundamentally different from what the OP did and which did not work for him (php5.5 composer.phar install) because the commands invoked by Composer will still use the default PHP version? Test this yourself, add a "scripts" : "php-version": "php --version" and call e.g. /user/bin/php7.4 /usr/bin/composer php-version.
  • Darius.V
    Darius.V almost 3 years
    maybe no, because he done in not exactly same way
  • pixelbrackets
    pixelbrackets over 2 years
    This is the correct answer. As Sybille outlined with the example the platform setting does not change any local PHP paths. Instead Composer will use this value to fetch dependencies matching this version only. No more, they still run with the current PHP version. Only a change of the $PATH variable will change the PHP version used to run subscripts.
  • heady12
    heady12 over 2 years
    Legend, just what I needed :)
  • Boyan Georgiev
    Boyan Georgiev over 2 years
    Added "$@" at the end, like so: /opt/plesk/php/7.4/bin/php /usr/local/psa/var/modules/composer/composer.phar "$@"
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    This won't run Composer with a different PHP version, but configure Composer to run the dependency resolver for another PHP version. IMHO, this does not answer the given question properly
  • Nico Haase
    Nico Haase about 2 years
    Please share more details. What do you mean by "SIP"? Also, on shared hosting (as given in the question), you cannot edit anything in /usr/local/bin
  • Anony Mous
    Anony Mous about 2 years
    @SybillePeters, change your script to be e.g. "php-version": "/user/bin/php7.4 --version" I suppose you could configure your entire machine to use a specific version for php using sudo update-alternatives --set php /usr/bin/php7.4
  • wapmorgan
    wapmorgan about 2 years
    But this sometimes can help. For example, when you build your app in docker, and have another php version on local machine where develops
  • ssi-anik
    ssi-anik about 2 years
    just in case someone wants to verify if it works or not, try this. stackoverflow.com/a/56379908/2190689
  • pixelbrackets
    pixelbrackets about 2 years
    One more comment - there are several tools available to change the PHP version via the $PATH env variable. One example is github.com/webit-de/php-version-pickup. It picks up the version from a .php-version file and continues to use it in the current shell session, also for all subprocesses like the ones initiated by Composer.
  • Neek
    Neek almost 2 years
    Despite being down voted, this did work for me :) Fresh Rocky linux install with composer from remi repo, composer kept trying to use the core system PHP 7.2 and not my PHP 7.4 from remi required by composer.json packages.