Install WP-CLI without root or sudo Access via SSH on any Host

Whenever a host doesn’t have WP-CLI installed by default, I get sad 🙁 this powerful WordPress management tool isn’t available! Luckily it is easy to install WP-CLI without root or sudo access via SSH.

Following this method you will still be able to use WP-CLI just like you can when it is installed the more traditional way with root and placed in the /usr/bin or /usr/bin/local folder.

This post will show you how to install WP-CLI on almost any Linux distribution like CentOS, debian, Ubuntu etc as long as it has the php-cli package available. All you need is SSH access and a way to enter these commands like the Mac terminal or PuTTY on Windows.

Install WP-CLI without root or sudo Access

We need a place to store the WP-CLI phar file so let’s create a folder and enter it

mkdir -p /tmp/wp-cli
cd /tmp/wp-cli

Grab the latest WP-CLI phar and make it executable

wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O wp
chmod 755 wp

Or this way by downloading the WP-CLI phar straight into your temporary folder

mkdir -p /tmp/wp-cli
wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /tmp/wp-cli/wp
chmod 755 /tmp/wp

Or to any folder of your choice really

cd /yourfolder
wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /yourfolder/wp
chmod 755 /yourfolder/wp

To use the WP-CLI we downloaded, we will need to put ./ before the wp, we also need the path of the WordPress installation files.

First find out the full path of your WordPress installation, if you are already in the right directory, you can find this out with pwd command.

pwd

And you will get this output showing the full path, it should be used in the --path flag

/var/www/vhosts/wp-bullet.com/www

In this example I am in the /tmp/wp-cli folder and my WordPress installation is in /var/www/vhosts/wp-bullet.com/www

./wp option get siteurl --path=/var/www/vhosts/wp-bullet.com/www

And you’ll get some output like this

https://wp-bullet.com

It can be tedious to keep adding the --path so let’s make it so we can run WP-CLI just by typing just wp from any directory instead of having to use ./wp from the /tmp/wp-cli directory we created earlier.

To do this we can add the WP-CLI phar file wp’s folder to the Linux shell’s PATH variable so we don’t have to keep adding the --path flag.

If you are using a different temporary path, change /tmp/wp-cli to your path of choice.

export PATH="$PATH:/tmp/wp-cli"

Now you can use WP-CLI as if it were installed in the common /usr/bin or /usr/bin/local.

Here is a snippet you can copy and paste to install WP-CLI and add the PATH for you so you can use it anywhere!

mkdir -p /tmp/wp-cli
wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /tmp/wp-cli/wp
chmod 755 /tmp/wp-cli/wp
export PATH="$PATH:/tmp/wp-cli"

When you are done you can clean up afterwards too.

rm -r /tmp/wp-cli

Enjoy WP-CLI on the host that didn’t provide it 😉

Sources

WP-CLI Handbook
Setting PATH on Linux