WP-CLI is an incredibly powerful tool that allows you to manage your WordPress site from the command line. This tool simplifies many tasks, such as listing plugins, running cron jobs, or updating WordPress, WooCommerce, plugins and themes. If your hosting provider doesn’t include WP-CLI by default or it’s not up-to-date but you have SSH access, you can install it yourself with a few straightforward steps.
You can follow these steps even if you don’t have root (admin) access to your server.
Requirements:
- PHP CLI
- Linux Server (Ubuntu)
- SSH access
- Basic familiarity with the command line
Step 1: Check the $PATH Environment Variable
First, it’s essential to understand the value of the $PATH environment variable. This variable tells your system where to look for executable files. If you place a program in a directory listed in your $PATH, you can run it from anywhere just by typing its name.
To view your $PATH, use the following command:
echo $PATH
You’ll see a list of directories separated by colons, something like this:
/home/__YOUR_USER__/.local/bin:/home/__YOUR_USER__/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/cpanel/composer/bin
Here, __YOUR_USER__
is your system’s username.
Step 2: Create a Bin Directory (if it doesn’t exist)
Choose a directory within your $PATH to place the WP-CLI executable. A common choice is $HOME/bin
. Create this directory if it doesn’t already exist:
mkdir -p $HOME/bin
Step 3: Download WP-CLI
Now, download the latest version of WP-CLI directly into your chosen directory. Use the curl
command as follows:
curl --output $HOME/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Step 4: Set Executable Permissions
For WP-CLI to run, you need to make the file executable. Use the chmod
command:
chmod 0755 $HOME/bin/wp
Step 5: Verify the Installation
To ensure WP-CLI is installed correctly, check its version:
wp --info
If everything is set up correctly, you’ll see output detailing your WP-CLI configuration, similar to this:
OS: Linux 4.18.0-477.21.1.el8_8.x86_64 #1 SMP Thu Aug 10 13:51:50 EDT 2023 x86_64
Shell: /bin/bash
PHP binary: /opt/cpanel/ea-php81/root/usr/bin/php
PHP version: 8.1.29
php.ini used: /opt/cpanel/ea-php81/root/etc/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 15.1 Distrib 10.6.18-MariaDB, for Linux (x86_64) using readline 5.1
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/__YOUR_USER__/public_html
WP-CLI packages dir:
WP-CLI cache dir: /home/__YOUR_USER__/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.10.0
Step 6: Test WP-CLI with Your WordPress Site
Navigate to your WordPress installation directory where wp-config.php
is located and check the site’s URL:
wp option get home
This should return your site’s URL, confirming that WP-CLI is working correctly.
Troubleshooting
If you encounter issues running WP-CLI, you might need to specify the full path to the PHP binary:
/usr/bin/php $HOME/bin/wp --info
You can check which PHP version your server is using with:
which php
This should return the following output
/usr/bin/php
For servers with multiple PHP versions, you can view the installed php versions. It seems you can run this as a regular user.
update-alternatives --display php
php - auto mode
link best version is /usr/bin/php.default
link currently points to /usr/bin/php.default
link php is /usr/bin/php
slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php.default - priority 100
slave php.1.gz: /usr/share/man/man1/php.default.1.gz
/usr/bin/php8.2 - priority 82
slave php.1.gz: /usr/share/man/man1/php8.2.1.gz
/usr/bin/php8.3 - priority 83
slave php.1.gz: /usr/share/man/man1/php8.3.1.gz
So technically you can load wp-cli from a different php version.
/usr/bin/php8.2 $HOME/bin/wp --info
By following these steps, you can install and configure WP-CLI on your Linux server, enabling efficient command-line management of your WordPress site.