Install and Configure WP-CLI

WP-CLI or WordPress Command Line Interface is a suite of commands that can make quick work out of updating plugins, setting up multisites, and much more WordPress management tasks, right from the Command Line Interface (CLI).

NOTE: InMotion Hosting’s WordPress Hosting includes WP-CLI and is already installed. If you are already familiar with working in a Command Line Interface (CLI), check out our WP-CLI Education Channel to learn about the commands available using WP-CLI.

Prerequisites for WP-CLI

Before we dive into installing WP-CLI, let’s make sure you’ve got everything you need to get started:

  • PHP version: First up, you’ll need PHP installed on your server, as WP-CLI is built to work with WordPress, which runs on PHP. Specifically, you’ll want PHP 7.4 or higher – this keeps things compatible and secure. If you’re unsure about your PHP version, a quick check with your hosting provider or a command like php -v in your terminal will clear things up.
  • SSH access: You’ll need SSH access to your server to run WP-CLI commands. Most hosting providers, like InMotion Hosting, offer this—double-check with your host to ensure you’ve got the credentials handy.
  • WordPress installation: Another key requirement is WordPress itself. WP-CLI is designed to manage WordPress sites, so you’ll need at least one WordPress installation ready to go. WP-CLI works with WordPress 3.7 or higher, but we recommend using a recent version for security and performance reasons.
  • cURL and wget: These tools are usually pre-installed on most servers, but WP-CLI may use them for downloading files. Run curl– version or wget– version to confirm they’re available.
  • Sufficient permissions: Make sure your user account has the necessary permissions to execute commands and modify WordPress files on the server.

Install WordPress WP-CLI

NOTE: The following instructions require root access available on a VPS or Dedicated Server.
  1. SSH into your server as the root user.
  2. You will need to download wp-cli.phar using either wget or curl.

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


    or


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



    NOTE: Using wget, if you get an error about the SSL certificate mis-match between www.github.com and raw.githubusercontent.com domains, you can skip the certificate check to download the file with:


    wget –no-check-certificate https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar



  3. Then test WP-CLI directly with PHP with this command:

    php wp-cli.phar –info


    You should see output similar to:


    PHP binary:  /opt/cpanel/ea-php70/root/usr/bin/php PHP version:  7.0.26 php.ini used: /opt/cpanel/ea-php70/root/etc/php.ini WP-CLI root dir:  phar://wp-cli.phar WP-CLI packages dir:   WP-CLI global config:  WP-CLI project config:   WP-CLI version: 0.24.1

  4. Next you’ll need to make the WP-CLI file executable and move it to your path:

    chmod +x wp-cli.phar


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


  5. Now you can simply use wp at the command line to access WP-CLI:

    wp –info


Set up Bash Tab-to-Complete for WP-CLI

A great time-saving feature of command line interfaces is using tab completion to quickly type in commands. You can add the ability to use tab completion for WP-CLI by setting it up in your local ~/.bash_profile file.

  1. SSH into your server as the user that will be using WP-CLI.
  2. Download the WP-CLI tab completion file to your server.

    curl -O https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash


    or


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


    NOTE: While using wget, if you get an error about the SSL certificate mis-match between www.github.com and raw.githubusercontent.com domains, you can skip the certificate check to download the file with:

    wget –no-check-certificate https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash



  3. Move the WP-CLI tab completion file to your home directory:

    mv wp-completion.bash ~/


  4. Now edit your ~/.bash_profile file to include the WP-CLI tab completion file:

    vi ~/.bash_profile


  5. Add this line to the bottom of your ~/.bash_profile file:

    source ~/wp-completion.bash


    Now save this file.
    To verify that WP-CLI is installed correctly, you can run the command: wp –info. This will display environment-specific information about WP-CLI, including the version, PHP version, WordPress version, and system details. 
    Example of WP-CLI wp --info Output:

    OS: Linux 5.15.0-105-generic #115-Ubuntu SMP x86_64
    Shell: /bin/bash
    PHP binary: /usr/bin/php
    PHP version: 8.1.2
    php.ini used: /etc/php/8.1/cli/php.ini
    WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
    WP-CLI version: 2.9.0

  6. Back at the command line, make sure to source your ~/.bash_profile file by running this command:

    source ~/.bash_profile


  7. Now just type wp at the prompt, and hit Tab twice to see all the tab completions you just set up:
    exampl3@server [/home/exampl3/public_html]# wp
    cache  comment  eval  help  menu  plugin  role  shell  super-admin  transient
    cap  core  eval-file  import  network  post  scaffold  sidebar  term  user  cli
    db  export  media  option  rewrite  search-replace  site  theme  widget

