Vagrant Cheat Sheet
Reference for Vagrantfile syntax and the vagrant CLI for creating and managing reproducible development VMs.
Basic Vagrantfile
Minimal configuration provisioning a VM with a shell script.
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/jammy64" config.vm.hostname = "dev-box" config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "private_network", ip: "192.168.56.10" 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 SHELLend
Vagrant CLI
Common commands for managing the VM lifecycle.
vagrant init ubuntu/jammy64 # Create a Vagrantfilevagrant up # Create and start the VMvagrant ssh # SSH into the VMvagrant halt # Stop the VMvagrant suspend / resume # Pause / resume the VMvagrant destroy # Remove the VM entirelyvagrant reload --provision # Restart and re-run provisionersvagrant status # Show VM state
Key Concepts
Core Vagrant building blocks.
- box- Base VM image/template that Vagrant clones to create new machines
- provider- Backend virtualization engine (virtualbox, vmware, docker, hyperv)
- provisioner- Tool used to configure the VM after boot (shell, ansible, chef, puppet)
- synced folder- Directory shared between host and guest, mounted automatically
- Vagrantfile- Ruby DSL file declaring the machine's configuration
Multi-Machine Setup
Define and independently configure multiple VMs from a single Vagrantfile.
Vagrant.configure("2") do |config| config.vm.define "web" do |web| web.vm.box = "ubuntu/jammy64" web.vm.hostname = "web" web.vm.network "private_network", ip: "192.168.56.11" web.vm.provision "shell", path: "provision/web.sh" end config.vm.define "db" do |db| db.vm.box = "ubuntu/jammy64" db.vm.hostname = "db" db.vm.network "private_network", ip: "192.168.56.12" db.vm.provider "virtualbox" do |vb| vb.memory = 4096 vb.cpus = 2 end endend# vagrant up web -- bring up a single machine# vagrant ssh db -- SSH into a specific machine
Box & Snapshot Management
Advanced CLI commands for managing box versions and VM state snapshots.
vagrant box list # List installed boxes and versionsvagrant box outdated # Check if current box has an updatevagrant box update # Update box(es) used by this Vagrantfilevagrant box remove ubuntu/jammy64 --box-version 20240101.0.0vagrant package --output custom.box --base web_1 # Package a running VM into a reusable boxvagrant snapshot save pre-upgrade # Save current disk/memory statevagrant snapshot list # List saved snapshotsvagrant snapshot restore pre-upgrade # Roll back to a snapshotvagrant snapshot delete pre-upgrade # Remove a snapshot
Triggers
Run host- or guest-side commands before/after Vagrant actions such as up or destroy.
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/jammy64" config.trigger.before :up do |trigger| trigger.info = "Starting VM..." trigger.run = { inline: "echo 'pre-up hook on host'" } end config.trigger.after :destroy do |trigger| trigger.info = "Cleaning up local build cache" trigger.run = { inline: "rm -rf .build-cache" } trigger.on_error = :continue endend
Advanced Networking & Communicator Options
Lesser-used networking and communicator settings for real-world Vagrantfiles.
- forwarded_port auto_correct- `auto_correct: true` remaps a forwarded port to a free host port if the requested one is taken
- private_network :dhcp- Lets the provider assign an IP dynamically instead of hardcoding a static address
- public_network- Bridges the VM directly onto the host's LAN, prompting for a bridge interface at `vagrant up`
- config.vm.communicator- Selects the guest control channel; `ssh` (default) or `winrm` for Windows guests
- config.ssh.insert_key- When true, Vagrant replaces the default insecure keypair with a randomly generated one on first boot
- NFS synced folder- `type: "nfs"` on `config.vm.synced_folder` gives much faster I/O than the default VirtualBox shared folder
- rsync synced folder- `type: "rsync"` one-way syncs host changes to the guest, useful when NFS/VirtualBox mounts aren't available
Plugins & Windows Guests
Managing plugins and configuring a WinRM communicator for Windows guests.
vagrant plugin install vagrant-vbguest # Keep VirtualBox Guest Additions in syncvagrant plugin install vagrant-hostmanager # Manage /etc/hosts across multi-machine setupsvagrant plugin listvagrant plugin uninstall vagrant-vbguest
Use 'config.vm.provision' with the ansible_local provisioner when the host lacks Ansible installed — it installs Ansible inside the guest and runs your existing playbooks unchanged.