Docker & DevOps

Vagrant: reproducible development VMs with VirtualBox or libvirt

Use Vagrant to create disposable development VMs from a Vagrantfile: install it, choose a provider and box, forward ports, share folders, provision with a shell script and tear down.

BeginnerDeveloper workstations2 min read

Vagrant describes a virtual machine in a small text file, the Vagrantfile, so every developer gets the same environment with one command. It drives hypervisors like VirtualBox or libvirt/KVM. This guide covers getting started with Vagrant development VMs.

Before you start

  • A workstation with a supported hypervisor: VirtualBox (see VirtualBox lab VM) or libvirt/KVM on Linux with the vagrant-libvirt plugin.
  • Vagrant installed from the official HashiCorp downloads or your package manager. Check its current licence terms.

Create a project

mkdir myapp-vm && cd myapp-vm
vagrant init bento/ubuntu-24.04

This creates a Vagrantfile using a community box. Pick boxes from trusted publishers and check they support your provider.

Edit the Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-24.04"
  config.vm.hostname = "myapp-dev"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder ".", "/vagrant"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048
    vb.cpus = 2
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y nginx
  SHELL
end

Start and use the VM

vagrant up
vagrant ssh

Open http://localhost:8080 to reach Nginx in the VM. Your project folder is available inside at /vagrant.

Everyday commands

CommandAction
vagrant haltShut down the VM
vagrant reload --provisionRestart and re-run provisioning
vagrant snapshot save cleanSave a snapshot
vagrant destroy -fDelete the VM completely
vagrant statusShow state

Use libvirt/KVM on Linux

vagrant plugin install vagrant-libvirt
vagrant up --provider=libvirt

Replace the virtualbox provider block with a libvirt block for memory and CPU. Requires KVM; see install KVM on Ubuntu.

Provision with real tools

Instead of inline shell, point Vagrant at an Ansible playbook (config.vm.provision "ansible"), the same one you use for servers; see Ansible for VPS configuration.

Troubleshooting

  • Box not available for provider: choose a box that lists your provider.
  • Port 8080 already in use: change the host port or add auto_correct: true.
  • Synced folder errors: install guest additions (VirtualBox) or use rsync/NFS synced folders.

Frequently asked questions

Vagrant or Docker for development?

Docker is lighter for app stacks; Vagrant is better when you need a full VM with its own kernel, init system or OS.

Can Vagrant create servers in the cloud?

Some providers exist, but Terraform is the usual tool for that; see Terraform basics.

Does Vagrant work on Apple silicon?

Yes with ARM64 boxes and a supporting provider.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.