Now that you have installed WP-CLI you can begin using the command line to manage your WordPress website(s).

Configuring Your Command-line Interface

If you’re regularly working with WordPress via the command line, you’ve probably found yourself typing the same flags or options over and over. It can get tedious, and that’s where the WP-CLI configuration file, wp-cli.yml, comes in.

What is wp-cli.yml?

The wp-cli.yml file is a simple YAML configuration file that lets you define default options and settings for your WP-CLI commands. Instead of repeating the same parameters, you set them once and forget them. WP-CLI reads this file automatically. Thus, it saves you time and reduces human error.

Why Use a Configuration File?

Imagine you’re always running wp core update with –path=/var/www/html/mysite. Instead of typing that every single time, you can store it in your wp-cli.yml file like this:

  • path: /var/www/html/mysite

Now, every WP-CLI command you run from that directory will default to that path—no extra typing needed.

How to create a wp-cli.yml file?

  • Choose a location: WP-CLI supports multiple levels of config files, so you can create one in:
    • The current working directory (for project-specific settings)
    • Your home directory (~/.wp-cli/config.yml for global settings)
    • Inside the WordPress root directory
  • Create the file: Use any text editor to create a file called wp-cli.yml. Here’s a basic example:
    • path: /var/www/html/mysite
    • url: https://mysite.com
    • user: admin
  • Save it and test: Once saved, WP-CLI will automatically read the file. Try running a command like wp plugin list without specifying the path or URL – it’ll just work.

Useful Command Tips

Tired of digging through documentation or typing long commands? Here’s a list of the most essential WP-CLI commands every WordPress developer should have in their toolkit.

  • Core management:
    • wp core version – quickly see installed WP version
    • wp core update – update WordPress core to latest
    • wp core install – install WP with URL, title, admin creds
    • wp core verify-checksums – check if core files were modified
  • Plugin/theme management:
    • wp plugin list – view all plugins and their status
    • wp plugin install plugin-slug –activate – install & activate in one go
    • wp plugin update –all – update every plugin at once
    • wp theme install theme-slug –activate – install and activate a theme
    • wp theme update –all – update all themes
  • User management:
    • wp user list – list all users with roles and IDs
    • wp user create username [email protected] –role=editor – add a new user
    • wp user update 2 –user_pass=newpassword – reset user password
  • Database operations:
    • wp db export – quick backup of your database
    • wp db import file.sql – restore from a SQL file
    • wp db query “SELECT * FROM wp_users;” – run raw SQL
    • wp search-replace ‘oldurl.com’ ‘newurl.com’ – find & replace across DB

Updating WP-CLI 

Update WP-CLI by running wp cli update in your terminal. Ensure you have proper permissions or use sudo if needed. 

Troubleshooting WP-CLI

1. Permission Issues in Shared Hosting

Shared hosting environments often impose strict permissions, which can cause WP-CLI to fail. Common issues include a lack of write access to WordPress directories or restricted shell access.

Solution

  • Ensure the WordPress directory has the correct file permissions (755 for directories, 644 for files). If necessary, adjust them using FTP or a file manager.
  • The user running WP-CLI should have the same ownership as the WordPress files. If not, contact your hosting provider to correct ownership or grant necessary permissions.
  • Some shared hosting environments restrict shell access. If needed, request SSH access from your hosting provider to use WP-CLI effectively.

2. PHP Version Compatibility Problems

WordPress and WP-CLI require a minimum PHP version to work correctly. Sometimes, your host may run an outdated PHP version, which causes compatibility problems with WordPress or WP-CLI commands.

Solution

  • Run php -v from the command line to verify your PHP version. WordPress typically requires PHP 7.4 or higher.
  • If you have control over your hosting environment (e.g., VPS), upgrade your PHP version. For shared hosting, contact your provider to request an upgrade to the necessary PHP version.
  • WP-CLI also requires a minimum PHP version. If needed, update WP-CLI using the command wp cli update.

