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-libvirtplugin. - Vagrant installed from the official HashiCorp downloads or your package manager. Check its current licence terms.
Create a project
mkdir myapp-vm && cd myapp-vmvagrant init bento/ubuntu-24.04This 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
endStart and use the VM
vagrant upvagrant sshOpen http://localhost:8080 to reach Nginx in the VM. Your project folder is available inside at /vagrant.
Everyday commands
| Command | Action |
|---|---|
vagrant halt | Shut down the VM |
vagrant reload --provision | Restart and re-run provisioning |
vagrant snapshot save clean | Save a snapshot |
vagrant destroy -f | Delete the VM completely |
vagrant status | Show state |
Use libvirt/KVM on Linux
vagrant plugin install vagrant-libvirtvagrant up --provider=libvirtReplace 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.
