Disposable Drupal installations with drush

Submitted by on

Sometimes we need a Drupal installation for a quick test of a module, theme, or feature that we are working on. You can have a LAMP stack installed locally or, as we do at Agaric, use virtual machines. In both cases, it can take a considerable amount of time to make the required configuration to install Drupal. It is possible to avoid all that by leveraging Drupal’s support for SQLite and using PHP’s built in web server. Let me show you how to easily create a disposable installation with a few drush commands.

During BADCamp 2015 sprints, some developers and myself joined Jesús Manuel Olivas to work on Drupal Console. It is an amazing project that allows you to speed up your Drupal 8 development by generating scaffolding code. Some Console commands require a Drupal installation to work. I wanted to contribute to the project so I used drush to download and install Drupal in seconds. For instructions on installing drush, check this great article by Karen Stevenson.

1. Download Drupal

drush pm-download --drupal-project-rename=drupal drupal-8.0.x

This will download the latest development version of Drupal 8.0 and place it in the directory specified by the --drupal-project-rename parameter. In our case, the directory name will be called drupal. If you omit this parameter, the files will be downloaded to a directory with the Drupal version number in it.

2. Change directory and install Drupal

cd drupal
drush site-install standard --db-url=sqlite:///tmp/test.db

Once we have downloaded the code, we change directories to the Drupal root and issue the installation command. standard is the installation profile to be used. The --db-url parameter allows us to specify the database to connect to. Usually this would include the credentials for MySQL or your favorite database server. In our case, we are going to use SQLite which is a self-contained database engine. To use it, we only need to precede with sqlite:// a file path on our system where the database will be created. In this example, that path is /tmp/test.db

3. Start PHP’s built in server

drush runserver

Starting in version 5.4.0, PHP includes a lightweight web server. The previous command will start the server listening by default on 127.0.0.1:8888. Paste that in your browser’s address and enjoy a fully working Drupal installation in front of you. If you got a different IP address or port number use that instead. The server will keep listening and even output logs as you interact with the site. When you are done testing, press Ctrl + C to stop the web server.

4. Clean up

rm /tmp/test.db
cd ..
rm -rf drupal

Finally, do some clean up. Delete the database file. If you set up the database in /tmp you might not need to manually delete it. You should also remove the Drupal directory. Everything will be gone in a snap.

The complete recipe is:

# Download Drupal
drush pm-download --drupal-project-rename=drupal drupal-8.0.x

# Change directory and install Drupal
cd drupal
drush site-install standard --db-url=sqlite:///tmp/test.db

# Start PHP’s built in server. Press Ctrl + C to stop the web server.
drush runserver

# Clean up.
rm /tmp/test.db
cd ..
rm -rf drupal

A shourtcut

drush core-quick-drupal --core=drupal-8.0.x --profile=standard drupal

The previous drush command will download Drupal, install it using SQLite, start PHP's built in server, and even open a browser windows with user 1 already logged in. The command exposes lots of parameter to configure the Drupal installation. In our example, we are downloading version 8.0.x, place it in a directory called drupal, and use the standard installation profile. All the other parameters would use the defaults. For a list of available parameters and arguments issue the following command in the terminal drush help core-quick-drupal.

My motivation for following this approach was that I needed to write and test code when sprinting on Drupal Console. If you do not need to modify code, Patrick Drotleff has built an amazing website that allows you to create temporary Drupal installations with support for modules, themes, distributions, patches, and more. To use it, visit https://simplytest.me

Drupal Console not only generates scaffolding code. It also lets you interact with your Drupal installation in similar ways to Drush. For example, it lets you download and install Drupal using a MySQL database. Notwithstanding, it is not possible at the moment to install Drupal using SQLite or start the PHP’s built in server. Jesús Manuel Olivas, one of the project maintainers, said that these options would be available in future versions of Drupal Console.

What do you think about this method for creating disposable Drupal installations? Do you follow a different approach? Let me know in the comments.

Add new comment