Vagrant development environment setup with VDD on MacOSX

Submitted by christophe on Thu, 12/03/2015 - 21:40
Vagrant logo

After spending some time configuring Yosemite to end up in a poor result (slow server, MySQL crashing repeatedly), I decided have a look at this Vagrant stuff.
I was astonished how easy it was to get up and running with it.

Vagrant is a VM based development environment that automates current actions for a vhost (database creation, Apache entries, ...) and provides shared directories between the host system (here, MacOSX) and the VM. Starting points are called boxes (ready to user VM's) and can be found on sites such as http://www.vagrantbox.es/ or https://atlas.hashicorp.com/boxes/search

A Drupal ready environment is provided by the community, it comes out of the box with

  • A LAMP stack (Ubuntu)
  • Drush (with automatic aliases)
  • Xdebug
  • Webgrind (Xdebug profiling)
  • Mailcatcher (that is preconfigured out of the box to intercept every outgoing email, nice)
     

Hereunder, a shortcut for the documentation based on https://www.drupal.org/node/2008792

Vagrant setup

  1. Download VirtualBox https://www.virtualbox.org/wiki/Downloads
  2. Download Vagrant http://www.vagrantup.com/downloads.html
  3. Download https://www.drupal.org/project/vdd
  4. Expand VDD in home directory. Example: one version on vdd_test, for quick module review / another on vdd_sites as a replacement of ~/Sites driven by Mac OSX Apache install.
  5. Edit the config.json on vdd_sites with the website details
  6. On the vdd_sites directory, run vagrant up : will build your vhosts, databases, drush aliases
  7. Once done, vagrant ssh to open a SSH session on your fresh VM
  8. The first thing to do is to setup NFS on the VM : sudo apt-get install nfs-kernel-server, that will enable the NFS protocol between MacOSX and the VM (much faster thant the default configuration)
  9. Change the config.js : under "synced_folders""type": "default" become "type": "nfs".
  10. Run vagrant reload under vdd_sites (on MacOSX)
  11. Visit http://192.168.44.44/
  12. Copy the values dedicated to the MacOSX /etc/hosts files
  13. Git clone in the current directory : cd into sites/my_site then git clone https://my.git.repo . (do not forget the trailing period)
  14. Code happily

If you have to rebuild (vagrant destroy then vagrant up - removes all your databases, keeps files and recreates the environment), keep in mind to change back type = "default" (from "nfs") before launching vagrant up, otherwise, it will cause errors (no more nfs packages installed on your VM).

If you encounter the PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away, your database probably needs more ressources. Modify your MySQL configuration file

sudo nano /etc/mysql/my.cnf and append the following under the [mysqld] section : max_allowed_packet = 32M save and relaunch MySQL sudo service mysql restart

Time to insall Drupal now !

Inside ~/sites/my_site (defined in config.json)

Drupal 8

cd in your docroot

git clone --branch 8.0.x http://git.drupal.org/project/drupal.git .
drush @my_site si standard -y

Drupal 7

remove the docroot directory

drush dl drupal --drupal-project-rename=docroot
drush @my_site si standard -y

You can also continue by configuring PHP Storm for XDebug, use your GitHub GUI by adding the cloned repo, ...

Apply changes in config.json

The documentation talks about vagrant reload --provision I always got this error:
Shared folders that Chef requires are missing on the virtual machine. This is usually due to configuration changing after already booting the machine. The fix is to run a `vagrant reload` so that the proper shared folders will be prepared and mounted on the VM.
To get rid of this and apply changes:rm .vagrant/machines/default/virtualbox/synced_folders
vagrant reload --provision

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.