Developing robust software requires an accurate testing and staging environment. Vagrant provides just that. It lets you mimic your production server map on your computer.

N.B. Keep in mind, everything typed herein is case sensitive.

Creating the Vagrantfile

commanddescription
vagrant initInitialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you’ll need to specify a base image in the Vagrantfile. I rarely initialize Vagrant this way.
vagrant init boxpathInitialize Vagrant with a specific box. To find a box, go shopping. When you find one you like, just replace it’s name with boxpath. For example, vagrant init chef/centos-6.5. This is how I typically initialize Vagrant.

Miscallenous Vagrantfile customizations

Specifying which box to use

vagrant.configure("2") do |config|
  config.vm.box = "chef/centos-6.5" # or whatever box you want to use
end

Starting a VM

commanddescription
vagrant upBoot the VM’s specified in your Vagrantfile. Equivalent to pressing the power buttons on your “servers.”
vagrant reloadThe equivalent of vagrant halt followed by vagrant up

Getting into a VM

commanddescription
vagrant sshOpen an ssh session to the default box specified in your working directory’s Vagrantfile. If you only have one box in your Vagrantfile, that’s the default one and this command will ssh into it.
vagrant ssh boxnameIf you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory.
vagrant ssh boxidThe boxid can be obtained from vagrant global-status. Works from any directory.

Cleaning up a VM

commanddescription
vagrant destroyStop and delete the default VM. The next time you vagrant up, the VM will be created from scratch.
vagrant destroy -fVagrant does this annoying thing where it prompts you before destroying a VM. -f is short for force, which will destroy the VM without confirming first.

Boxes

Boxes are prebuilt VM images. You never modify your box images.

commanddescription
vagrant box listSee a list of all installed boxes on your computer
vagrant box addDownload a box image to your computer

Networking into and out of VM’s

Forward a port from the VM to your computer

vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080 # guest is the VM; host is your computer
end

Provisioning stuff on your VM’s

Provisioning lets you execute commands on the VM when vagrant up is run.

commanddescription
vagrant reload --provisionRun provisioners without halting the VM and running vagrant up

Run a shell script when you vagrant up (a.k.a., when a VM is created)

vagrant.configure("2") do |config|
  config.vm.provision :shell, path: "my_bash_script.sh" # path is relative to your `Vagrantfile`
end

Share folders between your VM and your computer

By default ./ on your computer is shared as /vagrant on the VM.

Letting other people access your VM’s

Vagrant has a cool feature that lets you publicly share your VM’s with anyone, through a sort-of “DynDNS” solution. This is a great way to let stakeholders view your devbox.

commanddescription
vagrant loginLog in to HashiCorp’s Atlas on your computer
vagrant shareShares your VM and provides you with the public URL
ctrl+cQuits the sharing session