Skip to content

Vagrant boxes and the Vagrantfile — reproducible dev environments

A practical look at Vagrant boxes, providers, and the Vagrantfile patterns that hold up beyond a single laptop — multi-machine, provisioning, and box hygiene.

Vagrant wraps a virtualization provider (VirtualBox, libvirt, Hyper-V, Docker) behind a single CLI and a config file. Its sweet spot is reproducible developer environments — a developer clones a repo, runs vagrant up, and gets the same VM as everyone else on the team. This article covers what a Vagrant box is, how the Vagrantfile holds together at the team scale, and the operational patterns that prevent box drift.

How to verify

Confirm Vagrant and its provider are installed, the provider can spawn a VM, and Vagrant can find the box you’ll use.

vagrant --version
vagrant plugin list
vboxmanage --version || virsh --version
vagrant box list
vagrant global-status     # all VMs Vagrant knows about

For Linux operators, libvirt is the better provider — install vagrant-libvirt plugin (vagrant plugin install vagrant-libvirt). For macOS, VirtualBox is the path of least resistance until Apple Silicon, where you switch to qemu via vagrant-qemu.

What’s happening

A box is a tarball containing a disk image plus metadata — VM type (virtualbox, libvirt, qemu), default username, and a public SSH key Vagrant uses to log in on first boot. Boxes are published to Vagrant Cloud (the registry at app.vagrantup.com) or fetched from a private HTTP server. The first vagrant up downloads the box; subsequent vagrant ups clone the box’s disk and boot a fresh VM.

A Vagrantfile is a Ruby file that defines one or more VMs. It tells Vagrant which box to use, what provider, how to forward ports, how to share folders with the host, and what provisioner runs after first boot (shell, Ansible, Chef, Puppet). The Vagrantfile is committed to the repo; the boxes are not. Anyone with Vagrant installed can reproduce the environment.

The procedure

  1. Pick a provider and box that match. A box built for VirtualBox does not work with libvirt; the registry tags boxes by provider. The “ubuntu/jammy64” box has both VirtualBox and libvirt variants — Vagrant picks the one matching your provider:

    vagrant box add bento/ubuntu-22.04 --provider=libvirt
    vagrant box list

    Prefer the bento/ images for libvirt — they’re better-maintained than the official ubuntu/ ones for non-VirtualBox providers.

  2. The minimal Vagrantfile. Three lines plus a provider block:

    Vagrant.configure("2") do |config|
      config.vm.box = "bento/ubuntu-22.04"
      config.vm.hostname = "dev01"
      config.vm.network "private_network", ip: "192.168.56.10"
    
      config.vm.provider :libvirt do |libvirt|
        libvirt.memory = 2048
        libvirt.cpus = 2
      end
    
      config.vm.provision "shell", inline: <<-SHELL
        apt-get update
        apt-get install -y nginx
      SHELL
    end
  3. Multi-machine — the pattern that gets real. Two or three VMs in the same Vagrantfile, each defined inside a config.vm.define block. This is how you reproduce a small cluster on a laptop:

    Vagrant.configure("2") do |config|
      %w[web01 web02 db01].each do |name|
        config.vm.define name do |node|
          node.vm.box = "bento/ubuntu-22.04"
          node.vm.hostname = name
          node.vm.network "private_network", ip: "192.168.56.#{10 + %w[web01 web02 db01].index(name)}"
          node.vm.provider :libvirt do |libvirt|
            libvirt.memory = 1024
            libvirt.cpus = 1
          end
        end
      end
    end
  4. Provisioning — shell for quick, Ansible for real. Inline shell is fine for apt-get install nginx; anything serious goes through an Ansible playbook so the same provisioning runs in dev (via Vagrant) and prod (via direct Ansible):

    config.vm.provision "ansible" do |ansible|
      ansible.playbook = "ansible/site.yml"
      ansible.inventory_path = "ansible/inventory.dev"
    end
  5. Synced folders — performance gotchas. Vagrant’s default rsync syncs are one-way (host → guest, on demand). NFS is bidirectional but needs root on the host the first time. virtfs-9p is a third option for libvirt. Choose explicitly; the defaults vary by provider and bite when you assume:

    config.vm.synced_folder ".", "/vagrant", type: "rsync",
      rsync__exclude: [".git/", "node_modules/"]
  6. Box hygiene — pin versions, rebuild periodically. A Vagrantfile that just says config.vm.box = "ubuntu/jammy64" gets whatever is “latest” today, which is fine for trying things and disaster for reproducibility. Pin the version:

    config.vm.box_version = "20240101.0.0"

    Build your own boxes with Packer (see Packer image builds) for anything that needs to outlive a single team’s tenure.

  7. Common lifecycle. vagrant up boots, vagrant ssh enters, vagrant halt stops, vagrant destroy -f removes the disk, vagrant reload --provision re-runs provisioning. vagrant package exports a running VM as a new box for redistribution.

Common pitfalls

  • A private_network IP in the wrong subnet for your provider’s host-only network breaks SSH to the VM with a confusing “Authentication failure” — fix the host-only network in VirtualBox or the libvirt network range.
  • vagrant up failing with “Box not found” after a vagrant destroy usually means the box version was unpinned and the upstream changed — pin it.
  • Shared folders on macOS hosts running VirtualBox can be extremely slow for many small files (e.g. Node.js node_modules) — use rsync synced folders and exclude the heavy directories.
  • Provisioners only run on the first vagrant up unless you pass --provision to subsequent ones — running vagrant reload does not re-provision by default.
  • vagrant global-status showing entries for destroyed VMs means the cache is stale; vagrant global-status --prune cleans it up.

Stack Harbor uses Vagrant in client environments where developer onboarding requires a non-trivial local stack — the Vagrantfile lives in the application repo, the boxes are built by us via Packer and hosted on a private box server, and any change to the dev environment is a PR. Clients whose dev environment is “set up your laptop by reading the wiki for two days” get that fixed as part of Managed Operations.