Tutorial

How to Install and Use WP CLI on Linux

Learn to install WP-CLI on Linux. Also learn to enable bash completion and use the basic WordPress commands in this tutorial.

Abhishek Prakash
Abhishek Prakash

Table of Contents

WordPress is one of the most widely used open-source CMS software on the internet. It’s easy to use and even a non-coder can easily set it up and run a website on it.

This doesn’t mean that WordPress is not for developers. WordPress provides a range of tools and documents that help the developers.

One of such tools is WP-CLI, the command line interface for WordPress. With the WP-CLI tools, you can install WordPress or manage and update an existing WordPress installation. You can configure multisite, you can update WordPress core and plugins in the backend server thanks to this command line tool.

The WP-CLI is helpful in debugging issues and managing a WordPress install specially in the case when the WordPress frontend doesn’t work.

You can find a number of reasons to use it. I am going to show you how to install WP-CLI on Ubuntu and other Linux distributions and how to use the basic commands for managing your WordPress install.

Install WP-CLI on Ubuntu and other Linux distributions

Wp Cli Linux

I am using Ubuntu 18.04 LTS server hosted on UpCloud. You can get $50 free credits if you use this link for registering for UpCloud servers.

I am using ServerPilot for hosting multiple WordPress install on a single server. If you want to install multiple WordPress on one cloud server, configuration could be complicated. ServerPilot solves this problem and you can install as many WordPress instances as you want in a matter of a few clicks.

Whichever Linux distribution you are using, the installation steps remain the same for all of them.

Step 1: Download the WP-CLI archive file

The WP-CLI is available as .phar (PHP Archive) file. You can download this archive file using Wget or Curl command. I am using wget command here.

wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

You should see an output similar to this:

wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
 --2019-07-14 12:29:16--  https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
 Resolving raw.githubusercontent.com (raw.githubusercontent.com)… 151.101.112.133
 Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.112.133|:443… connected.
 HTTP request sent, awaiting response… 200 OK
 Length: 5506663 (5.3M) [application/octet-stream]
 Saving to: 'wp-cli.phar'
wp-cli.phar                                        100%[================================================================================================================>]   5.25M  --.-KB/s    in 0.1s  2019-07-14 12:29:17 (38.5 MB/s) - 'wp-cli.phar' saved [5506663/5506663]

Step 2: Make the WP-CLI executable

Now that you have download the archive file, you should make this file executable by changing its file permission. You can use the chmod command:

chmod u+x wp-cli.phar

The next step is to move this file into the /user/local/bin. In the Linux directory structure, the bin directory is used for keeping the binaries of the commands you run.

sudo mv wp-cli.phar /usr/local/bin/wp

Step 3: Verify that WordPress CLI is working fine

Now that you ‘changed’ the archive file into a command, you should verify that it’s working fine. The easiest way is to check the version of the WP-CLI tool. Use the command below:

wp --info

If the WP-CLI is ‘installed’ properly, the output should be similar to this:

OS:    Linux 4.15.0-32-generic #35-Ubuntu SMP Fri Aug 10 17:58:07 UTC 2018 x86_64
Shell:    /bin/bash
PHP binary:    /opt/sp/php7.3/bin/php
PHP version:    7.3.7
php.ini used:    /etc/php7.3-sp/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:    /srv/users/abhi
WP-CLI packages dir:    
WP-CLI global config:    
WP-CLI project config:    
WP-CLI version:    2.2.0

Activate bash completion for WP-CLI [Optional]

The bash completion feature allows you to auto-complete commands by hitting tab key. This is one of the must know terminal shortcut for any Linux user.

You’ll have to do a little effort to activate bash completion feature for WordPress CLI.

Go to your home directory using the cd command:

cd ~

Now get the bash completion script:

wget https://github.com/wp-cli/wp-cli/raw/master/utils/wp-completion.bash

Your terminal should have a hidden .bashrc file in your home directory. If not, you can create it. What you have to do here is to add the content of the wp-completion.bash file into the .bashrc file. I am going to use the cat command here but you can whatever method you prefer to edit the file.

cat wp-completion.bash >> .bashrc

Now source the .bashrc file so that the changes are taken into effect immediately.

source .bashrc

That’s it. Now the wp commands can be auto-completed with the tab key.

Basic WordPress commands you should know

Now that you installed WordPress CLI, it’s time to use it to manage your WordPress install.

To run any command with WP CLI, you must be in the public directory of your WordPress instance installed.

Check the WordPress version

To check the version of your WordPress install, use this command:

wp core version

Check if any update is available for WordPress core

To check if any WordPress core update is available, use the following command:

wp core check-update

If there is update available, the output will notify you of that:

wp core check-update
 +---------+-------------+-------------------------------------------------------------+
 | version | update_type | package_url                                                 |
 +---------+-------------+-------------------------------------------------------------+
 | 5.2.2   | major       | https://downloads.wordpress.org/release/wordpress-5.2.2.zip |
 +---------+-------------+-------------------------------------------------------------+

If your WordPress core is the latest version, you should see this message:

Success: WordPress is at the latest version.

Update WordPress via command line

If there is a core update available, you can update the WordPress install using the following command:

wp core update

The output displays the process:

Updating to version 5.2.2 (en_US)…
Downloading update from https://downloads.wordpress.org/release/wordpress-5.2.2-no-content.zip…
Unpacking the update…
Success: WordPress updated successfully.

Clear WordPress cache in command line

A number of WordPress issues are caused by cache. Clearing the cache could ‘fix’ them. You can flush the cache using this command:

wp cache flush

Manage WordPress plugins from command line

If you have used WordPress long enough, you know that plugin conflicts often cause trouble. One of the way to handle this issue is to disable the troublesome plugin. If you don’t know which plugin is causing the issue, deactivating all of them and then reactivating them one by one is the way to go.

You can list all the plugins using this command:

wp plugin list

The output is in tabular form:

+--------------------+----------+--------+---------+
 | name               | status   | update | version |
 +--------------------+----------+--------+---------+
 | akismet            | active   | none   | 4.1.2   |
 | atomic-blocks      | active   | none   | 2.0     |
 | members            | active   | none   | 2.1.0   |
 | wp-seopress        | active   | none   | 3.5.8   |
 | wp-seopress-pro    | active   | none   | 3.5.8   |
 | wpforms-lite       | active   | none   | 1.5.3.1 |
 | wp-rocket          | inactive | none   | 3.3.5.2 |
 | advanced-cache.php | dropin   | none   |         |
 +--------------------+----------+--------+---------+

You can deactivate a plugin by using its name. You can see the name in the plugin list command used above.

wp plugin deactivate plugin_name

You can deactivate all plugins in one single command:

wp plugin deactivate --all

You can activate the plugins the same way. Just use activate instead of deactivate in the above commands.

You can also update individual plugins or all of them together:

wp plugin update --all

More WordPress commands

There are more commands you can use in WP-CLI. You can see the list of plugins with wp plugin list, list of themes with wp theme list. You can install and delete plugins and themes as well. You can manage users and their passwords via WP commands.

It won’t be possible for me to cover all the WP commands. I suggest you refer to the WordPress CLI handbook for finding out the commands you may need.

I hope this tutorial was helpful to you in installing WP-CLI on Linux and you have some basic idea about using the WordPress in command line.

If you have questions or suggestions, please leave a comment below.



Join the conversation.