What is Vagrant? And how should I use it?

What is Vagrant ?

For the last month I had to switch my working computer 3 time, due to various reasons. Unfortunately the process of setup and configuration of development environment takes time. A lot of time, and it's uterrly borring. I've started to wonder on how I can automate this process and make my life easier. Fortunately Rafal has stumbled upon Vagrant before and I've decided to give it a try.

Probably most of you have some experience with virtualization, Vagrantant automates process of creation, configuration and provisioning of virtual machines on most available platforms (Windows, Linux, Mac). With one file we can create a virtual machine with specified operating system, configure networking, install required packages and dependencies, configure services and setup folder sharing beetwen host and machine. Once the Vagrant configuration file is complete it takes all the work of setting up development environent from us.

Some time later during this project we brought new people. Some of them were using different operating system then me. I was using Windows, Rafal decided to stay on Ubuntu. I've previously used Ubuntu, but due to my pet project I had to dual boot, which sucks. Thanks to earlier migration to Vagrant it wasn't a problem. All it took for him was installing Vagrant, selected hypervizor and issuing vagrant up in order to obtain complete and identical to mine development environment.

Vagrant installation

In order to install Vagrant we need to things, hypervizor of our choice and Vagrant itself. Vagrant can work with: VirtualBox, Hyper-V, Docker, VMware, AWS. We can install it from the binary or using a packaging tool, personally I recommend Chocolatey. The installator from Vagant website already contains VirtualBox installer.

Creating new machine

We start from command (on Windows you can open command line in folder by shift right clicking in desired location and selecting Open command prompt here):

vagrant init hashicorp/precise64
vagrant up

It will create Vagrantfile with basic settings and base image hashicorp/precise64. Second command will start our virtual machine (Important! it can tak a while). If the machine is not yet created (we are running this command first time) it will be created and initialized. In case base image does not exists locally it will be downloaded from the repository. After everything is done we have working virtual machine with installed operating system.

Example file Vagrantfile looks like this (in order to make it more readable I've removed comments):

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise64"
end

Configuration

Packages installation and configuration

Vagrant can be provisioned by using different providers. At the time this article was written available was:

In order to add Apache2 installation while provisioning stage you need to add following lines to Vagrantfile before end

  config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update
    sudo apt-get install -y apache2
  SHELL

If we do not want to keep configuration in Vagrantfile we can move it to separate script file. In Vagrantfile it looks like this:

  config.vm.provision "shell", path: "script.sh"

Network configuration

To specify networking address accessible from you host we need to add following line to Vagrantfile, it adds new networking interface which is connected by private network with host with given IP address (192.168.56.2):

  config.vm.network :private_network, ip: '192.168.56.2'

Shared folders

Implicitly Vagrant mounts . folder of your host in /vagrant folder in your machine. If you would like to change method of mounting or change mount targets you need to add following line:

  config.vm.synced_folder '.', '/vagrant', nfs: true

Currently available methods are nfs, rsync and smb. If you use VirtualBox default method is VirtualBox shared folder. Unfortunately it can be slow, I recommend switching to nfs.

Usefull commands

Creation and running a machine it's only a begining of what vagrant command line can do.

In order to stop, but not to destroy, machine we use:

vagrant halt

To delete whole machine

vagrant destroy

We can also stop machine

vagrant suspend

To resume stopped machine type

vagrant resume

Connection with machine using ssh (if you do not have ssh command in command line, this will show info necessary to connect)

vagrant ssh

Summary

Right now you should have an idea of what is Vagrant, how to install it, how to configure and provision, setup interfaces and shared folders. Once you finish your Vagrantfile I recommend adding it to your project version controll and distribute it along your project files. This way whenever someone needs to start developing or you need to update development environent all the need to do is download source code and run vagrant up!

If you have any comments or you have found mistakes and typos I will be glad if leave a comment.

We use cookies to ensure you get the best experience on our website. Privacy policy