PrimuS
PrimuS

Reputation: 2683

Composer uses wrong php version, but php -v shows the correct one (Ubuntu)

I'm trying to install my composer packages, but it gives me this:

This package requires php >=7.0.0 but your PHP version (5.5.9)

But php -v gives me this: PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )

I am running an Ubuntu 16.04.3 LTS machine, I found some soultions for Mac and Windows, but nobody seems to have the issue on Linux?

Upvotes: 34

Views: 36923

Answers (9)

JatinBhattPhp
JatinBhattPhp

Reputation: 43

/opt/cpanel/ea-php81/root/usr/bin/php /opt/cpanel/composer/bin/composer install

Above command solved my issue.

Upvotes: 0

hakan
hakan

Reputation: 168

None of above didnt worked for centos 7. After this command composer php version fixed The correct answer below

SSH Command:

scl enable ea-php74 'composer diagnose'

Upvotes: 0

Yuri Tinyukov
Yuri Tinyukov

Reputation: 144

Well, this worked for me

$ alias composer="php /usr/bin/composer.phar"
$ composer install

Use the exact php binary in the alias, for example

$ alias composer="php8.1 /usr/bin/composer.phar"

Upvotes: 2

MC Emperor
MC Emperor

Reputation: 23047

I recently came across the same problem. php --version returned 7.4.30, but Composer said it was using PHP 8.0.18.

It turns out Composer is using its own PHP version. The composer script contains a hardcoded path to PHP 8. (To me, this is a composer bug, as Composer should respect the value of the config.platform.php property of the composer.json file.)

  1. An option may be to alias composer:

    alias composer='/usr/local/bin/php /usr/bin/composer.phar
    
  2. Another option may be to rewrite composer:

    cat /usr/bin/composer \
        | sed 's~/usr/bin/php8~/usr/local/bin/php~g' \
        > /usr/bin/composer.tmp
    mv /usr/bin/composer.tmp /usr/bin/composer
    

This is how I found out. First, I wanted to find the location of composer. By using whereis composer, one can find the path of the composer command. For me, it returned

composer: /usr/bin/composer

I then wanted to see the contents of /usr/bin/composer, so I could find out what the composer command was doing under the hood. By using cat /usr/bin/composer, the contents of the composer script are printed. For me, it returned

#!/bin/sh

/usr/bin/php8 /usr/bin/composer.phar "$@"

There it is. The composer command uses hardcoded /usr/bin/php8 to execute the composer.phar file.

Upvotes: 1

Roland
Roland

Reputation: 304

composerreferences the PHP executable here as follow:

#!/usr/bin/env php

When I do which php I get /c/Program Files/php-7.1/php under GIT-Bash (Windows 10).

Under Linux (at home I have Debian), php may be a symbolic link to an actual PHP binary.

So do the following:

  • Double-check the said php with ls -l `which php`
  • Make sure that you only have one PHP version installed, this may cause mixing incompatible versions which may be the root cause of your problem

That should help you, finding the root cause.

Upvotes: 5

Charles Iams
Charles Iams

Reputation: 66

Just sharing here because I had this same issue and found this thread first while searching. For me I had a Windows server with PHP 5.6.? on it as well as PHP 7.2.? on it. I had configured IIS to use 7.2 but 5.6 was still in the environment variables under path. Open System Properties>Advanced tab> "Environment Variables...". Edit "path", and remove the reference to "C:/program files (x86)/PHP/v5.6" from path and save. Restart your terminal and you should be set. Hope that helps someone.

Upvotes: 0

Mooncake
Mooncake

Reputation: 1552

If you're using Debian based systems, you can ask it to globally use a specific version with the following command (depending on how and where your php versions are installed to):

sudo update-alternatives --set php /usr/bin/php7.2

update-alternatives creates, removes, maintains and displays informations about the symbolic links comprising the Debian alternatives system.

Upvotes: 9

Malek Zarkouna
Malek Zarkouna

Reputation: 988

Try this it worked for me :

  alias php='/usr/local/php7/bin/php'

php composer.phar install

Upvotes: 4

user10089632
user10089632

Reputation: 5570

try this:

composer install --ignore-platform-reqs

or this in composer.json

"config": {
    "preferred-install": "dist",
    "platform": {
        "php": "7.0.0"
    }
}

in the second solution basically you're faking a platform, and run composer.phar update after this

Upvotes: 28

Related Questions