When working on WordPress-based projects that utilize Composer, there are times that we’re going to want to have certain dependencies available for our project and there are times where we’re going to want to have tools available throughout our system.
For example, for some projects, we may want to have PHP CodeSniffer with the WordPress Coding Standards available. For others, maybe not.
But having a tool like WP-CLI is something that’s likely better served when it’s available throughout the entire system rather than on a project-by-project basis. Globally installing WP-CLI is a relatively trivial matter.
It does, though, assume you have Composer already installed and available on your system.
Globally Installing WP-CLI
There are only three steps required to make this happen:
- Define WP-CLI in Composer’s configuration,
- Install the utility,
- Make sure Composer’s vendor directory is in your .bash_profile (or the variables of whatever shell you want to use).
To update, add WP-CLI to Composer’s configuration, you can do this via command line by entering:
$ composer global require wp-cli/wp-cli
Or you can manually add it to the composer.json file by navigating to:
$ cd ~/.composer
And then opening composer.json in your editor of choice.
From there, you add the following lines:
{ "require": { "wp-cli/wp-cli" : "^1.1", "psy/psysh" : "~0.6" } }
Next, run the following command so that Composer will install WP-CLI:
$ composer global update
Then update your environmental variables (if you haven’t done so at some point earlier). You can do this by entering the following command:
$ export PATH=~/.composer/vendor/bin:$PATH
And from here, you should be able to run wp from the command-line with no problem:
And you’ll have it globally accessible across your system. This means that you’ll be able to use it outside of any given project and that you can access it from an instance of your terminal inside or outside of your IDE.
The convenience this adds – especially if you want to learn to do a lot of CLI work with WordPress – pays dividends.