Drupal 9: Getting Setup Quickly With DrupalVM

I like working with DrupalVM and I've worked with Ansible based Vagrant setups for years and so I'm very familiar with it's setup. More than that, I find I have very few problems with running it. I normally run it with Vagrant, but you can run it with Docker if you like.

When starting a new site project I normally add DrupalVM to the codebase so that I can get the site up and running quickly. This is especially useful if something like Solr is involved as setting that up is a pain. I thought I would go through the steps involved in adding DrupalVM to your codebase as it's pretty simple and will get you up and running with a Drupal site in about 10 minutes.

Start out with a Drupal site in a composer setup. I normally run the Drupal recommended composer setup file so that I have a up to date Drupal codebase, so let's do that here.

composer create-project drupal/recommended-project drupal_vm_setup

This will create your composer file and install the Drupal codebase along with a few other dependencies. For more information on what is in the recommended composer file see my previous blog post on the subject. After this step you can require any other Drupal based modules or projects that you want. Adding Drush at this point is usually a good idea.

composer require drush/drush

With that setup the next step is to require DrupalVM. We do this as a 'dev' dependency as we don't need to be running DrupalVM in production.

composer require --dev geerlingguy/drupal-vm

This doesn't do a lot on it's own, so before we provision the machine we need to add some configuration files.

The first thing we need to add to the project is a VagrantFile, which will allow Vagrant to function. Instead of a full copy of the DrupalVM VagrantFile we instead create a small proxy file that sets up the configuration directories and then loads in the VagrantFile from the  DrupalVM directory.

Copy the following into your VagrantFile. Note that in later versions of Vagrant this file is called Vagrantfile (without the capital F).

# The absolute path to the root directory of the project. Both Drupal VM and
# the config file need to be contained within this path.

# The relative path from the project root to the config directory where you
# placed your config.yml file.

# The relative path from the project root to the directory where Drupal VM is located.
ENV['DRUPALVM_DIR'] = "vendor/geerlingguy/drupal-vm"

# Load the real Vagrantfile
load "#{__dir__}/#{ENV['DRUPALVM_DIR']}/Vagrantfile"

As you can see above we have defined a configuration directory called "box". We could name this whatever we want, but I think it's important to give it a name very different from 'config' so that it doesn't get mixed up with the Drupal configuration directories.

In the box directory add a file called config.yml. This is the configuration file that DrupalVM will use to configure the box in the way you want.

What you put in this file can vary quite a lot. The default configuration file for DrupalVM is absolutely massive, and you do not need to copy all of those options to get your local machine working. Instead a sample cross section of those values to change some of the default and to expose some of the more frequently changeable settings is fine. The code below shows some sensible defaults to get you a machine setup correctly without trying to do extra things that you might not want (eg, installing Drupal after setting up the box).

Add the following to the file at box/config.yml.

# Set up some defaults for the box.
vagrant_machine_name: drupalvmsetup
vagrant_hostname: "{{ vagrant_machine_name }}.local"

# Setup the vagrant synced folders.
  - local_path: .
    destination: "/var/www/{{ vagrant_machine_name }}"
    type: nfs
    create: true
drupal_core_path: "/var/www/{{ vagrant_machine_name }}/web"

# Allow easy configuration of the machine resources.
vagrant_memory: 2048
vagrant_cpus: 2

# The web server software to use. Can be either 'apache' or 'nginx'.
drupalvm_webserver: apache

# Turn off all automatic installer features.
drupal_build_makefile: false
drupal_build_composer: false
drupal_build_composer_project: false
drupal_install_site: false

# Configure built in software.
  - adminer
  # - blackfire
  # - drupalconsole
  - drush
  # - elasticsearch
  # - java
  - mailhog
  # - memcached
  # - newrelic
  # - nodejs
  - pimpmylog
  # - redis
  # - ruby
  # - selenium
  # - solr
  # - tideways
  # - upload-progress
  - varnish
  # - xdebug
  # - xhprof # use `tideways` if you're installing PHP 7+

# Configure PHP and xdebug.
php_version: "7.4"
php_xdebug_idekey: PHPSTORM
php_xdebug_version: "2.9.6"
php_xdebug_default_enable: 1

Note: The inclusion of the specific version of Xdebug here is to get around an issue I had with PHPStorm not liking the newer version. You can probably leave this out.

With all that set up you can now run 'vagrant up' and DrupalVM will provision your machine for you.

Once complete you can visit http://dashboard.drupalvmsetup.local/ (or whatever URL you have added into your Vagrant file) and see the DrupalVM dashboard. From here you can visit you Drupal site and start getting Drupal installed properly.

You can also log into box using 'vagrant ssh', which will take you directly to the Drupal codebase and allow you to run Drush commands on the site.

From start to finish I can go from nothing to a fully installed Drupal site in around 10-15 minutes, although that depends on what speed your internet connection is. The good thing about this setup is that it creates a minimal setup in your codebase and can be added to your project git repo without any problems.

By the way, if you want to install Drupal 8 using the above method then the only thing you need to change is the initial version of the recommended-project setup.

composer create-project drupal/recommended-project:8.9.0 drupal_vm_setup

Add new comment

The content of this field is kept private and will not be shown publicly.
17 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.