3. Working with WP-CLI When You Don’t Have Root Access

If you are using a shared hosting environment, you may not have root access to install or configure WP-CLI globally. This can limit your ability to use certain WP-CLI features.

Solution

  • You can install WP-CLI locally within your WordPress directory by downloading the wp-cli.phar file. To do this:
    • Download the WP-CLI PHAR file:
      curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    • Make it executable:
      chmod +x wp-cli.phar
    • Move it to a directory included in your system’s PATH:
      sudo mv wp-cli.phar /usr/local/bin/wp
    • If you can’t use sudo, simply run it as php wp-cli.phar from within your WordPress directory.
  • If you’re working on a shared host and don’t have root access, create custom shell scripts to automate your WP-CLI tasks. Just make sure you have access to execute PHP scripts and that the correct permissions are set.

Learn more about WP-CLI, the command-line interface for WordPress, and how to streamline your WordPress maintenance workflows!

Advanced Use for Command Line Interface and WordPress

WordPress CLI (WP-CLI) is an incredibly powerful tool for developers who want to streamline their workflow, automate repetitive tasks, and integrate with various systems. Here’s an advanced look at using WP-CLI

  • Integration with development workflows: WP-CLI integrates into CI/CD pipelines for automated testing, deployment, and site management, speeding up the development lifecycle.
  • Using WP-CLI with version control: Combine WP-CLI with Git to manage theme/plugin updates and configurations, allowing for streamlined version control and collaboration.
  • Performance optimizations for large sites: Use WP-CLI to manage large datasets, clear caches, optimize databases, and batch processing tasks, improving speed.
  • Scheduled tasks and automation: WP-CLI can automate tasks like backups and updates using cron jobs for recurring maintenance.
  • Multisite management commands: WP-CLI simplifies multisite network management by enabling commands for creating, updating, and managing multiple sites at once.
IC
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

7 thoughts on “Install and Configure WP-CLI

  1. I’m getting a total diff output when doing:

    $php wp-cli.phar –info

    dyld: Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
    Referenced from: /usr/local/bin/php
    Reason: image not found
    Abort trap: 6

    …going to try locating the issue

    1. Hello and thanks for contacting us. I recommend you check your bash history with the “history” command to ensure you followed the steps correctly. You may learn more from error logs as well.

  2. This is great. Never knew it existed. Is there any compelling reason NOT to put “wp plugin update –all” into a cron job to run daily?

     

    1. I would not recommend updating plugins daily. The problem exists in conflicts and upgrades. You want to be able to upgrade the plugins and immediately test to ensure everything in functioning properly. Also, prior to upgrading it’s bests practices to backup your site. That way if anything goes wrong in the upgrade, you can restore it accordingly. It really comes down to personal preferences.

  3. Managing multiple WordPress websites is a hectic job, as you have to logging to each site and setup/install plugins and settings for each site independently. 

    Luckily for those well versed to work in command line, they can use WP-CLI to manage multiple WordPress sites with just a few commands. WP-CLI comes with built-in commands specific just to manage WordPress websites. 

    Through WP-CLI you can install, manage plugins and themes. Control settings such as cron-job, comments etc. 

  4. # wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    --2014-06-25 14:52:09-- https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    Resolving raw.githubusercontent.com... 23.235.40.133
    Connecting to raw.githubusercontent.com|23.235.40.133|:443... connected.
    ERROR: certificate common name `www.github.com' doesn't match requested host name `raw.githubusercontent.com'.
    To connect to raw.githubusercontent.com insecurely, use `--no-check-certificate'.
    Unable to establish SSL connection.
    1. Hello Robert,

      It seems like GitHub is using the SSL certificate for www.github.com also on their raw.githubusercontent.com domain, and wget is seeing this mis-match and warning you much like a web-browser would.

      You can bypass this in wget as the error message says by using –no-check-certificate:

      wget –no-check-certificate https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

      Or you could also use curl which doesn’t check the certificate in this way:

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

      Please let us know if you’re still having any issues, and thanks for letting us know about this problem so we can update this guide.

      – Jacob

Comments are closed.