Starting with version 8.1, the Drupal core has received a dedicated package manager Composer. It helps to handle managing dependencies in PHP-based projects as well as installing and updating libraries quickly, including the third-party ones (which had to be performed ‘manually’ by developers before).

If you already have any experience in working with package managers like Linux apt, yum, or Yarn, it won’t be difficult for you to get a grip of Composer (it is basically a regular package manager exclusive to PHP).

Important: Currently, Drupal 8 supports PHP 5.5.9+. You may still, however, require at least version 7 PHP.

This article reviews and discusses the package manager and explains how to use Composer with Drupal.

Advantages of using Composer with Drupal

Despite Composer being an additional capacity in the Drupal core, its integration has become common courtesy among web developers. It's only reasonable for project scaling when the number of modules gradually adds up.

Not yet sure whether you need Composer or not? Let’s take a look at the benefits:

ability to manage any Core, Contrib packages or even personally developed Modules as well as JS libraries;

much simpler in use than Git submodules;

accessible dependencies system, which can download third-party dependencies without manually executing multiple operations;

no need to detect changes during updates (Composer handles that as well);

D8 repository is as small as possible;

accessible and near-fully automated patching system enabled by the employment of the special composer plugin. The patches are applied straight in the composer.json;

an ability to use composer.json repeatedly in new projects (config-files nginx on the server must be customized for that, but it's the most accessible option out there).

To sum up, Composer practically allows fully automated management of the ever-growing list of dependencies (e.g. you can autonomously download and update certain packages’ versions). Composer isn't completely simple to use, but once you figure it out, you'll free yourself from tons of cumbersome routine tasks.

How does Composer work?

Composer consists of two files:

composer.json. This file includes data on which particular packages are required, where they should be installed, and which configurations and scripts must be included in it. Basically, composer.json provides recommendations on all the things you can manually customize in the module you’re installing.

This file includes data on which particular packages are required, where they should be installed, and which configurations and scripts must be included in it. Basically, composer.json provides recommendations on all the things you can manually customize in the module you’re installing. composer.lock. This one is automatically updated and includes all the data about the CURRENT state of a project. It appears without any manual input as well and there’s no need to customize or delete it.

Learning to work with Composer

Have you already decided to start learning using Composer with Drupal 8 sites? The tool has three main commands. If you learn to work with them, you’ve got the skills for working with Composer.

composer require PACKAGENAME

The modules are installed with the composer require PACKAGENAME command. In order to install a bunch of modules in one command, simply define their names separated by spaces (names of modules are defined in the format vendor/name - which allows you to exclude cases where identically named packages occur inside a single project). For all projects from drupal.org, the format would be drupal/PROJECT_NAME. So, you should input the following command in order to install, for instance, a Commerce module — composer require drupal/commerce.

composer update --with-dependencies

The update is very similar to installation. The difference between these two processes lies in that with this command the name of the package is unnecessary. The composer update command updates all dependencies by default defined in the composer.json file. I.e. update via Composer only involved the packages mentioned in that file. On the other hand, if a module features special dependencies which are not defined in composer.json, they will be updated only if you additionally employ the following keyphrase in the command - --with-dependencies. Use the composer update --with-dependencies in order to update each and every package, including their dependencies (this command is most relevant to use when minor core version updates are taking place). You can define the certain names of the packages to be updated as well: composer update drupal/commerce.

composer remove PACKAGENAME

Packages are deleted with the composer remove PACKAGENAME command. Take care with the required module being deleted from your admin panel too (this should be done before the command is input).

...And for several more important moments in using Composer

Unfortunately, not all beginners can immediately realize that the best solution is to start building a project via Drupal 8 Composer initially, as there are no tips for this on the official website.

Firstly, that way, you can forget about the frequent employment of the Drush utility. Secondly, you won’t need to manually install all the required modules by downloading them through FTP (which may only be required for custom solutions) or the administrative panel. Thirdly, Composer allows downloading and updating not only modules and themes, but the core itself. Without Composer (which is practically a standard for the 8th version of Drupal), you won’t be able to install some modules at all (these may include such modules as Commerce, Search API, ImageMagick, and others). Even if you handle downloading and launching them, they still won’t work due to the unavailable dependencies in the form of packages stored in Composer exclusively. On the other hand, if you work with this tool from the very beginning, the dependencies in a project will be defined automatically and you won’t have to struggle with which to update and which not to.

We expect the number of modules installed only through composer will increase in the future. This means that if you decide to update your Drupal to the 8th version, you will have to learn to work with Composer one way or another.

Drupal 8 Composer tutorial: conclusion

Composer isn’t the only solution for your project if you’re working with Drupal of a version earlier than the 8th. The latest version, however, gives developers maximum opportunities for the implementation of project functionality and maximum solutions for automation (which, in turn, allow focusing on the more uncommon tasks and not waste time on the connection/update of modules). That’s why, sooner or later, you’ll have to face the necessity of using Composer with Drupal. And that’s exactly why we recommend learning the nuances of working with this tool early on (we hope our brief Drupal 8 Composer tutorial helps you with that) in order to make your further development processes smooth and result-driven. All in all, it will be an excellent time investment in your future website developer career.

Got an idea for a project? We’ll provide its top-notch execution!