How to make WP-CLI use Laravel Valet version specified via the isolate command

By default wp-cli on your Mac will use the system’s PHP version located in /usr/bin/env, however as of Valet 3.0 you an use the isolate command to make specific site using specific PHP version as follows:

cd ~/Sites/your-website
valet isolate [email protected]

This will make your-website.test to use PHP 8.0.x, however instead of calling PHP directly in the command line, you will have to use valet in front, as follows:

valet php composer install
# instead of 
php composer install

Now here comes the main part, how to install WP-CLI in a way that it will use the current Valet PHP version with respect to the isolate command. This is important because if you use composer on your site you will want to have the same PHP version everywhere.

1. Install WP-CLI

curl -O
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

If using ZSH, then add wp command to .zshrc

echo "alias wp='valet php /usr/local/bin/wp'" >> ~/.zshrc
source ~/.zshrc

If using BASH, then add wp command to .bashrc

echo "alias wp='valet php /usr/local/bin/wp'" >> ~/.bashrc
source ~/.bashrc

That’s it, now you have isolated wp cli command available.

2. Using WP-CLI

To verify the above steps, simply go to your-website folder and check if the wp-cli output version matches the version you set via the isolate command

cd ~/Sites/your-website
wp --info

Then the PHP Version output should match PHP 8.0.x that we set with isolate before.

As i said above, having WP-CLI and your website versions same is important for proper development. Imagine if you are using composer in your plugins or themes that uses the systems PHP binaries and your site uses valets PHP binaries, you will end up with a messy installation and unpredictable errors.

Related posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Secured By miniOrange