Vagrant Cheatsheet
Vagrant Cheatsheet
Command reference and Vagrantfile patterns for spinning up disposable VMs, tuned for speed. The Optimize section collects the settings that actually make Vagrant fast: linked clones, NFS/rsync shares, headless boot, package caching, and golden boxes.
Core lifecycle
| Command | What it does |
|---|---|
| vagrant init [box] | Create a Vagrantfile in the current dir. Add –minimal to skip the comments. |
| vagrant up | Create + boot + provision. –provider=libvirt picks a backend. |
| vagrant ssh [name] | SSH into the machine (name needed for multi-machine). |
| vagrant status | State of machines defined in this environment. |
| vagrant global-status | Every machine Vagrant knows about. –prune clears stale entries. |
| vagrant halt | Graceful shutdown. |
| vagrant suspend / resume | Save RAM state to disk / restore it (fastest stop-start). |
| vagrant reload | Restart and re-read the Vagrantfile. –provision re-runs provisioners. |
| vagrant provision | Re-run provisioners on a running machine. |
| vagrant destroy [-f] | Delete the machine. -f skips the prompt. |
| vagrant validate | Check the Vagrantfile syntax without booting. |
| vagrant port | Show the forwarded port map. |
| vagrant ssh-config | Emit an SSH config block (wire up scp / ansible / your IDE). |
Boxes
| Command | What it does |
|---|---|
| vagrant box add USER/BOX | Download a box from Vagrant Cloud. |
| vagrant box list | List installed boxes and their providers. |
| vagrant box outdated –global | Check every box for updates. |
| vagrant box update | Update the box used by the current environment. |
| vagrant box remove NAME | Delete a box. –box-version targets one version. |
| vagrant box prune | Remove all but the latest version of each box. |
Good boxes bento/* and generic/* are clean, multi-provider images. Start one with vagrant init bento/ubuntu-22.04.
Vagrantfile essentials
A lean, fast starting point: named VM, right-sized resources, linked clone, headless, one forwarded port.
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-22.04"
config.vm.hostname = "dev"
config.vm.provider "virtualbox" do |vb|
vb.name = "dev"
vb.memory = 2048
vb.cpus = 2
vb.linked_clone = true # near-instant, disk-efficient clones
vb.gui = false # headless
end
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
config.vm.provision "shell", inline: "apt-get update -y"
endNetworking
# Forwarded port (host 8080 -> guest 80); auto_correct avoids clashes config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true # Private host-only network, static IP config.vm.network "private_network", ip: "192.168.56.10" # Private network via DHCP config.vm.network "private_network", type: "dhcp" # Public bridged network onto a host NIC config.vm.network "public_network", bridge: "eth0"
Synced folders
The default VirtualBox share is slow. Disable it if unused, or switch to NFS / rsync for real throughput.
# Turn off the default /vagrant share (faster boot if you don't need it) config.vm.synced_folder ".", "/vagrant", disabled: true # NFS: much faster than vboxsf on Linux / macOS (needs host nfsd) config.vm.synced_folder "./src", "/srv/app", type: "nfs", nfs_udp: false # rsync: one-way push, ideal for remote / libvirt / cloud providers config.vm.synced_folder "./src", "/srv/app", type: "rsync", rsync__exclude: [".git/", "node_modules/"] # keep it live with: vagrant rsync-auto
Windows host Use type: “smb” (runs as Administrator) instead of NFS.
Provisioning
# Inline shell config.vm.provision "shell", inline: <<-SHELL apt-get update && apt-get install -y nginx SHELL # External script config.vm.provision "shell", path: "provision.sh" # Run on every boot, not just first up config.vm.provision "shell", run: "always", inline: "systemctl restart nginx" # Push a file into the guest config.vm.provision "file", source: "./app.conf", destination: "/tmp/app.conf" # Ansible config.vm.provision "ansible" do |a| a.playbook = "playbook.yml" end
Snapshots
| Command | What it does |
|---|---|
| vagrant snapshot save NAME | Take a named snapshot. |
| vagrant snapshot restore NAME | Roll the machine back to it. |
| vagrant snapshot list | List snapshots. |
| vagrant snapshot delete NAME | Remove a snapshot. |
| vagrant snapshot push / pop | Stack-style: quick throwaway snapshot, then undo. |
Provider Snapshots need a provider that supports them (VirtualBox, libvirt). Snapshot before risky provisioning to iterate fast.
Multi-machine
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-22.04"
config.vm.define "web" do |web|
web.vm.hostname = "web"
web.vm.network "private_network", ip: "192.168.56.11"
end
config.vm.define "db" do |db|
db.vm.hostname = "db"
db.vm.network "private_network", ip: "192.168.56.12"
end
end
# Target one machine: vagrant up web ; vagrant ssh dbN identical nodes Loop it: (1..3).each { |i| config.vm.define “node#{i}” do |n| … end }.
Plugins
| Command / Plugin | Purpose |
|---|---|
| vagrant plugin install NAME | Install a plugin. |
| vagrant plugin list | List installed plugins. |
| vagrant plugin update [NAME] | Update plugins. |
| vagrant plugin uninstall NAME | Remove a plugin. |
| vagrant-vbguest | Auto-match VirtualBox Guest Additions to the guest kernel. |
| vagrant-libvirt | KVM / libvirt provider (fast on Linux). |
| vagrant-cachier | Cache apt/yum packages across VMs — big speed win. |
| vagrant-disksize | Resize the primary disk of a box. |
| vagrant-hostmanager | Manage /etc/hosts across host and guests. |
Providers
| Provider | Notes |
|---|---|
| virtualbox | Default, cross-platform. Easiest, not the fastest. |
| libvirt | KVM on Linux — fastest option; needs the vagrant-libvirt plugin. |
| vmware_desktop | VMware Workstation/Fusion; commercial Vagrant plugin. |
| docker | Containers as lightweight “machines”. |
| hyperv | Windows Hyper-V (run the shell as Administrator). |
Pick one vagrant up –provider=libvirt per run, or set export VAGRANT_DEFAULT_PROVIDER=libvirt once.
Optimize
The settings that actually move the needle, roughly in order of payoff.
| Lever | How |
|---|---|
| Golden box | Bake dependencies into a custom box with Packer and keep Vagrant provisioning thin — the single biggest speedup for repeated up. |
| Linked clones | vb.linked_clone = true (VirtualBox); default on libvirt. Near-instant, low-disk VM creation. |
| Faster shares | NFS or rsync instead of vboxsf; disabled: true on the default /vagrant if unused. |
| Headless | vb.gui = false — no VM window, less overhead. |
| Right-size | Set vb.memory / vb.cpus deliberately; don’t overcommit the host. |
| Package cache | vagrant-cachier shares apt/yum caches across VMs and rebuilds. |
| Parallel bring-up | vagrant up –parallel on providers that support it (e.g. libvirt). |
| Guest Additions | vagrant-vbguest keeps them in sync so shared folders don’t silently break. |
| Prune | vagrant global-status –prune and vagrant box prune to reclaim space. |
| Iterate safely | vagrant snapshot push before a risky provisioning run, pop to undo. |
Env vars & debugging
| Var / Command | Effect |
|---|---|
| VAGRANT_DEFAULT_PROVIDER | Default provider for up. |
| VAGRANT_LOG=info|debug | Log verbosity for any command. |
| VAGRANT_DOTFILE_PATH | Relocate the per-project .vagrant state dir. |
| VAGRANT_NO_PARALLEL | Force serial bring-up. |
| VAGRANT_EXPERIMENTAL | Opt into experimental features. |
| vagrant up –debug | Full trace when a boot fails. |
| vagrant validate | Catch Vagrantfile errors before booting. |
| vagrant destroy -f && vagrant up | Clean rebuild from scratch. |