This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Vagrantfile Cheatsheet

A quick reference guide for Vagrantfile configuration: box settings, port forwarding, private/public networking, synced folders, providers, and provisioners.

· Published

Vagrant builds and manages virtual machine development environments through a single configuration file named Vagrantfile.

Vagrantfile Configuration

Vagrant.configure("2") do |config|
  # Base box image
  config.vm.box = "ubuntu/focal64"

  # Optional URL if box does not exist locally
  # config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/focal64"

  # Port Forwarding: access host localhost:8080 to guest 80
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Private Network: host-only access via static IP
  config.vm.network "private_network", ip: "192.168.33.10"

  # Public Network: bridged directly to local physical network
  # config.vm.network "public_network"

  # SSH Agent Forwarding
  # config.ssh.forward_agent = true

  # Synced Folders: mount host directory to guest path
  config.vm.synced_folder "../data", "/vagrant_data"

  # Provider customization (e.g. VirtualBox)
  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "2048"
    vb.cpus = 2
  end

  # Shell Provisioning
  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y nginx
  SHELL

  # Puppet Provisioning
  # config.vm.provision :puppet do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "site.pp"
  # end

  # Chef Solo Provisioning
  # config.vm.provision :chef_solo do |chef|
  #   chef.cookbooks_path = "../cookbooks"
  #   chef.add_recipe "nginx"
  # end
end

References

Related topics