Updating Drupal Module is a fairly painless process whether you manage your website using the GUI or using Composer. In this article, we will be looking at both ways. Before we update our live site or production site however we need to make sure we have first tested the update or updates on what is commonly known as a staging site.
To create a staging site you will need to copy the site files and duplicate the database to a new database, and then change the
settings.php database configuration. You may also need to change some
.htaccess directives if you have your site set up in a sub-directory.
I will also mention, as your site becomes more complex as your business grows you may update your production site using a Git workflow and the built-in configuration management. I won't cover this here as this is an advanced topic and this article is aimed at smaller sites.
Even if your staging site is an exact copy of your current production site I would still suggest making a backup of both the files and the database. Check the Updating Drupal core article's preparation section for more details on that.
Okay, with that out of the way we can move on with the updating of our site modules.
Using the GUI to update Drupal modules
To update using the GUI is as simple as following these steps.
- Visiting the update page.
- See the updates needed.
Read the release note if you feel the urge.
Over time it is a good thing to do this as you can see new features added or if you had patched the module for a bug you may see that the issue has been fixed and thus the patch is no longer needed.
If you are happy to update continue with the update.
- Put your site off-line.
- Check the check box
- Click update
The system will go through the process, download the archive, apply the updated module, and then let you know it is done on a page that also lets you know if there are any database updates.
- Confirm update
- If there are database updates, run the script by choosing the correct option.
- If there are no database updates you can put your site online.
- Clear the cache using the GUI or Drush.
- Check out the site do some basic checks of functionality that uses the module you just updated. If this is production you should have done this step but it is always good to check again. After checking over your site you can visit the recent log message page in the reports section of the admin.
Roll-back an update
Rolling-back a module is as easy as replacing the module source code with the previous version you had installed. You will need to do this either using the CLI or cPanel file browser, delete the currently installed updated version and add back the version you need.
If you got as far as running database updates, then you will also need to restore the database. Hopefully, your site is still working and if so then you can use Backup and Migrate restore. If you have a WOD (white screen of death) then you will need to get deeper into the workings of SQL; check out working with SQL for more information.
Roll-backs are something you never want to do and is one of the reasons you should always be working on duplicate site to test updates. However, if you are using Composer the process to roll back is even easier, you can just run
composer require to the version you need. Database restore is the same regardless of the way you update the code. Let's now look at using Composer to update.
Using Composer for updating modules
If you are managing your site with Composer, which I recommend you do, then updating is just a couple of commands. I would guess that if you are using Composer you will also be using Drush so the commands following are a mix of Composer and Drush.
Log in to your VPS using SSH and cd into the root of your Drupal site.
Show outdated modules with the below command.
composer show -o "drupal/*"
See what updates are needed and put your site offline and run the update command. In the following example, we are updating the pathauto module with dependencies. It is best to update dependencies if they need to be updated so using the --with-dependencies flags is good practice.
drush state:set system.maintenance_mode 1 drush cache:rebuild composer update drupal/pathauto --with-dependencies
After Composer has done its job, check for database updates, make the updates if needed, and then put your site back online and rebuilt the cache.
drush updatedb:status drush updatedb drush state:set system.maintenance_mode 0 drush cache:rebuild
Roll-back an update with Composer
If you need to roll-back you will explicitly need to set the version to the version you want using Composer; see how the version is set to
1.7 and not
composer require 'drupal/pathauto:1.7'
Using the ^ sets the version minimum and it will always update to the latest if you use it, so you may want to use it later once you have the issue sorted. See more about versioning and using the caret
^ on the Composer website.
On this page, we have look at updating modules in Drupal 9, something as a site owner or manager I suggest you do as needed. It is easier to run a single update as needed than get way behind and be confronted with modules that conflict due to being old. I don't really have any proverb I can quote here but if I did it would be something like the turtle always wins the race, meaning that you are better to take lots of small slower steps forward that have to rush.
On a final note, with the maturity of Drupal over the year it seems that you don't need to add a lot of contributed or custom modules to get a great business site up and running and the modules I do recommend are very stable and don't seem to have new release all that often.
On that note Happy Drupaling and thanks for reading!