Beginner's Guide: Getting Started With Drush for Efficient Drupal Development
May 08, 2024Drupal Odyssey is supported by it's readers. When you purchase products or services using the links on this site, we may earn a small commission at no additional cost to you. Learn more
Let's face it, Drupal site administration is a PITA. Especially for trivial tasks such as resetting user passwords, clearing stale or corrupt caches, exporting configuration, and many other tasks. While all of these CAN be done in the browser, it means you have to pause your current workflow to log in to the site then navigate to the administration page to complete the task that you need then switch back to what you were doing before.
There is a better way.
What is Drush?
Drush a Unix command line utility meant for Drupal developers and site administrators. For developers, it contains commands to generate entities, controllers, various plugin types, and many other developer tasks. For site administrators it has commands for generating user one time login links, clearing caches and interacting with the watchdog logging.
History
Drush was originally developed for Drupal 4.7 by Arto Bendiken. It was partially rewritten in 2007 by Franz Heinzmann for Drupal 5. Drush was originally implemented as a Drupal module which limited what was possible to do with the tool. Drush 3 saw the move from a module to a stand-alone project outside of the Drupal ecosystem which was, in my opinion, a pivotal point in its history which allowed for the maintainers and the community as a whole to begin adding the long list of additional actions that users may want.
When Drupal overhauled the architecture to be Symfony-based in Drupal 8, some developers questioned Drush's status with the introduction to Drupal Console since they both did the same things but in different ways. The edge that Drupal Console had over Drush was its ability to generate boilerplate code for new entity types, controllers, and other features. In the latest versions of Drush (version 10.2.0 and newer), generating boilerplate code is now available.
Installation
Installation is seriously straight forward as it can be if a site is managed by Composer. You simply need to run the following composer command to install the latest version of Drush.
composer require drush/drush
If you want to install a specific version of Drush, you can alter the composer command to specify the version like the following.
composer require "drush/drush:^{version_tag}"
Setup
Out of the box, the drush command just works with no additional setup. However, there may be some instances where you need to override the defaults that are specific to your use case such as the PHP version, adding system paths where your Drush commands or site aliases are located.
The easiest way to do this is to create a folder named "drush" in the root of your project or web directory. Then in the new folder, add a new drush.yml file. See the Drush documentation for more information and configuration options available.
NOTE - Since Drush is installed via composer, the command is not in the system path so to run the commands, you will need to be in the root of your site and reference the relative path to the actual drush command itself which is vendor/bin/drush.
Managing Remote Sites With Drush
One of the biggest features, in my opinion, is managing remote site environments such as the dev, staging, and live environments. To do this you must first create the site alias file using the following command. (For all options available, see the complete documentation for the command.)
vendor/bin/drush generate drush:alias-file --destination=drush/sites/self.site.yml
Once your alias file is generated, you can add options for each environment that you wish to be able to remotely manage.
Each top-level key in the YAML file is the name by which you'll reference the environment using the drush command. Take the following self.site.yml file for example.
local:
root: /home/user/public_html
uri: http://local.example.com
stage:
host: stage.domain.com
user: www-admin
root: /other/path/to/drupal
uri: http://stage.example.com
ssh:
options: '-p 100'
live:
host: www.domain.com
user: www-admin
root: /other/path/to/drupal
uri: http://example.com
ssh:
options: '-p 100'
The previous alias file defines three environments for the current site; local dev, staging/testing, and live/production. For both the stage and live environments, you will need to previously set up passwordless ssh login and replace the user keys in the YAML file with your appropriate with your user.
After creating the alias file, you should rebuild the caches with the following command.
vendor/bin/drush cr
After the caches are cleared, you should be able to run any command on the environment that you choose. For example, to rebuild the caches on the live site, you would run the following command.
vendor/bin/drush @self.live cr
Conclusion
The Drush command line utility is indispensable to developers and site administrators. There are way too many commands to go into detail here, but all of the default commands are listed in the official Drush documentation. There are also several community modules that have implemented their own specific commands to make working with those modules easier. In a later blog post, I'll cover the topic of writing your own Drush commands for your module.
0 Comments
Login or Register to post comments.