16

On a development server I have a co-install of PHP 5.6 & 7; nginx is configured with PHP 5.6. When I type "wp" it returns several errors and at the end of the error is a message containing the following:

Your PHP installation appears to be missing the MySQL extension which is required by WordPress.

Typing wp --info returns:

PHP binary: /usr/bin/php7.0
PHP version: 7.0.10-2+deb.sury.org~precise+1
php.ini used: /etc/php/7.0/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI packages dir:
WP-CLI global config: /srv/www/wp-cli.yml
WP-CLI project config:
WP-CLI version: 0.26.0-alpha-5672b63

WP-CLI seems to be defaulting to PHP 7, I would prefer it to use PHP 5.6.

So I was wondering if there was an option I could add to the configuration yml file to select which PHP version to use?

If you need any further information, please let me know

2
  • By the time WP CLI loads the yml file, it's already running inside PHP, the answer to this will depend on your environment, OS, and how PHP is installed, take a look at stuff like PHPBrew etc
    – Tom J Nowell
    Commented Oct 27, 2016 at 15:03
  • As an aside, you should have mysqli installed, such an extension is available for PHP7 too, although the mysql extension is unsupported and should be avoided. Also, always work from the first error/warning down, the 2nd error may only occur when the 1st one occurs
    – Tom J Nowell
    Commented Oct 27, 2016 at 15:04

4 Answers 4

18

On a system where

  • you can't change the /usr/bin/php symlink
  • you can't change the PATH to point to a different version (because the php executables don't reside in distinct /lib/ directories)
  • WP_CLI_PHP has no effect

(like my Arch Linux with php(8), php7 installed from extra and wp-cli installed from AUR. I'm using php7 vs php8 here, but this should work for any versions.)

…a workaround may be to call the wp phar executable with php7 cli:

whereis wp
# /usr/bin/wp

php7 /usr/bin/wp cli info
# PHP binary:     /usr/bin/php7
# PHP version:    7.4.25

for convenience you can add a bash alias in your .bashrc:

alias wp-php7='php7 /usr/bin/wp'
# or override wp altogether
alias wp='php7 /usr/bin/wp'
2
  • a brilliant solution
    – Abilogos
    Commented May 5, 2022 at 13:16
  • this is actually best solution in my usecase.. I've had virtual users (no home root could be writen any file) - Thanks @kubi Commented Nov 7, 2023 at 22:27
13

Got the same problem! Just switch the php version. On my server PHP5.6 was default for apache, while CLI was configured with PHP7.1. After installing WP-CLI, with wp --info I got this result:

PHP binary: /usr/bin/php7.1
PHP version: 7.1.5-1+deb.sury.org~xenial+1
php.ini used: /etc/php/7.1/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar

And when i used the wp core install command i got the error: Your PHP installation appears to be missing the MySQL extension which is required by WordPress. The problem is just the mix between the different versions: we have just to switch completely to 5.6 or 7.1. In my case problem was solved simply by writing on the shell:

sudo update-alternatives --set php /usr/bin/php5.6

And then wp --info

PHP binary: /usr/bin/php5.6
PHP version: 5.6.30-10+deb.sury.org~xenial+2
php.ini used: /etc/php/5.6/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar

Problem solved! WP-CLI worked like a charm.

10

You can set the php binary that WP-CLI uses by setting an environment variable in your linux shell.

export WP_CLI_PHP=/path/to/php5.6
1
  • 4
    According to the documentation this only works if you've installed wp-cli via git or composer. I wasn't able to get it to work with wp-cli installed via yum on CentOS.
    – cstrouse
    Commented Mar 31, 2020 at 22:16
1

Sounds like you need to change your default PHP version.

I assume php -v returns 7?

You'll need to change the PATH.

See this: https://stackoverflow.com/questions/31206864/use-different-php-version-cli-executable-for-one-command

Or this: https://make.wordpress.org/cli/handbook/installing/#using-a-custom-php-binary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.