Even though not an essential tool I still like to use Drush when managing a site as it allows for quicker operations such as database updates, clearing or rebuilding the cache, and setting the state into maintenance in between running Composer commands on the command line without needing to switch to the Drupal GUI.
In this post we will look at how we can install Drush, then install the Drush Launcher so we can use it by typing
drush instead of
vendor/bin/drush, and then wrap up with a bunch of commands that are the most useful in everyday development.
On the Drush website it states that "It is required that Drupal sites be built using Composer" and thus to install Drush it is as easy as running
composer require drush/drush.
If you don't have Composer installed you will need to do that first. You can check how to do that on the installing Composer page.
Non Composer Managed Site?
I have got Drush working on a tarball installed site by running
composer require drush/drush. However, obviously Composer was installed on the server and when you do an update of vendor files, which is every minor release, the Drush package needs to be added back. Whether this really makes sense I still don't know, the jury is still out on this mainly due to the fact I mainly use Composer to maintain Drupal 9 sites.
Once installed you can then run Drush by using
If you run the below command you will see the status, you should see Drush 10. I have included all the most used Drush commands at the end of the page.
Having to type
vendor/bin/drush each time is a bit of a hassle so we will fix that.
Run Drush with shortened command
To use Drush only using
drush from within the root directory of your Drupal site you need to install the Drush Launcher. Drush Launcher listens on the system $PATH and when Drush is run the launcher passes the control to the site-specific Drush located in the vendor directory. Drush Launcher is hosted on Github and has everything thing you need to get going however I will go over it here for Linux.
For OSX or Windows please visit the Github page as the commands and steps differ ever so little. For OSX it is suggested you use curl and for Windows there is a small snippet of code you need to add to work correctly.
To install follow these 3 steps
- Grab the URL from the fore-mentioned page and run it in the terminal on the server you want to use it.
wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/latest/download/drush.phar
- Change perms on drush.phar
chmod +x drush.phar
- Move into $PATH and rename
sudo mv drush.phar /usr/local/bin/drush
What is a .phar?
A PHAR (PHP Archive) file is a package format to enable distribution of applications and libraries by bundling many PHP code files and other resources (e.g. images, stylesheets, etc.) into a single archive file. https://en.wikipedia.org/wiki/PHAR_(file_format)
So with that done, you can now run Drush from within you site root directory using
Useful day to day Drush commands
Following are some useful commands that I find I use a lot. To see them in use with other tools such as CLI or Composer check out the Update Drupal Core article or the Update Drupal Modules article.
drush state:set system.maintenance_mode 1 drush state:set system.maintenance_mode 0
drush updatedb:status drush updatedb drush cache:rebuild
Well, that's is about it for Drush, a useful command line tools that can save you time by allowing you to run basic maintenance tasks. I haven't used it past the basic tasks listed above but still find it something I couldn't do without.
Thanks for reading and be sure to sign up for the newsletter if you are interested in front-end development, design, and of course using Drupal as a headless CMS.