Install and update Drupal 8 core and contrib modules with Composer

Submitted by christophe on Wed, 26/10/2016 - 14:00
Composer logo

 

Some modules, like Address or Search API Solr, really needs an installation with Composer.
We should now avoid to rely on Composer Manager (deprecated) so here is a kickstart.

1. Turn off XDebug

If enabled, turn it off, otherwise it will certainly result in a time out.
Edit your php.ini or specific conf file (e.g./etc/php/7.0/cli/conf.d/20-xdebug.ini) and comment the following line :

; zend_extension="/usr/lib/php5/modules/xdebug.so"

More on this : https://getcomposer.org/doc/articles/troubleshooting.md#xdebug-impact-on-composer

A cleaner option, if using a VM for running Drupal (DrupalVM, ...), is running Composer on the host OS instead (MacOS, ...), it will avoid turning XDebug off (assuming that XDebug is not installed or is disabled on your host). This is the setup that I choosed.

2. Install Drupal Core

Download the Drupal core into your docroot.
In a private git setup, I like to have the docroot directory defined in the repo, close to the config directory (so the directory structure is /docroot, /config and not Drupal core directly at the root of the repo).

# replace 8.2.*@dev by the desired version
composer create-project drupal/drupal docroot 8.2.*@dev
cd docroot
# make it git compliant for your own repo, if not done during project creation
rm -Rf .git
cp example.gitignore .gitignore

Then install Drupal via drush (on your VM if using one)

drush site-install standard --db-url='mysql://[db_user]:[db_pass]@localhost/[db_name]' --site-name=Example

3. Fetch contributed modules and themes

Add the Drupal composer repository

composer config repositories.drupal composer https://packages.drupal.org/8

Define this rule in the composer.json to place them in the contrib directory

"extra": {
    "installer-paths": {
        "modules/contrib/{$name}": ["type:drupal-module"],
        "profiles/contrib/{$name}": ["type:drupal-profile"],
        "themes/contrib/{$name}": ["type:drupal-theme"]
    }
}

The result will look like

Compose contributed modules and themes extra

 

Then download the desired modules.

# download the modules + dependencies
composer require drupal/search_api
composer require drupal/search_api_solr
# use 1.x-dev instead of ~1.0 to get the -dev release instead
composer require "drupal/address ~1.0"


Then you can go on by enabling modules with drush e.g. drush en search_api (on your VM if using one).

4. Update

# update everything
composer update
# or the core only with dependencies
composer update drupal/core --with-dependencies
# or a single module with its dependencies
composer update drupal/address --with-dependencies
# run database update on the VM
drush updb

Pitfall

After running composer update drupal/core --with-dependencies, if you got the following error

Package "drupal/core" listed for update is not installed. Ignoring.

make sure that the require section of your composer.json contains

"drupal/core": "~8.2"

and is not included in the replace section.

5. Apply patches

Some core and contrib modules will probably still need patches at the time of writing, define them into the extra section of your composer.json, they will apply automatically when you run composer install or update.

"patches": {
            "drupal/weight": {
                "Weight selector missing": "https://www.drupal.org/files/issues/weight-show-selector-2671840-4.patch"     
            },
            "drupal/core": {
                "Allow image fields to use any extensions the current image toolkit supports": "https://www.drupal.org/files/issues/1014816-69.patch"
            }
}


Ressources



 

Comments

Your Pitfall section under Update saved me! Thank you!

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.