As you become more and more experienced with WordPress, you’ll find yourself repeating the same actions over time. Perhaps the number of installations under your management will increase and you find yourself with an additional workload that is repetitive and boring. The WordPress graphical user interface or GUI is excellent from an intuitive point of view. It’s easy to figure out exactly what you want to do. But it’s not great for mass actions repeated over and over again. If you’re ready to take your WordPress management skills to the next level, why not try the command line instead? In this article, we’re going to take a look how to install WP-CLI on Windows.
While WP-CLI is a PHP application and thus was originally envisioned as running in a Linux environment, it ports over easily as a Windows application. Many web hosts provide WP-CLI as a tool from which you can manage your WordPress installations. So while putting WP-CLI onto a Linux server is simple, we need to take a few intermediate steps to ensure that it works properly on Windows. To install WP-CLI on Windows we need to take the following steps:
- Install PHP;
- Install the Visual C++ Redistributable;
- Basic PHP Configuration;
- Download and setup WP-CLI.
The first step is deciding which version of PHP you want to install. Since we’re not going to be doing anything terribly complicated like running an Apache Web server on a Windows system, we don’t really have to worry too much about compatibility. Our goal is simple – provide a PHP environment from which we can execute scripts.
But even before we install PHP, you need to know what kind of system you’re working on. x86 and x64 architectures are completely different installation packages. To find out what you’re running, open up the system information pane in Windows and check to see whether you have a 64-bit or a 32-bit operating system as shown here:
If like me you have a 64-bit system, then we are interested in the x64 version of PHP. If yours is 32-bit, then you need to download the x86 version instead.
To do this, go to the PHP download page for windows. In the drop-down box at the top, we need to select our PHP version that matches our architecture. In this example, I’m selecting the latest PHP version 7 for x64.
Making the selection will scroll me down to the package relevant to me. Download the zip file onto your PC and extract it into a folder in C: called “php” like this:
A PHP installation needs a “php.ini” file to function. Luckily for us, we don’t have to create one from scratch. Instead, the PHP version that we just downloaded comes already bundled with sample php.ini files. In this example, I have one called “php.ini-development” and one called “php.ini-production”. I simply rename the latter to “php.ini” and I’m done.
Installing the Visual C++ Redistributable
Each version of PHP for windows is compiled using the Visual Studio compiler. For it to run on your local PC, you need to have the appropriate Visual C++ redistributable. There are many versions and you need to download the one that is right for you. The version number will be given after “VC” in the PHP package name. You can see the screenshots for example, that mine is 14. So I need to download the Visual C++ redistributable version number 14. You can get the links for the downloads from the left-hand sidebar on the PHP downloads page for windows.
Click that and you’ll be redirected to the Microsoft site where you can download the installer and run it. After installation, you would have all the required software for PHP to run on Windows. But we still need to take a few additional steps to ensure that you have a workable environment.
Adding the PATH Variables
Adding PATH variables is very important in Windows so that you don’t have to navigate to the specific directory where your executable files are held in order to run them. For example, we’ve extracted our PHP zip file into C:\php. But merely opening up a command line prompt and typing in “php” is not enough since Windows will not know where to look for the file “php.exe”. So we need to tell it by setting up explicit environment variables as follows.
Go to the “Advanced System Settings” tool in Windows and at the bottom, you’ll find a button labeled “Environment Variables”.
Click this, and you’ll see a dialog with two panes at the top and bottom. Select the line called “Path” and click “Edit”
This will show you a list of your environment variables. One for each line. Click “New”, and in the resulting text box, enter the path to your PHP folder – C:\php as shown here:
Save your changes. The PHP executable inside the folder should now be available from the command line prompt from any directory. To test this, open up the command line in Windows with admin permissions. To test whether or not you have a working PHP installation and if the environment variables have been set up properly, type in:
This should print out a complete list of PHP related configuration information. If you see this, you’ve done everything right up until this point!
Downloading and Setting up WP-CLI
Now that we have the right environment, we can finally download WP-CLI. Grab the latest package from github here. This is a “phar” file. Just like zip or other archive file formats, the “phar” standards allow us to package an entire PHP application into a single file. Is an extremely convenient method for distributing PHP applications neatly.
As we did before, create a directory in C:\ called wp-cli and move the file we just downloaded into it. Next, create a new file inside the same directory called “wp.bat” and paste the following code inside:
@ECHO OFF php "c:/wp-cli/wp-cli.phar" %*
Finally, we need to add the C:\wp-cli directory into our environment path as we did earlier for the PHP C:\php directory. Just follow the same steps as before. Now to run the WP-CLI installation, we can simply type in “wp” from any command prompt in Windows from any location. We can even omit the last step and simply type in the command directly to execute the “phar” file, but this is much easier.
To check and see whether WP-CLI is set up properly, execute the following line on the command prompt:
This should give you an output as shown here if everything goes correctly:
Now the WP-CLI has been properly installed, it’s time to learn how to configure it so that it connects to your WordPress installation, thereby allowing you to manage it seamlessly from the command line instead of having to wait for the GUI to load, clicking links etc. You can read my follow-up article on how to configure WP-CLI to connect to WordPress.