Vagrant

What is Vagrant?

Vagrant is “the command line utility for managing the lifecycle of virtual machines.” (HashiCorp)

“Vagrant is a tool for building and managing virtual machine environments in a single workflow.” (HashiCorp)

“Vagrant…is all you need to work on any project, to install every dependency that project needs, and to set up any networking or sync folders, so you can continue working from the comfort of your machine.” (HashiCorp)

Documentation

Vagrant has excellent documentation, this page can serve as a quick reference guide to some of the basics but really, check out the docs.

Concepts

  • Vagrantfile – A simple document used to describe the virtual machine, its configuration, and software.
  • Boxes – The base image upon which one builds one’s specific system.
  • Vagrant Cloud – Hosts boxes, you’ll usually download your base box from here.
  • Synced Folders – Allows you to work with files both on your local system and in the virtual machine.
  • Provisioning – Vagrant’s execution of the configuration specified in the Vagrant file to create the desired virtual machine.
  • Port Forwarding – Passes traffic between the local system the virtual machine. This allows one, for example, to browser a site hosted on the VM as if it was on the local machine.
  • Vagrant Share – Provides a simple way to provide anyone with an internet connection access to your virtual machine.
  • Provider – The backend software/service that hosts the Virtual Machine (e.g. VirtualBox, VMWare, Hyper-V).
  • Aliases – Create aliases for existing commands, combine multiple commands into new commands, create new commands entirely, etc.

Basic Commands

  • vagrant init producername/boxname – Initialize a new Vagrantfile.
  • vagrant up – Start running a virtual machine built from a Vagrantfile.
  • vagrant box add producername/boxname – Add a specific box to your local system.
  • vagrant ssh – Connects to the specified virtual machine.
  • vagrant destroy – Deletes the virtual machine.
  • vagrant reload --provision – Reruns the provisioner portion of the Vagrantfile.
  • vagrant share – Allows your VM to be accessible to external guests using ngrok.
  • vagrant halt – Attempts a clean shutdown of the VM.
  • vagrant suspend – Takes a snapshot of the current state of the VM and suspends it (e.g., it is no longer actively consuming CPU/RAM).
  • vagrant resume – Begins running the VM using the snapshot of state taken by vagrant suspend.
  • vagrant snapshot – Take a point-in-time state of VM which can then be rolled back to later.
    • push – Takes snapshot.
    • pop – Restores snapshot.
  • vagrant status – Reports status of machines.

Common Vagrantfile Configurations

  • config.vm.box = "producername/boxname" – Defines which box will serve as the base image for your specified system.
  • config.vm.box :shell, path: "nameofscript.sh" – Define a script that will be run by Vagrant when provisioning the VM.
  • config.vm.box :forwarded_port, guest: port, host: port – Pass traffic between your local machine (host) and the virtual machine (guest).

Common Problems

  • VirtualBox is usually the easiest way to get started with Vagrant but on Windows systems Hyper-V is oftentimes enabled by default. At this time VirtualBox and Hyper-V do not run well alongside each other. You should disable Hyper-V if you want to use VirtualBox.