Skip to main content
info@drupalodyssey.com
Thursday, June 5, 2025
Contact

Main navigation

  • Home
  • Blog
    • all
    • Development
    • Community
    • Management
    Aluminum Cans Passing Through the Assembly Line by cottonbro studio on Pexels
    Automate and Simplify Your Drupal Workflow with Bash Scripts for Shared Hosting
    Jul 19, 2024
    Binoculars resting on newspapers.
    Evaluating Search and Replace Scanner: The Ultimate Tool for Drupal Bulk Content Edits?
    Jun 29, 2024
    People looking at a computer screen
    S3 File System Module Not Working with Media Entity Download Module? Here's the Fix
    Jun 18, 2024
    Mechanic hands working on an engine.
    Setting Up the Etsy OAuth2 Client For Use With The Etsy Shop Integration Module
    May 10, 2024
    Fashion designer sketching new garments.
    Crafting Your Online Store: Drupal's Role in Your Etsy Success
    May 09, 2024
    Socket toolbox
    Beginner's Guide: Getting Started With Drush for Efficient Drupal Development
    May 08, 2024
    Stargazing over mountians.
    Drupal-Powered Stargazing: A Module for NASA's Astronomy Picture of the Day
    Sep 15, 2023
    Computer screen with code.
    Learn How To Script Drupal Installations Using Drush
    Dec 08, 2014
    Scuba diver with Drupal mask.
    Scuba: Drupal Style
    Oct 16, 2014
    Woman frustrated with laptop.
    5 Reasons Your CMS Sucks
    Jul 24, 2013
    Two young men having a discussion in front of a computer.
    Deployment Module XSRF Patch Committed
    Jul 05, 2013
    Two young men having a discussion in front of a computer.
    Deployment Module XSRF Patch Committed
    Jul 05, 2013
    Application settings.
    Using PHP To Disable Internet Explorer Compatibility Mode
    Jun 04, 2013
  • Resources
  • About
  • SPACER
  • SPACER
  • SPACER
  • SPACER
  • SPACER
Search
Development

Beginner's Guide: Getting Started With Drush for Efficient Drupal Development

May 08, 2024

Drupal 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.

Author

Ron Ferguson

Previous Blog

Next Blog

0 Comments

Login or Register to post comments.

Categories

Categories

  • Development
    (8)
  • Community
    (9)
  • Management
    (3)

Trending Blog

Trending Blog

Woman frustrated with laptop.
5 Reasons Your CMS Sucks
24 Jul, 2013
Mechanic hands working on an engine.
Setting Up the Etsy OAuth2 Client For Use With The Etsy Shop Integration Module
10 May, 2024
Stargazing over mountians.
Drupal-Powered Stargazing: A Module for NASA's Astronomy Picture of the Day
15 Sep, 2023
People looking at a computer screen
S3 File System Module Not Working with Media Entity Download Module? Here's the Fix
18 Jun, 2024
Computer screen with code.
Learn How To Script Drupal Installations Using Drush
08 Dec, 2014

Tags

Tags

  • Drupal 10
  • Drupal 9
  • Drupal 8
  • Drupal 7
  • Drush

Ad - Sidebar (300 x 250 AD)

Ad - Sidebar (300 x 600 AD)

Newsletter

Subscribe my Newsletter for new blog & tips Let’s stay updated!

Categories

  • Development
  • Community
  • Management

Useful Links

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer
  • Cookies

Must Read

Aluminum Cans Passing Through the Assembly Line by cottonbro studio on Pexels
Automate and Simplify Your Drupal Workflow with Bash Scripts for Shared Hosting
19 Jul, 2024
Binoculars resting on newspapers.
Evaluating Search and Replace Scanner: The Ultimate Tool for Drupal Bulk Content Edits?
29 Jun, 2024
Mechanic hands working on an engine.
Setting Up the Etsy OAuth2 Client For Use With The Etsy Shop Integration Module
10 May, 2024
Socket toolbox
Beginner's Guide: Getting Started With Drush for Efficient Drupal Development
08 May, 2024

© 2024 All Rights Reserved.

Proud supporter of active military, veterans and first responders.