Drush (http://www.drush.org/en/master/) is a command-line shell and Unix-scripting interface that allows us to interact with Drupal. Drush gives us the ability to use the command line to accomplish tasks quickly, without the need to rely on the Drupal admin UI. As part of the composer install, our project has the latest version of Drush installed automatically.
Executing a Drush command is typically as easy as typing the word drush
within a Terminal window.
However, the challenge of having a per-project instance of Drush is in the way we are forced to currently execute Drush commands. Since the drush
executable is located within the projects /vendor/bin/drush
folder, if we are within the root of our project, we execute drush by entering the following within the Terminal window:
./vendor/bin/drush
The problem is the path can easily change; if, for instance, we are in the /web
root, the same command would be:
../vendor/bin/drush
Notice the two dots indicating one must traverse up a level to locate the /vendor
folder.
This is not ideal when we will be using Drush quite frequently to perform various tasks. We can resolve this in a couple of different ways.
The first is to use drush.wrapper
located within the /vendor/drush/drush/examples
folder. This file is a wrapper script that launches Drush within a project. If we open the file within an editor, we will see that it states we need to copy the file to our /web
folder and rename it to drush.
Choosing to follow this method would then allow us from within the /web
folder to execute drush commands by entering the following within our Terminal window:
./drush
This is a little better; however, this is not quite as nice as simply typing the word drush
without the need to know how to run a script. We can accomplish this by globally installing Drush using Composer.
Installing Drush globally varies based on the operating system or AMP stack, as there is a dependency on PHP 5.5.9 or higher. This dependency will be satisfied in most cases, but ensure that you verify the version of PHP that is available.
Begin by opening the Terminal window, changing into the user directory, and executing the following commands:
- Verify that Composer is installed:
composer
- Add Composer's bin directory to the system path:
export PATH="$HOME/.composer/vendor/bin:$PATH"
- Install the latest stable release:
composer global require drush/drush
- Verify that Drush works:
drush status
- Now that Drush has been installed globally, we can easily ensure that we always have the latest version by running this:
composer global update
- To get our first look at the available commands that Drush provides, we can execute the following:
drush
The list of Drush commands is quite long, but it does provide us with the ability to perform almost any action we may need when working on a Drupal project. Some simple commands that we will commonly use throughout the book are clearing cache, managing configurations, and even installing Drupal. For a list of all the various commands, we can browse Drush Commands at https://drushcommands.com/.
Some common uses of Drush are to download modules, themes, and even Drupal itself. The command to execute this task is drush dl
. Since we previously installed Drush globally, we can change to a brand-new directory using the Terminal window, and download another copy of Drupal by executing the following command:
drush dl drupal
As we can see from the preceding screenshot, executing the command downloads the current version of Drupal. We can verify this by listing the contents of the current directory:
Now that we have a second copy of Drupal, we can use Drush to perform a quick install.
Note
Note that, to use Drush to install Drupal without setting up or configuring an instance of an *AMP stack, we will need to at least have PHP 5.5.9 or higher installed.
Within a Terminal window, change into the drupal-8.x
directory that Drush downloaded and execute the following command:
drush qd --use-existing --uri=http://localhost:8383 --profile=standard
This command tells Drush to perform a quick Drupal installation using the existing source files. Drupal will use the standard profile and, once the installation has completed, a PHP server will be started on localhost port 8383
.
Ensure that you specify that you want to continue with the installation when prompted. Once the Drupal installation has finished, a browser window will open on the admin user page with the one-time login where we can then create a new password:
We will not be using this instance of Drupal, so we can terminate the PHP server that is currently running in the Terminal window by entering Ctrl + C on the keyboard.
Hopefully, we can begin to see how using Drush can speed up common tasks. Throughout each lesson, we will explore Drush in more detail and utilize additional commands. Now that we have a better understanding of Drush, it's time to take a look at another command-line tool that we can benefit from using when developing a Drupal website.