Drupal Best Practices: Use Drush and Composer for Efficient Site Management
Drupal development involves managing various dependencies and performing repetitive tasks like clearing caches, updating modules, and exporting configuration. Without proper tools, these tasks can become time-consuming and error-prone. Luckily, Drush and Composer—two essential tools in the Drupal ecosystem—can dramatically improve your workflow, ensuring efficient management of your Drupal site while maintaining consistency across development environments.
In this blog post, we’ll explore why Composer and Drush are essential tools for Drupal developers and how you can leverage them to streamline your development process:
- Composer for Dependency Management: Manage all dependencies, including Drupal core, contributed modules, and libraries, using Composer. It simplifies updates and ensures consistency across environments and teams.
- Drush for Command Line Operations: Use Drush to speed up common development tasks, such as clearing cache, updating modules, exporting configuration, and more.
By adopting these tools, you'll not only improve your development efficiency but also enhance collaboration and maintainability of your Drupal projects.
Composer for Dependency Management
Composer is a PHP-based dependency manager that’s become the standard for managing Drupal projects, starting with Drupal 8. It simplifies the process of managing dependencies—such as modules, themes, and external libraries—ensuring that your Drupal site is consistent across all development environments. Composer also makes it easier to update Drupal core and contributed modules without manual intervention.
Why Composer is Essential for Drupal Development
- Centralized Dependency Management: Composer allows you to manage all the dependencies of your Drupal site in a centralized way, using a
composer.json
file. This means that Drupal core, contributed modules, and third-party libraries are all managed through a single interface. - Consistent Environments Across Teams: By using Composer, you ensure that every developer on your team has the same versions of Drupal core, modules, and libraries. This consistency is critical for avoiding the "works on my machine" problem, where code behaves differently across environments.
- Simplified Updates: Updating Drupal core, modules, or libraries is as simple as running a single Composer command. Composer resolves dependencies, downloads the necessary files, and updates your
composer.lock
file to ensure your environment stays in sync. - Version Control: Instead of committing the actual code of modules and libraries into version control, Composer tracks them in the
composer.json
file, while the installed versions are recorded in thecomposer.lock
file. This keeps your repository clean and ensures that all developers or automated deployment systems can install the exact versions of the dependencies required for the project.
Setting Up Composer for Drupal
If you’re starting a new Drupal project or migrating an existing project to use Composer, here’s how to get started.
1. Create a New Drupal Project with Composer
You can start a new Drupal project with Composer by running the following command:
composer create-project drupal/recommended-project my_drupal_site
This command sets up a new Drupal project, including the necessary files like composer.json
and composer.lock
, and installs the latest version of Drupal.
2. Installing Modules with Composer
To install a contributed module, you can use the composer require
command. For example, to install the Pathauto module, run:
composer require drupal/pathauto
Composer will handle the download, as well as any dependencies the module requires. It also ensures that the installed version is recorded in the composer.lock
file so that other developers can install the same version with composer install
.
3. Updating Drupal Core and Modules with Composer
To update Drupal core or any contributed module, run the following command:
composer update drupal/core --with-dependencies
This will update Drupal core and its dependencies to the latest version specified in your composer.json
file. Similarly, to update a specific module, run:
composer update drupal/pathauto
After updating, commit the updated composer.lock
file to version control so that your team or deployment pipeline installs the correct versions.
4. Manage Patches with Composer
If you need to apply patches to a module or core, you can use the cweagans/composer-patches plugin. First, install the plugin:
composer require cweagans/composer-patches
Then, add your patch to the composer.json
file:
"extra": {
"patches": {
"drupal/pathauto": {
"Description of patch": "URL to patch or local path"
}
}
}
When you run composer install
or composer update
, the patch will be applied automatically.
Drush for Command Line Efficiency
Drush is a command-line interface for Drupal that simplifies and automates many common tasks. Instead of manually navigating the Drupal admin interface to clear caches, update modules, or export configuration, you can use Drush commands to handle these tasks in seconds. Drush boosts productivity and ensures that you can perform tasks consistently across development, staging, and production environments.
Why Drush is Important for Drupal Developers
- Speed Up Development Tasks: Drush allows you to perform common tasks like clearing caches, running updates, and managing modules much faster than doing so through the admin interface.
- Automation: Drush is scriptable, which means you can automate tasks such as deployments, cron runs, or content imports. This is particularly useful for continuous integration and deployment pipelines.
- Consistency: Drush provides a consistent way to manage Drupal across different environments. Whether you’re working on your local development environment or on production, Drush commands work the same way.
- Access to Advanced Features: Drush also gives you access to advanced features like SQL queries, debugging tools, and site synchronization that are not available through the Drupal admin interface.
Common Drush Commands for Everyday Use
Here are some of the most useful Drush commands for speeding up your development process.
1. Clearing the Cache
Clearing the cache is one of the most common tasks in Drupal development. With Drush, you can clear all caches with a single command:
drush cr
This command is short for drush cache-rebuild
and clears the cache for all components of your Drupal site.
2. Installing and Enabling Modules
You can download and enable a contributed module using Drush. For example, to install the Pathauto module, run:
drush en pathauto -y
If the module isn’t already installed, Drush will automatically download it from Drupal.org (if Composer isn’t used) and enable it.
3. Running Database Updates
When you update modules or Drupal core, you may need to run database updates. Instead of using the admin UI, you can execute updates via Drush:
drush updb
This command applies any pending updates to your Drupal database.
4. Exporting and Importing Configuration
Managing configuration in Drupal often requires exporting and importing configuration changes. Drush makes this process simple:
- To export the configuration, run:
drush config-export -y
This command exports all configuration changes to the config/sync
directory.
- To import configuration changes (from another environment or after making changes in version control), run:
drush config-import -y
This automates the process of syncing configuration between environments, which is critical for maintaining consistency.
5. Running Cron
To manually trigger Drupal’s cron tasks (such as clearing logs, checking for updates, or running scheduled tasks), use the following Drush command:
drush cron
This is especially useful if you need to trigger cron manually during development or testing.
6. Running SQL Queries
Drush also allows you to run SQL queries directly on the Drupal database:
drush sqlq "SELECT * FROM users LIMIT 10;"
This can save you time when debugging or inspecting data without needing to open a separate SQL client.
Combining Drush and Composer for a Smooth Workflow
Drush and Composer complement each other perfectly in a modern Drupal development workflow. Here’s an example of how you might use both tools together:
- Installing a New Module:
- Use Composer to install the module and handle dependencies:
bash composer require drupal/pathauto
- Use Drush to enable the module:
bash drush en pathauto -y
- Updating Drupal Core or Modules:
- Use Composer to update core or a module:
bash composer update drupal/core --with-dependencies
- Use Drush to apply database updates and clear the cache:
bash drush updb -y drush cr
- Exporting Configuration:
- Use Drush to export configuration changes:
bash drush config-export -y
- Commit the changes to version control, ensuring your team can synchronize configurations across environments.
Conclusion
Using Drush and Composer is essential for developing and managing Drupal websites efficiently. Composer simplifies the management of dependencies, ensures consistency across environments, and makes it easy to apply updates. Drush accelerates common tasks like clearing caches, updating modules, and exporting configurations, freeing up more time for actual development work.
By incorporating these tools into your workflow, you’ll not only save time but also improve the consistency and maintainability of your Drupal projects. Whether you’re working on a small site or a large, enterprise-level application, Composer and Drush will enhance your productivity and ensure smooth collaboration across your development team.