Composer is package manager for PHP. Explaining purpose of package manager is beyond scope of this article.
This article, actually section, will describe how to use Composer within WordPress ecosystem. We believe this will benefit developers as well as system-admins.
As composer is a big topic, it’s better to define scope first. We will cover following:
We will also cover some composer concepts for newbies.
You may follow official instructions present here.
We follow and recommend slightly different installation as below:
curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir=/usr/local/bin
This will install composer
binary in /usr/local/bin
path. This will make it easy to run composer commands from anywhere in system.
We recommend using brew:
brew install composer
Composer has a really good documentation which can explain this in much better way.
Everything in project revolves around composer.json
To understand concept in quickly, lets divide entire workflow between: development and deployment.
composer.json
file. It can be created from scratch, copy/pasted from existing sample or can be generated using composer commands like composer init
/composer create-project
.composer.json
file. Again dependencies can be added by directly editing composer.json
file in text-editor or using composer command composer require
.composer.json
file for their sub-dependencies.composer update
command. This downloads all dependencies locally and generates a composer.lock
file.composer.json
and composer.lock
files to git repo. And git push our project/package to something like Github.Steps 5 and 6 need to followed everytime we need to update dependencies.
Note: Please note that sometime a dependency update may not require making changes to composer.json
file. Only running composer update command would suffice.
git clone
a composer-powered project.composer install
commands which will download dependencies based on composer.lock
content.composer.lock
file is not present, composer install will refer to composer.json
. But we strongly recommend using composer.lock in production setup.As you can see, deployment process is very streamline. You may add composer install
to your webhooks or deploy scripts.
Use Case: If you want to deploy a WordPress project across multiple-servers or may be using a cloud-hosting platform like Amazon ElasticBeanstalk, you need to put entire WordPress project under version control.
Usually this involves using git submodules
, which we find tedious. Here is a simpler and better way to achieve same with composer.
composer.json
in root folder (htdocs
typically).composer.json
will contain required WordPress version, theme and plugin dependencies.composer update
command. This downloads wordpress core, themes and plugins locally and generates a composer.lock
file.composer.json
and composer.lock
files to git repo. And git push our project/package to something like Github.Steps 5 and 6 need to followed everytime we add or remove themes/plugins or change WordPress core version.
git clone
a composer-powered project.composer install
commands which will download WordPress core, themes, plugins and any other dependencies based on composer.lock
content.composer.lock
file is not present, composer install will refer to composer.json
. But we strongly recommend using composer.lock in production setup.We also use some custom composer scripts as part of deployment flow. More about it will be explained later.
Following are commands we use quite often:
composer.json
interactively. We really don’t use this but copy/paste existing files.composer.json
and generate/update composer.lock
file.composer.lock
file. If composer.lock doesn’t exists, download dependencies based on composer.json