skip to content

Search

Syspirit
EN

Vagrant

Vagrant Guide - Managing virtualized development environments!

Vagrant is a tool for creating and managing portable and reproducible virtualized development environments.

Installation

PlatformCommand
Ubuntu/Debianwget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor
CentOS/RHELsudo yum install -y yum-utils && sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
macOSbrew install vagrant
Windowschoco install vagrant
Direct binaryDownload from vagrantup.com/downloads

Prerequisites: VirtualBox, VMware or Hyper-V installed

Essential commands

ActionCommandDescription
Initializevagrant init ubuntu/focal64Create a Vagrantfile with a box
Startvagrant upLaunch the virtual machine
SSH connectionvagrant sshConnect to the VM
Suspendvagrant suspendPause the VM
Restartvagrant reloadRestart and reload config
Stopvagrant haltGracefully stop the VM
Destroyvagrant destroyCompletely delete the VM
Statusvagrant statusDisplay VM status

Box management

ActionCommand
List boxesvagrant box list
Add boxvagrant box add ubuntu/focal64
Remove boxvagrant box remove ubuntu/focal64
Updatevagrant box update
Clean upvagrant 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

TypeConfigurationUsage
Port forwardingconfig.vm.network "forwarded_port", guest: 80, host: 8080Access from host
Private networkconfig.vm.network "private_network", ip: "192.168.33.10"Static private IP
Public networkconfig.vm.network "public_network"Network access
Private DHCPconfig.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

DebugCommand
Debug modeVAGRANT_LOG=debug vagrant up
Global statusvagrant global-status
Clean cachevagrant global-status --prune
Reload configvagrant reload --provision
SSH infovagrant ssh-config
BoxDescription
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

PluginInstallationUsage
vagrant-hostsupdatervagrant plugin install vagrant-hostsupdaterAuto update /etc/hosts
vagrant-vbguestvagrant plugin install vagrant-vbguestAuto update VBoxGuestAdditions
vagrant-disksizevagrant plugin install vagrant-disksizeResize disk

Best practices

PracticeDescription
OrganizationOne Vagrantfile per project
CleanupDestroy unused VMs
PerformanceUse NFS for shared folders
BoxesUse official boxes