A Guide to Drush with Lando: Essential Commands for Drupal Management
If you followed our guide on installing Drupal with Lando, you're ready to take your local development environment to the next level. Enter Drush, your new best friend. Drush is a command-line tool for managing Drupal sites. It is like a magic key that unlocks some secret Drupal powers. It will provide you with the convenience to handle your Drupal site entirely, without ever leaving your command line. With Lando, Drush comes pre-installed, simplifying your workflow even further. This guide will walk you through essential Drush commands within Lando and reveal developer secrets to maximize productivity in your Drupal projects.
What Is Drush?
Drush, short for Drupal Shell, is a powerful command-line tool specifically built for managing Drupal sites. Designed to make administrative and development tasks quicker and more efficient, Drush allows developers to manage nearly every aspect of a Drupal site without needing to access the web interface. This tool is particularly valuable for developers who want to automate repetitive tasks, manage complex workflows, or troubleshoot issues more proficiently.
With Drush, you can perform a wide range of operations directly from the terminal, such as:
- Module and Theme Management: Install, enable, disable, and uninstall modules and themes quickly, which is much faster than using the web interface.
- Database Management: Run database backups, restores, and updates, making it easier to manage Drupal’s backend without needing additional database tools.
- Cache and Performance: Clear and rebuild caches to troubleshoot or refresh the site’s performance. This is especially helpful during development, as cached data can sometimes prevent updates from displaying correctly.
- Configuration Management: Drush provides tools for exporting, importing, and synchronizing configurations, which is crucial for moving settings and customizations between environments (like from development to production).
- User Management: Create, update, delete users, assign roles, manage permissions and login to any account with just a few commands.
- Cron Jobs: Run scheduled tasks on-demand. Drupal sites often rely on cron jobs for maintenance tasks, such as checking for updates and cleaning up logs, and Drush makes it easy to trigger these manually when needed.
One of Drush’s greatest strengths is its versatility in adapting to complex development workflows. Many developers rely on Drush to automate large portions of their work, writing scripts that handle repetitive tasks or deploying custom commands for unique site requirements.
Using Drush with Lando
With Lando, Drush is available within your environment without any extra setup. All Drush commands can be prefixed with lando to run them inside your Lando environment. If you have not installed Drupal with Lando yet or need a refresher on using the command line, we recommend following our guide that goes through the process step-by-step: How to Install Drupal with Lando
Basic Commands and Setup
First, you will need to make sure that you’ve installed Drupal with Lando and that your Lando environment is up and running.
Once that is done, navigate to your Drupal project directory:
cd my-drupal-site
And start the environment:
lando start
Once started, you can begin using Drush commands with the lando prefix.
Essential Drush Commands with Lando
Check Drupal Status
The st, or status command verifies the status of your Drupal site, showing essential information about the installation, including the version of Drupal core, PHP version, database connection status, and file permissions. This is helpful for quickly checking that your environment is set up properly. Each command has a short version, or alias:
lando drush st
For any of those commands, you can also use the full name:
lando drush status
Generate a One-Time Login Link
The uli, or user-login command generates a one-time login link for a specified user or the admin user if no username is provided. This link bypasses the login screen, allowing immediate access to the Drupal site, which is especially useful if you’ve forgotten the admin password or are testing permissions. Use --name followed by the username to specify a different user account.
lando drush uli
To generate a login link for a specific user, include the username:
lando drush uli --name="username"
You can also go one step further and generate links with the user ID. For example, with the user ID 3:
lando drush uli --uid=3
Or even, using the user’s email address:
lando drush uli --mail=user@example.com
Clear Cache
Drupal uses caching to improve performance by storing copies of data that would otherwise need to be generated repeatedly. However, cached data can sometimes cause issues during development by preventing updates from appearing immediately. The cr, or cache-rebuild command clears all caches, ensuring that your site reflects the latest changes.
lando drush cache-rebuild
Enable a Module
Modules add functionality to your Drupal site. The en, or enable command activates a module in your site’s configuration, making it ready for use. Replace module_name with the module you want to enable, such as pathauto or views. This command allows you to quickly enable new features without navigating the admin interface.
lando drush en module_name
Disable a Module
If a module is no longer needed or is causing issues, you can disable it using the pmu, or pm-uninstall command. Disabling a module can help reduce resource usage or troubleshoot conflicts without permanently deleting the module from your project.
lando drush pmu module_name
Update Drupal Core or Modules
The up, or update command allows you to update Drupal core or installed modules to the latest version. Keeping Drupal and its modules up-to-date helps protect against security vulnerabilities and includes improvements and new features. You can update Drupal core specifically with up drupal, or update all modules at once.
To update Drupal core:
lando drush up drupal
To update all modules:
lando drush up
Run Database Updates
After updating modules or Drupal core, you may need to apply changes to your database schema. The updb, or update database command runs any required database updates, ensuring your site’s database structure aligns with the updated code.
lando drush updb
Export Configuration
Drupal stores configuration, such as site settings and module configurations, in the database by default. To facilitate consistent setups across environments (e.g., development, staging, and production), Drupal allows configuration exports. The cex, or config-export command exports all configuration into files, which can then be versioned and shared.
lando drush cex
Import Configuration
To apply configuration changes from one environment to another (for example, from a development site to a live site), you can use the cim, or config-import command. This will import configuration settings from exported files into your database.
lando drush cim
Create a User
Creating new user accounts is often necessary for testing or granting access to collaborators. The ucrt, or user-create command creates a new user account with the specified username, email, and password, allowing you to quickly set up test accounts or grant access to others.
lando drush ucrt username --mail="user@example.com" --password="password"
Set or Reset a User Password
The upwd, or user-password command allows you to set or reset the password for a specified user account, which is particularly useful if you need to regain access to an account or create a known password for testing. Simply specify the username and the new password.
lando drush upwd username --password="newpassword"
Replace username with the account username you want to update and newpassword with the desired password.
Run Cron
Cron jobs in Drupal automate scheduled tasks like clearing logs, checking for updates, and running custom maintenance scripts. The cron command manually triggers these scheduled tasks, which can be useful if you need them to run immediately instead of waiting for the scheduled interval.
lando drush cron
Access Drush Documentation Topics
The topic command provides access to Drush’s built-in documentation topics, which offer detailed information on various Drush commands and usage. This command is particularly useful for learning more about Drush’s capabilities, exploring specific command options, or finding helpful examples. Running this command without any additional arguments will display a list of available topics. To view details on a particular topic, include the topic name.
lando drush topic
For example, to view detailed information about configuration management:
lando drush topic config
Drush is a valuable tool for Drupal developers, and pairing it with Lando makes local development more efficient. Drush will allow you to perform essential Drupal administration tasks right from the command line. Try incorporating these Drush commands into your daily workflow to see just how much time they can save you!