Scriptable virtual environments with Vagrant

February 27th 2020

Got a question?

Please put your hand up at any point and I’ll take questions throughout


Alex Coleman


  • What is Vagrant?
  • Basic Vagrant commands
  • Vagrant case studies
    • Vagrant Ubuntu Desktop
    • Using host machine Github ssh keys
    • Building containers as root
    • Web app port forwarding
    • Accessing host files
  • How do I get it?
  • Further coding

What is Vagrant?

Is this a familiar conversation?

  • "I've got some really cool code, want a go?"

  • "Yes sure, send it to me!"

  • "Err, it's giving me an error?"

  • "Well, it worked on my machine..."

What is Vagrant?

  • Open source tool for building and managing virtual environments in a single workflow
  • Using a single file you can specify the exact environment required for your workflow
  • Works with VirtualBox, VMware and other providers
  • Runs on Windows, macOS and Linux

Basic Vagrant commands

  • Creating a virtual machine running Ubuntu 18.04 LTS 64-bit
            $ vagrant init hashicorp/bionic64
            $ vagrant up
            $ vagrant ssh


A single script to initialize a VM

            # Basic Vagrantfile
            Vagrant.configure("2") do |config|
     = "hashicorp/bionic64"

Vagrant boxes

  • Configuring and setting up virtual machines can be complicated and slow. Vagrant allows you bypass this by using existing base images called 'boxes'.
            $ vagrant box add continuumio/anaconda3


  • Set up what you want on your virtual box
  #!/usr/bin/env bash

  apt-get update
  apt-get install -y apache2
  if ! [ -L /var/www ]; then
    rm -rf /var/www
    ln -fs /vagrant /var/www
  # line in your Vagrantfile
  Vagrant.configure("2") do |config| = "hashicorp/bionic64"
    config.vm.provision :shell, path: ""

Cleaning up Vagrant

Switching off your Vagrant VM

            $ vagrant suspend

            $ vagrant halt

            $ vagrant destroy

Vagrant case studies

Test all this code out yourself using the vagrant_examples directory in the GitHub Repo for this presentation

Vagrant case studies

  • Get yourself a Ubuntu Desktop
            $ git clone
            $ cd vagrant-bionic-desktop
            $ vagrant up 2>&1 | tee log.txt
            $ vagrant reload

Vagrant case studies

  • Using your host ssh keys on a Vagrant box
      Vagrant.configure("2") do |config| = "hashicorp/bionic64"
        # line to forward active ssh agent from host to guest
        config.ssh.forward_agent = true
        config.vm.provision :shell, path: ""

Vagrant case studies

  • Accessing host machine files on Vagrant
                      $ vagrant up
                      $ vagrant ssh
                      vagrant@bionic64:~$ ls /vagrant

Vagrant case studies

  • So you've got a nice shiny/flask app you want to develop and test before putting it on a server?
    Vagrant.configure(2) do |config| = "hashicorp/bionic64"
      # run when vagrant sets up the VM
      config.vm.provision :shell, path: ""
      # networking through Vagrant :forwarded_port, guest: 4567, host: 4567

Vagrant case studies

  • Building Docker containers
        Vagrant.configure("2") do |config|
 = "pogosoftware/ubuntu-18.04-docker"
 "forwarded_port", guest: 8000, host: 8000
          config.vm.provision "shell", inline: <<-SHELL
          git clone

Getting Vagrant

  • IT Services can install Vagrant on your workstation via submitting an install request.

Submit an install request now!

Further coding/reading

Coming up

  • The next TechTalk (date TBC) will be on containerisation!

  • Join us for an informal chat with the Research Computing Team

  • Subscribe to the Research Computing mailing list by sending an email to with Subscribe as the subject

Please give us some feedback!