Drupal 9: Getting Setup Quickly With DrupalVM

11th October 2020 - 7 minutes read time

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

  1. # The absolute path to the root directory of the project. Both Drupal VM and
  2. # the config file need to be contained within this path.
  3. ENV['DRUPALVM_PROJECT_ROOT'] = "#{__dir__}"
  5. # The relative path from the project root to the config directory where you
  6. # placed your config.yml file.
  9. # The relative path from the project root to the directory where Drupal VM is located.
  10. ENV['DRUPALVM_DIR'] = "vendor/geerlingguy/drupal-vm"
  12. # Load the real Vagrantfile
  13. 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.

  1. # Set up some defaults for the box.
  2. vagrant_machine_name: drupalvmsetup
  3. vagrant_hostname: "{{ vagrant_machine_name }}.local"
  4. vagrant_ip:
  6. # Setup the vagrant synced folders.
  7. vagrant_synced_folders:
  8. - local_path: .
  9. destination: "/var/www/{{ vagrant_machine_name }}"
  10. type: nfs
  11. create: true
  12. drupal_core_path: "/var/www/{{ vagrant_machine_name }}/web"
  14. # Allow easy configuration of the machine resources.
  15. vagrant_memory: 2048
  16. vagrant_cpus: 2
  18. # The web server software to use. Can be either 'apache' or 'nginx'.
  19. drupalvm_webserver: apache
  21. # Turn off all automatic installer features.
  22. drupal_build_makefile: false
  23. drupal_build_composer: false
  24. drupal_build_composer_project: false
  25. drupal_install_site: false
  27. # Configure built in software.
  28. installed_extras:
  29. - adminer
  30. # - blackfire
  31. # - drupalconsole
  32. - drush
  33. # - elasticsearch
  34. # - java
  35. - mailhog
  36. # - memcached
  37. # - newrelic
  38. # - nodejs
  39. - pimpmylog
  40. # - redis
  41. # - ruby
  42. # - selenium
  43. # - solr
  44. # - tideways
  45. # - upload-progress
  46. - varnish
  47. # - xdebug
  48. # - xhprof # use `tideways` if you're installing PHP 7+
  50. # Configure PHP and xdebug.
  51. php_version: "7.4"
  52. php_xdebug_idekey: PHPSTORM
  53. php_xdebug_version: "2.9.6"
  54. php_xdebug_default_enable: 1

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.