Drupal 9: Getting Setup Quickly With DrupalVM

11th October 2020

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.

  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__}"
  4.  
  5. # The relative path from the project root to the config directory where you
  6. # placed your config.yml file.
  7. ENV['DRUPALVM_CONFIG_DIR'] = "box"
  8.  
  9. # The relative path from the project root to the directory where Drupal VM is located.
  10. ENV['DRUPALVM_DIR'] = "vendor/geerlingguy/drupal-vm"
  11.  
  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).

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

Add new comment

The content of this field is kept private and will not be shown publicly.