Skip to content

Instantly share code, notes, and snippets.

Last active March 19, 2022 15:44
  • Star 6 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Vagrant crash course

Vagrant crash course

This is a 5-minute crash course to start using vagrant. We will host our vagrant files in /home/USER/vms. That's my convention working with many vagrant machines.

Minute 0: Get vagrant and virtulabox



Minute 1: Create an init file

Think of vagrant as Python's virtualenv or making a new repo. You want to start off with a VM with some initial configuration.

cd /home/USER/vms && mkdir master && cd master
vagrant init

Now we have a directory called master and is holding a vagrant configuration file called Vagrantfile.

Minute 2: Download a vagrant box

The point of using vagrant is so you can download a boostrapped VM (most of them are actually just vanilla VMs straight out of .iso image installation).

Get a VM from But for now, let's just grab Ubuntu Server Precise 12.04.3 amd64 Kernel is ready for Docker (Docker not included).

To download a box, the syntax is vagrant box add {title} {url}. In our case:

vagrant box add 120403_64_docker

I try to be as descriptive as possible. It's easy to read what it means. I think so...

Minute 3: Configure your ini file

Wow. We got ourselves a box! Now let's actually use it in our init file.

Use your favorite editor to open Vagrantfile and then replace "base" with "120403_64_docker". We are telling vagrant to launch a new VM based on the stock box 120403_64_docker.

Easy right?

Minute 4: Launch your VM

Simple! While you are in the master directory:

vagrant up

By default, Vagrant will launch the machine via Virtualbox and will use a private IP, aka a class A IP. You cannot SSH into this machine directly.

After awhile your machine will be loaded and then

vagrant ssh

Yes! Now you are ssh-ed into the VM, you can start playing. Of course, you should do sudo apt-get update first. Notice your user is automatically created and is called vagrant. Guess what the password is? Yeah! vagrant too.

Minute 5: Shut down the VM

When you want to shut down the VM you just go back to the master directory, and then do vagrant halt.

That's it.

We still have time! So let's go over some advance stuff.


vagrant --help

Some notable advance commands:

  • package: At some point if you have done enough work but you are about to mess up your VM yet you want to save the VM? You can either clone using your VM Manager (e.g. virtualbox), or using vagrant's package command. Or if you want to distribute a VM to your friend, you can just package your VM into a vagrant box. You can even package the Vagrantfile!

  • reload: the fancy thing about this is if you change anything with Vagrantfile and doesn't want full restart, you can try reload.

  • box list: you can use this command to find out a list of boxes you have installed. Did I tell you that boxes you've exported via package command can also be add to your vagrant environment?

Vagrant - insecured-ssh-key

Vagrant comes with an insecured ssh key. That's how vagrant ssh works by default if you are using some default vagrant box. The location of this key is in ~/.vagrant.d/insecure_private_key, under the home directory of whichever user installed vagrant.

Vagrant - box location

Vagrant saves all the boxes you've added to vagrant to ~/.vagrant.d/boxes. Sometimes, you should free them up!


If you cd in slaves instead of masters, when you vagrant ssh you will ssh into the slave machine (provided that the machine is on). So remember, the vagrant command works with relative vagrant file.

Vagrantfile - init

If you already have a box, you can actually create your Vagrantfile by doing vagrant init {box_name} so you don't have to edit the file just to use the right box. I also believe if the box comes with its own Vagrantfile during package time, that packaged Vagrantfile will be copied and created on your loca disk for you. This means, the settings will be carried over. I forgot, but probably a yes.

Vagrantfile - ssh

You can specify the default user and the ssh private key to use for a given Vagrantfile.

  config.ssh.default.private_key_path = "meow_private_key"
  config.ssh.default.username = "cat"

This will allow us to ssh into the machine (using vagrant ssh) that was created by the Vagrantfile with this configuration as cat using the meow_private_key. Note the key is relative to the location of Vagrantfile.

Vagrantfile - network

You can enable the public network by uncommenting :public_network. The so-called public network is simply a LAN network IP so that other machines on the same network can SSH into it directly).

You can also enable another kind of private network other than the class A and the 192.168.1.* kind. Vagrant will assign static private network of 192.168.33.* if you use :private_network. By default, the last byte is the number 10 (

It is best if you just use either the private or the "public network." Don't try to mess with the /etc/network/interface file. If you have to (sometimes when Vagrant isn't working well and you want to control the VM directly via Virtualbox), then you have to boot without vagrant with Virtualbox and remove the static and return to use DHCP.

This is what vagrant does to the interface file:

# The primary network interface
auto eth0
iface eth0 inet dhcp
pre-up sleep 2
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth1
iface eth1 inet static

See Vagrant networking for more information.

Vagrantfile - customize

You can change how many cores and memory the VM will have in Vangrantfile. Remember the core of vagrant is to provide friendly commands to VM Manger. You can set these hardware settings directly from the VM Manager (e.g. virtualbox).

I believe by default the total memory is around 378MB. Whatever the number is, it's pretty low.

The option is in vb.customize.

To use it, you must enable config.vm.provider. In our tutorial, the following code will set 600MB for memeory:

   config.vm.provider :virtualbox do |vb|
  #   # Don't boot with headless mode
  #   vb.gui = true
  #   # Use VBoxManage to customize the VM. For example to change memory:
     vb.customize ["modifyvm", :id, "--memory", "600"]

Refer to the following posts on how to set CPU cores and memory.

Vagrant provisioner - ansible

Yes. You can hook up ansible with vagrant, but silex isn't working properly or well with this integration. But if you have time, play with it.

You probably can use Vagrant for other tasks in other areas. So keep your mind open about this integration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment