A simple vagrant Cheat Sheet

Vagrant is an open-source tool that is used to manage a development environment. Through the command line, you can grab any available OS, install it, configure it, run it, work inside of it, shut it down, and more. Essentially, it is a layer of software installed between a virtualization tool (such as VirtualBox, Docker, Hyper-V) and a VM.

This article provides a quick cheat sheet with almost everything you need to know about configuring and managing Vagrant.

This article assumes that you already have Vagrant installed on a local machine or at least access to a system where Vagrant is installed.

NOTE: For almost all of the following commmands, you'll need to be in a Vagrant directory containing a Vagrantfile and .vagrant directory.

Creating a VM

  • vagrant init -- Initialises Vagrant. This creates a Vagrantfile and ./.vagrant directory in the current directory, using no specified base image.
    NOTE: Before you can do a vagrant up, you'll need to specify a base image in the Vagrantfile.
  • vagrant init <boxpath> -- Initialise Vagrant with a specific box. For example:
    $ vagrant init ubuntu/trusty64
    To find a box, visit the public Vagrant box catalog.

Starting/Stopping a VM

  • vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
  • vagrant resume -- resume a suspended machine (vagrant up works just fine for this as well)
  • vagrant halt -- stops the vagrant machine
  • vagrant suspend -- suspends a virtual machine (remembers state)
  • vagrant provision -- forces reprovisioning of the vagrant machine
  • vagrant reload -- restarts vagrant machine, loads new Vagrantfile configuration
  • vagrant reload --provision -- restart the virtual machine and force provisioning

Accessing a VM

  • vagrant ssh -- connects to machine via SSH
  • vagrant ssh <boxname> -- If you give your box a name in your Vagrantfile, you can ssh into it with boxname.

Removing a VM

  • vagrant destroy -- stops and deletes all traces of the vagrant machine
  • vagrant destroy -f -- stops and deletes all traces of the vagrant machine without confirmation

Snapshots

  • vagrant snapshot save [options] [vm-name] <name> -- Allows us to save so that we can rollback at a later time

VM Boxes

  • vagrant box list -- see a list of all installed boxes on your computer
  • vagrant box add <name> <url> -- download a box image to your computer
  • vagrant box outdated -- check for updates vagrant box update
  • vagrant boxes remove <name> -- deletes a box from the machine
  • vagrant package -- packages a running virtualbox env in a reusable box

Tips

  • vagrant -v -- get the vagrant version
  • vagrant status -- outputs status of the vagrant machine
  • vagrant global-status -- outputs status of all vagrant machines
  • vagrant global-status --prune -- same as above, but prunes invalid entries
  • vagrant provision --debug -- use the debug flag to increase the verbosity of the output
  • vagrant push -- Vagrant can be configured to deploy code. More information on using is available http://docs.vagrantup.com/v2/push/index.html
  • vagrant up --provision | tee provision.log -- Runs vagrant up, forces provisioning and logs all output to a file