Using WP-CLI in XAMPP for Linux

WP-CLI or WordPress Command Line Interface is used to manage sites without the need of accessing the website through a browser. Here is a quick instructions on how to install WP-CLI in XAMPP for GNU/Linux.

 

Getting Started

Download WP-CLI, you can use curl, or wget commands to download the wp-cli.phar file.

$ 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

Next, is to check whether the downloaded file works or not, by running the following command:

$ php wp-cli.phar --info

You’ll notice the following output:

$ php wp-cli.phar --info
The program 'php' can be found in the following packages:
 * php7.0-cli
 * hhvm
Try: sudo apt install <selected package>

This is due to the missing PHP package, XAMPP is used to ease the installing of such packages for a quick development server setup, installing PHP package isn’t an ideal solution, as XAMPP already includes it. What you need to do is define the PATH variable.

Defining PATH Variable

When you type the following in your shell, you’ll notice your existing path from which the executable files are called:

$ echo $PATH

PATH variable lists all the paths of the executable directory from where shell will try to execute a particular command.

To add the XAMPPs PHP directory, you’ll need to define the following rules in the .profile file located in your home directory. The rules will be as follows:

# Define path for XAMPP
XAMPP_PATH=/opt/lampp/bin:/opt/lampp/sbin
export PATH="$XAMPP_PATH:$PATH"

Once saved, run the source command to read, and execute the new PATH variable set in the .profile file.

$ source ~/.profile

Re-run the check to verify that the wp-cli.phar file is working, or not.

$ php wp-cli.phar --info

The output will be as follows:

PHP binary:	/opt/lampp/bin/php-7.1.7
PHP version:	7.1.7
php.ini used:	/opt/lampp/etc/php.ini
WP-CLI root dir:	phar://wp-cli.phar
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/home/nitin/Desktop
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	1.2.1

Executing using WP command

In order to run the wp-cli.phar globally, the file should be executable, and located inside the /usr/bin folder, use the following commands as follows:

$ chmod +x wp-cli.phar
$ mv wp-cli.phar /usr/bin/wp

Once done, execute wp-cli.phar using wp as the default command:

$ wp --info
PHP binary:	/opt/lampp/bin/php-7.1.7
PHP version:	7.1.7
php.ini used:	/opt/lampp/etc/php.ini
WP-CLI root dir:	phar://wp-cli.phar
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/home/nitin
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	1.2.1

You can now navigate to WordPress root folder, and run WP-CLI:

$ wp theme list
+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentyfifteen   | inactive | none   | 1.8     |
| twentyseventeen | active   | none   | 1.3     |
| twentysixteen   | inactive | none   | 1.3     |
+-----------------+----------+--------+---------+

For Tab completion, download wp-completion.bash, and add the following line in your .profile file, located in the home folder:

source /FULL/PATH/TO/wp-completion.bash

Make sure to source the .profile file, so that the changes get reflected:

$ source ~/.profile

You have now successfully configured, and good to manage your WordPress installations via your terminal using WP-CLI.

Leave a Reply