Using Composer with Drupal 8

With Drupal 8, composer is super easy to use and should be the defacto standard for managing your Drupal projects.

I am not going to go into too much detail here about Composer but in a nushell, Composer is a PHP based package manager that looks after your project dependencies. You need it because it makes your life easier!

To get started, you obviously need to install composer

Once you have composer working, the first thing you should do is globally install the prestissimo plugin. This plugin drastically speeds up composer by performing parallel downloads.

Now that you have Composer working, let's install a Drupal project. I suggest you use Composer template for Drupal projects

Creating a new Drupal project is as easy as the following command:

composer create-project drupal-composer/drupal-project:8.x-dev my-drupal-project-folder --stability dev --no-interaction

By default, it will put your webroot in a folder called web. If you wanted to rename it to docroot for compatibility with Acquia, it's pretty simple:

  1. Rename the web folder to docroot.
  2. Rename web occurrences in composer.json to docroot.
  3. Edit the scripts/composer/ScriptHandler.php file and replace "/web" to "/docroot" in the getDrupalRoot function.

I use the official drupal.org composer endpoints. Although they are technically in beta, they are pretty solid. If you use these, you will need to change the way you do your versioning as the versions are transposed to remove the core version and have proper semantic versions. You can't completely switch over to the d.o endpoints until the Drupal core subtree split issue has been dealt with. Until then, you can use both the d.o composer endpoints and drupal composer packagist together (just for the drupal/core package) with d.o endpoint getting priority:

...
"repositories": {
        "drupal": {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        },
        "drupal-composer": {
            "type": "composer",
            "url": "https://packagist.drupal-composer.org"
        }
    }
...

The project comes with the latest version of Drush so to install Drupal, simply do a site-install:

drush site-install --db-url=mysql://user:[email protected]/mydatabase -y

Congratulations, you now have a working Drupal installation!

Now managing new packages is easy. If you wanted to add the CloudFlare module, you would use:

composer require drupal/cloudflare:~1

Or to update all your project dependencies including Drupal core, projects. etc:

composer update

Enjoy!