Using Vagrant to simplify building Virtual Machines

Oracle’s VirtualBox software is a key tool in software and website development, but can be complicated to configure. Vagrant simplifies the process and enables developers to repeatably build and scrap near-identical Virtual Machines (VM). This post will create a Ubuntu 18.04 Virtual Machine with a local directory mounted on it to make it easier to code on.

If you’re new to Virtual Machines, please read How to manually build a Ubuntu 18.04 Virtual Machine server with Oracle VirtualBox about how to achieve the same goal, except manually.


1. Create the directories to work in

Start by creating a directory to work in and then move into it and create a directory to mount in the VM itself.

$ mkdir ~/Desktop/ubuntu-18-04
$ cd ~/Desktop/ubuntu-18-04
$ mkdir source

2. Initialise Vagrant with the Ubuntu 18.04 image

$ vagrant init generic/ubuntu1804

This will have created a file in your directory called Vagrantfile – this is where we write the configuration for the new VM.

This file should start with Vagrant.configure("2") do |config| and then finish with end. Everything in between these lines defines the VM’s configuration options.

By default there’ll only be one configuration line (the box to use – Ubuntu 18.04) and a lot of comments which are worth reading through to see what’s possible with Vagrant.

3. Configure the VM in VirtualBox

First, lets set the name, memory and CPU settings for the VM in VirtualBox:

config.vm.provider "virtualbox" do |vb| = "ubuntu-18-04"
  vb.memory = "1024"
  vb.cpus = "2"

Earlier we created a directory called source, so now we will tell Vagrant to mount it at /source:

config.vm.synced_folder "./source", "/source"

4. Test!

Launch the VM:

$ vagrant up

Vagrant will now download the base box image, which can take quite a while. Once its downloaded, Vagrant will finish off creating the VM with the configuration defined above. Now we need to SSH on to the VM:

$ vagrant ssh

This will SSH you onto the VM so you can have a rummage around to check everything is configured as required. Check in the root directory (/) for a directory called source. You should be able to now use that directory in your laptop’s code editor of choice to start writing code on the VM!

5. Additional available commands

Vagrant has a number of simple commands to control the VM:

# Stop the VM
$ vagrant halt

# Reboot
$ vagrant reload

# Scrap
$ vagrant destroy

When running the destroy command, the contents of the mounted directory will not be deleted. This makes scrapping and rebuilding the VM incredibly easy as you do not need to worry about loosing your work!

The Vagrantfile can also be setup to run a set of commands on startup. The following will update and upgrade the OS software and then install PHP 7.2!

config.vm.provision "shell", inline: <<-SHELL
  sudo apt-get update -y
  sudo apt-get dist-upgrade -y
  sudo apt-get upgrade -y

  sudo add-apt-repository ppa:ondrej/php -y
  sudo apt-get update -y
  sudo apt-get install -y php7.2