How to Install WP-CLI on Linux for WordPress

Managing a WordPress website can be a time-consuming and complex task. From updating plugins and themes to creating backups and managing databases, many tasks must be performed regularly to keep your website running smoothly. This is where WP-CLI comes in.

WP-CLI is a command-line interface for WordPress that provides a convenient and efficient way to perform various tasks on your website. With WP-CLI, you can automate many of the tasks involved in maintaining a WordPress website, freeing up time to focus on other tasks.

In this article, we will examine the benefits and negatives of using WP-CLI and show you how to install WP-CLI on Linux. The guide will also explore popular use cases for WP-CLI so that you can see the potential of this powerful tool.

The Benefits of WP-CLI

WP-CLI has many benefits over traditional methods of managing a WordPress website. Some of the key benefits of WP-CLI include the following:

  • Speed: WP-CLI is faster than a web-based interface, providing quick access to the commands you need.
  • Automation: With WP-CLI, you can automate many of the repetitive tasks involved in maintaining a WordPress website, freeing up time for you to focus on other tasks.
  • Flexibility: WP-CLI provides a flexible and scalable solution for managing a WordPress website, and it can be customized to meet your specific needs.
  • Improved security: WP-CLI runs on the server and does not expose the backend of your website to the public internet, improving security.

The Negatives of WP-CLI

While WP-CLI provides many benefits, there are also some negatives to consider:

  • Learning curve: WP-CLI requires a certain level of technical knowledge, and it may take time for users to become familiar with the commands and syntax.
  • Risk of errors: WP-CLI provides a lot of power, but it also increases the risk of making mistakes that can cause problems for your website.
  • Server access: WP-CLI requires access to your server. It is not a suitable solution for users who do not have access to the server or are unfamiliar with server administration.

Method 1: Installing WP-CLI with a Package Manager

The easiest way to install WP-CLI on Linux is using a package manager, such as APT or DNF. This method is recommended for most users as it is simple.

Installing WP-CLI on Debian and Ubuntu

If you are running Ubuntu or Debian, you can install WP-CLI by using the following commands:

sudo apt install wp-cli

Installing WP-CLI on CentOS, Fedora, RHEL, Rocky Linux

If you are running CentOS, you can install WP-CLI by using the following commands:

sudo yum install wp-cli

Method 2: Installing WP-CLI from the Source Code

For the manual installation of WP-CLI, you may follow these steps to install from the source code.”

First, Download the latest version of WP-CLI from the official website: https://wp-cli.org/#install. Luckily, this is straight forward with the following command.

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

To confirm the proper operation of the WP-CLI executable, you may perform the following verification step.

php wp-cli.phar --info

Example output (Note PHP and OS Versions will be different on each machine):

verify wp-cli on linux

To ensure the proper functionality of WP-CLI, you will need to make the WP-CLI executable using the following terminal command.

chmod +x wp-cli.phar

To complete the installation, it is recommended to relocate the WP-CLI executable to the directory “/usr/local/bin/wp” for convenient usage.

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

To verify the successful installation of WP-CLI, you may use the following terminal command.

wp --info

Example output below:

wp-cli info output to confirm installation on linux

How to Update WP-CLI Core

Updating WP-CLI can be easily accomplished, and two options are available to suit different needs. The stable option provides a reliable and secure update, while the nightly option offers access to the latest developments and is suitable for those who are more adventurous.

The standard stable build of WP-CLI can be updated using the following terminal command.

wp cli update

Please note if the root user owns the file, you may need to use sudo privileges, as shown in the following example.

sudo wp cli update

To install the latest developments, known as the nightly build, you can use the following terminal command.

wp cli update --nightly

It is important to remember that the nightly build may contain bugs or unstable features and is intended for advanced users and developers comfortable working with the latest developments. Before proceeding with the installation, ensure that you understand the potential risks and are confident using the latest features.

WP-CLI Commands Examples

In this section, we will explore sixteen popular use cases for WP-CLI and explain each command. These use cases will demonstrate the versatility and efficiency of this powerful tool and how it can simplify the management of your WordPress website.

