Skip to content
STAGE33R

Guides · Server software · 6 steps · reviewed

How to update Drupal core and modules safely with Composer and Drush

Drupal security advisories are published for core and contributed modules and are often exploited within days. A Composer-based site can be updated in a few commands, as long as you back up first and run the database updates.

  1. Back up code and database

    Keep the backup outside the web root.

    cd /path/to/drupal
    drush sql:dump --gzip --result-file=~/drupal-$(date +%F).sql
    tar czf ~/drupal-files-$(date +%F).tgz --exclude=web/sites/default/files/php .
  2. Put the site in maintenance mode

    Visitors see a notice while the code and database are out of step.

    drush state:set system.maintenance_mode 1 --input-format=integer
    drush cache:rebuild
  3. See what is outdated

    Composer lists the packages with updates; focus on security releases first.

    composer outdated "drupal/*"
    drush pm:security
  4. Update core and modules

    The with-all-dependencies flag updates what core needs.

    composer update "drupal/core-*" --with-all-dependencies
    composer update drupal/MODULE --with-dependencies
  5. Run database updates and clear caches

    This applies the schema changes that come with the update.

    drush updatedb --yes
    drush config:import --yes   # only if you use configuration management
    drush cache:rebuild
  6. Reopen and check

    Take the site out of maintenance mode and read the status report.

    drush state:set system.maintenance_mode 0 --input-format=integer
    drush core:requirements --severity=1

Common mistakes

  • Running composer as root: file permissions break.
  • Updating a module that is not compatible with the new core: read its release notes.
  • Skipping drush updatedb: the site shows errors until it runs.

How to undo it

Restore the files and the database from the backup, or git checkout composer.lock and run composer install.

Sources

Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.