Vagrant is a tool for creating and managing portable and reproducible virtualized development environments.
Installation
Platform Command Ubuntu/Debian wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmorCentOS/RHEL sudo yum install -y yum-utils && sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repomacOS brew install vagrantWindows choco install vagrantDirect binary Download from vagrantup.com/downloads
Prerequisites : VirtualBox, VMware or Hyper-V installed
Essential commands
Action Command Description Initialize vagrant init ubuntu/focal64Create a Vagrantfile with a box Start vagrant upLaunch the virtual machine SSH connection vagrant sshConnect to the VM Suspend vagrant suspendPause the VM Restart vagrant reloadRestart and reload config Stop vagrant haltGracefully stop the VM Destroy vagrant destroyCompletely delete the VM Status vagrant statusDisplay VM status
Box management
Action Command List boxes vagrant box listAdd box vagrant box add ubuntu/focal64Remove box vagrant box remove ubuntu/focal64Update vagrant box updateClean up vagrant box prune
Basic Vagrantfile
Simple configuration
Vagrant . configure ( " 2 " ) do | config |
# Base box
config . vm . box = " ubuntu/focal64 "
# Network configuration
config . vm . network " private_network " , ip: " 192.168.33.10 "
config . vm . network " forwarded_port " , guest: 80 , host: 8080
# Shared folders
config . vm . synced_folder " . " , " /vagrant "
config . vm . synced_folder " ./src " , " /var/www "
# Provider configuration
config . vm . provider " virtualbox " do | vb |
vb . name = " dev-server "
vb . memory = " 2048 "
vb . cpus = 2
vb . gui = false
end
# Provisioning
config . vm . provision " shell " , inline: <<-SHELL
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
SHELL
end
Multi-machines
Vagrant . configure ( " 2 " ) do | config |
# Web server
config . vm . define " web " do | web |
web . vm . box = " ubuntu/focal64 "
web . vm . network " private_network " , ip: " 192.168.33.10 "
web . vm . provider " virtualbox " do | vb |
vb . memory = " 1024 "
end
end
# Database
config . vm . define " db " do | db |
db . vm . box = " ubuntu/focal64 "
db . vm . network " private_network " , ip: " 192.168.33.11 "
db . vm . provider " virtualbox " do | vb |
vb . memory = " 2048 "
end
end
end
Network configuration
Type Configuration Usage Port forwarding config.vm.network "forwarded_port", guest: 80, host: 8080Access from host Private network config.vm.network "private_network", ip: "192.168.33.10"Static private IP Public network config.vm.network "public_network"Network access Private DHCP config.vm.network "private_network", type: "dhcp"Auto private IP
Shared folders
# Simple folder
config . vm . synced_folder " . " , " /vagrant "
# With options
config . vm . synced_folder " ./data " , " /var/data " ,
owner: " www-data " ,
group: " www-data " ,
mount_options: [ " dmode=755 " , " fmode=644 " ]
# NFS (more performant)
config . vm . synced_folder " . " , " /vagrant " ,
type: " nfs " ,
nfs_udp: false ,
nfs_version: 4
# Disable default folder
config . vm . synced_folder " . " , " /vagrant " , disabled: true
Provisioning
Shell inline
config . vm . provision " shell " , inline: <<-SHELL
apt-get update
apt-get install -y docker.io
usermod -aG docker vagrant
SHELL
External script
config . vm . provision " shell " , path: " bootstrap.sh "
config . vm . provision " shell " , path: " setup.sh " , args: [ " arg1 " , " arg2 " ]
Ansible
config . vm . provision " ansible " do | ansible |
ansible . playbook = " playbook.yml "
ansible . inventory_path = " inventory "
ansible . limit = " all "
end
Docker
config . vm . provision " docker " do | docker |
docker . pull_images " nginx:latest "
docker . pull_images " mysql:5.7 "
docker . run " nginx " ,
args: " -p 80:80 -v /vagrant:/usr/share/nginx/html "
end
Providers
VirtualBox
config . vm . provider " virtualbox " do | vb |
vb . name = " my-vm "
vb . memory = " 2048 "
vb . cpus = 2
vb . gui = false
vb . customize [ " modifyvm " , :id , " --natdnshostresolver1 " , " on " ]
vb . customize [ " modifyvm " , :id , " --natdnsproxy1 " , " on " ]
end
VMware
config . vm . provider " vmware_desktop " do | vmware |
vmware . vmx [ " memsize " ] = " 2048 "
vmware . vmx [ " numvcpus " ] = " 2 "
vmware . vmx [ " displayName " ] = " My VM "
end
Hyper-V
config . vm . provider " hyperv " do | hv |
hv . memory = 2048
hv . cpus = 2
hv . vmname = " my-vm "
end
Debugging & Troubleshooting
Debug Command Debug mode VAGRANT_LOG=debug vagrant upGlobal status vagrant global-statusClean cache vagrant global-status --pruneReload config vagrant reload --provisionSSH info vagrant ssh-config
Popular boxes
Box Description ubuntu/focal64Ubuntu 20.04 LTS ubuntu/jammy64Ubuntu 22.04 LTS centos/7CentOS 7 centos/stream8CentOS Stream 8 debian/bullseye64Debian 11 generic/fedora36Fedora 36 bento/ubuntu-20.04Ubuntu 20.04 (Chef Bento)
Useful plugins
Plugin Installation Usage vagrant-hostsupdater vagrant plugin install vagrant-hostsupdaterAuto update /etc/hosts vagrant-vbguest vagrant plugin install vagrant-vbguestAuto update VBoxGuestAdditions vagrant-disksize vagrant plugin install vagrant-disksizeResize disk
Best practices
Practice Description Organization One Vagrantfile per project Cleanup Destroy unused VMs Performance Use NFS for shared folders Boxes Use official boxes