Use Case 1: Install WordPress

WP-CLI makes it easy to install WordPress on your server. You can run the following command to download and install the latest version of WordPress.

wp core download
wp core config --dbname=<database_name> --dbuser=<database_user> --dbpass=<database_password>
wp core install --url=<site_url> --title=<site_title> --admin_user=<admin_username> --admin_password=<admin_password> --admin_email=<admin_email>

Use Case 2: Create a New Post

WP-CLI allows you to create a new post on your website with a single command.

wp post create --post_type=post --post_title='<post_title>' --post_content='<post_content>'

Use Case 3: Update a Post

You can use WP-CLI to update existing posts on your website. For example, you can update the title and content of a post with the following commands.

wp post update <post_id> --post_title='<new_title>' --post_content='<new_content>'

Use Case 4: Delete a Post

WP-CLI provides an easy way to delete posts from your website. You can delete a post by its ID with the following command.

wp post delete <post_id>

Use Case 5: Create a New Page

WP-CLI allows you to create a new page on your website with a single command.

wp post create --post_type=page --post_title='<page_title>' --post_content='<page_content>'

Use Case 6: Update a Page

You can use WP-CLI to update existing pages on your website. For example, you can update the title and content of a page with the following commands.

wp post update <page_id> --post_title='<new_title>' --post_content='<new_content>'

Use Case 7: Delete a Page

WP-CLI provides an easy way to delete pages from your website. You can delete a page by its ID with the following command.

wp post delete <page_id>

Use Case 8: Install a Plugin

WP-CLI makes it easy to install plugins on your WordPress website. You can install a plugin by its slug with the following command.

wp plugin install <plugin_slug>

Use Case 9: Uninstall a Plugin

You can use WP-CLI to uninstall plugins from your WordPress website. You can uninstall a plugin by its slug with the following command.

wp plugin uninstall <plugin_slug>

Use Case 10: Activate a Plugin

WP-CLI provides a simple way to activate plugins on your WordPress website. You can activate a plugin by its slug with the following command.

wp plugin activate <plugin_slug>

Use Case 11: Deactivate a Plugin

You can use WP-CLI to deactivate plugins on your WordPress website. You can deactivate a plugin by its slug with the following command.

wp plugin deactivate <plugin-name>

Use Case 12: Create a New User

Using the following command, you can easily create a new user on your WordPress site using WP-CLI.

wp user create <username> <email> --role=<role>

For example, if you want to create a new user with the username “john” and email “john@example.com” with the role of “subscriber,” you would use the following command.

wp user create john john@example.com --role=subscriber

Use Case 13: Delete a User

Using the following command, you can delete a user from your WordPress site using WP-CLI.

wp user delete <user-id>

For example, if you want to delete a user with the ID of “10”, you would use the following command.

wp user delete 10

Use Case 14: Update a User’s Password

Using the following command, you can update a user’s password using WP-CLI.

wp user update <user-id> --user_pass=<new-password>

For example, if you want to update the password for the user with its ID of “10” to “newpassword,” you would use the following command.

wp user update 10 --user_pass=newpassword

Use Case 15: List All Users

Using the following command, you can list all users on your WordPress site using WP-CLI.

wp user list

Use Case 16: Import Posts from an XML File

Using the following command, you can import posts from an XML file into your WordPress site using WP-CLI.

wp import <xml-file> --authors=<comma-separated-list-of-usernames>

For example, if you want to import posts from the file “posts.xml” and assign the authors to “john” and “jane,” you would use the following command.

wp import posts.xml --authors=john,jane

Conclusion

WP-CLI is a powerful tool that provides a convenient and efficient way to manage your WordPress website. With its many benefits and popular use cases, WP-CLI is an essential tool for anyone who wants to manage their WordPress website more efficiently. In this article, we have shown you how to install WP-CLI on Linux and explored ten popular use cases for WP-CLI. Whether you are a beginner or an experienced WordPress user, WP-CLI is a tool that you should consider adding to your toolkit.

For more information on using WP-CLI, read the quick start guide.

Your Mastodon Instance
Share to...