Before beginning with the What and Why of Composer, let’s understand the difference between “Upgrade” and “Update”.
Composer Best Practices for Updating Drupal 8 Core and Modules
Upgrading a Drupal website means migrating it to a new major version. For instance, moving from Drupal 6 to Drupal 7, or from Drupal 7 to Drupal 8. However, updating a website means moving it from one minor version to another. For instance, switching from Drupal 8.5.3 to Drupal 8.5.6.
Are you familiar with Composer?
Composer is used as a dependency manager in PHP. It helps declare and manage libraries on which your project depends or the modules that your website is made of. You can easily manage core dependencies such as Symfony components and certain packages.
The first thing you need to do is ask yourself a question — Why do I require Composer when I can update the Drupal core and modules simply by downloading these from the Drupal website?
This is because updating your Drupal-powered website by downloading the modules from Drupal’s official website is not simple. The reasons:
- A number of modules and themes cannot run the application without a third-party library. Managing the third-party library individually after installing the modules is a tedious task.
- Some modules and packages need to be compatible with a certain PHP or Drupal version. This may lead to some version-related issues while upgrading the Drupal core and modules, which you need to find manually.
- Some packages and modules may collide with each other when you run the composer update on the CLI. To fix the issue, you may need to review the composer.json file.
Update Drupal core and modules using Composer
Before beginning with updating your Drupal core and modules, you must learn about Composer. You can either refer to the documentation on Drupal’s official website or go for online tutorials.
The first step is to install Composer on your development machine to easily execute any Composer commands. It enables you to execute the installer directly from the command line.
The next step is to set up the website project template with Composer.
Take a backup of the database and files before the update
- Take a backup of the database dump using drush sql-dump.
- For backup of code and files, you can use drush archive-dump. It helps you take a backup in a single directory in Drush 8. However, if you upgrade the system or server using Drush 9, the command would not work, in which case you can use the tar command.
Check what Drupal core and packages are available to update
- Before you begin with updating your Drupal core and packages, verify which versions are available for update. Run the command Composer outdated in the command line interface, which provides a full package list, including the Symfony components, necessary for Drupal 8.
- If you simply want to verify which Drupal core version is available for update, run the command Composer outdated drupal/*. It provides a list of all core versions that need an update.
Update Drupal 8 core
- Begin with reading the core release notes to learn which issues are fixed in the release. It will help you understand which versions and packages need an update.
- Use the command drush sset system.maintenance_mode 1 to activate the maintenance code. Clear the caches using drush cr.
- Update the Drupal core and its dependencies, by running the Composer commands—Composer update Drupal/core –with-dependencies
- To update any pending database, run the command drush updb and clear the cache using the drush cr command.
- On the reports->available updates page, you can verify if the Drupal website is updated or not.
- For error reporting and website testing, review the reports->status page.
- Using drush sset system.maintenance_mode 0, deactivate the maintenance mode. Then, run the command drush cr to clear the cache.
- The best way to update modules on your Drupal 8 website is by using Composer. This is because Composer downloads the modules along with related dependencies needed to run the application.
- The Composer command require “drupal/<modulename>:<version> is recommended to install a single module, where you can specify the module name and version. For instance, the command Composer require “drupal/social_auth_google:^2.0” helps download the Social auth login module’s latest 8.x.-2.0 release.
- Social Auth Google module can be run using the third-party library League Google Provider for OAuth 2.0. Composer downloads the correct version of SDK, which is compatible with your module.
- Finally, you can verify the League folder in the vendor directory.
This is how Composer makes it substantially easy to update Drupal 8 core and modules if you understand the basics of the technology.