WP-CLI is a great framework for anyone who uses WordPress and is familiar with the Linux command line. As the name suggests, WP-CLI gives you a command-line interface for WordPress. Just about every admin task that can be performed via the WordPress dashboard can be done right from the command line.

This article covers how to install and configure WP-CLI. If you already have installed the utility then you can skip to one of our other WP-CLI articles:

Installation

For this article we will install WP-CLI for the domain is example.com. The site lives on our cpweb6 server, which is one of our Premium servers. That means that everything we need to install WP-CLI is available: we got SSH access and PHP’s Phar extension is enabled out of the box.

There are a couple of things we need to do to install WP-CLI:

  • Download the wp-cli.phar file
  • Store the file in a directory for executable files
  • Rename the file to wp
  • Make the file executable

Not all these steps are required. For instance, you don’t need to rename the file to wp. However, it is common practice to do so, and all example commands you find in the excellent WP-CLI handbook use wp as the name of the executable.

Where do executables live?

On Linux, binary files and scripts are typically stored in specific directories. These directories are defined in an environment variable named $PATH.

For example, many common utilities are kept in the /usr/bin directory:

$ type awk
awk is /usr/bin/awk

The output of the type command shows that the Awk utility’s binary is /usr/bin/awk. To run an Awk command you don’t need to enter the full path to the binary though. Instead of /usr/bin/awk --version you can simply run awk --version:

$ awk --version
GNU Awk 4.0.2

The reason you don’t have to enter the full path to the binary is that the /usr/bin directory is in your $PATH variable. To view your $PATH variable you can simply run echo $PATH. The output is not very readable (it is a long colon-separated string), but we can use a little trick to print each path on its own line:

$ echo "${PATH//:/$'\n'}"
/usr/local/cpanel/3rdparty/lib/path-bin
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
/opt/cpanel/composer/bin
/home/example/.local/bin
/home/example/bin

As you can see, /usr/bin is in our path. We also got two directories for our example user: /home/example/.local/bin and /home/example/bin. This is where we want to store the WP-CLI variable.

Download, rename, make executable

It is likely that the /home/example/.local/bin and /home/example/bin directories don’t yet exist. If so, you first need to create either directory:

[example@cpweb6-premium ~]$ mkdir ~/bin

Next, we change to the bin directory and download the wp-cli.phar file. We are using curl with the -o option to rename the file to wp at the same time.

[example@cpweb6-premium ~]$ cd ~/bin
[example@cpweb6-premium bin]$ curl -o wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

And now we just have to make the wp file executable:

[example@cpweb6-premium bin]$ chmod +x wp

The first test

Because we saved the file to a directory that is in our path and made the file executable we can now run the command wp from any directory. To check if everything is working correctly you can navigate to the public_html directory and execute the command wp --info:

[example@cpweb6-premium bin]$ $ cd ; pwd
/home/example

[example@cpweb6-premium ~]$ wp --info
OS:	Linux 3.10.0-962.3.2.lve1.5.28.el7.x86_64 #1 SMP Tue Jan 28 04:53:14 EST 2020 x86_64
Shell:	/bin/bash
PHP binary:	/opt/cpanel/ea-php72/root/usr/bin/php.cagefs
PHP version:	7.2.27
php.ini used:	/opt/cpanel/ea-php72/root/etc/php.ini
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/example
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.4.0

The wp --info argument outputs basic information about the server, shell, PHP and WP-CLI.

As an aside, you can also run the wp without any subcommands or arguments. This will output a long list of available subcommands.

Adding tab completion

Every proper shell supports tab completion, and you can download a small script that will make tab completion work for WP-CLI. If you don’t know what tab completion is, it allows you to let the shell complete a command when you hit the Tab key. For instance, if you want to run the command wp plugin list you can start typing wp pl and press the Tab key – it will then automatically complete pl to plugin. When a string can’t be completed because there is more than one possible match you can hit the Tab key a second time, and the shell will show you all available options.

We will download the script to our home directory (as we don’t really want it in the public_html directory):

[example@cpweb6-premium]$ cd ~ && wget https://raw.githubusercontent.com/wp-cli/wp-cli/v2.4.0/utils/wp-completion.bash

Next, we need to tell Bash to “source” the file we downloaded. That way, the tab completion script will be available every time a new session starts.

echo "source /home/$USER/wp-completion.bash" >> ~/.bash_profile 

And, finally, we need to reload our .bash_profile file:

[example@cpweb6-premium]$ source ~/.bash_profile

Updating WP-CLI

WP-CLI is free and open source software, and it is actively developed. That means that there will be regular updates that fix bugs and improve WP-CLI’s functionality. It is important to update WP-CLI regularly, and of course you can do so via WP-CLI itself:

$ wp cli update
Success: WP-CLI is at the latest version.

Where to go from here?

You can now start using WP-CLI. If you are new to WP-CLI then the articles about installing WordPress and managing WordPress options and plugins/themes will make you more familiar with the utility. And if that information is a little too basic for your liking then we recommend bookmarking the official WP-CLI Commands page.