Vagrant Up

Vagrant is a pretty cool project which enables you to describe / provision a complete virtual machine instance in a single text file. The Vagrantfile. The basic idea behind Vagrant is that the whole VM is described in a Vagrantfile. Instead of giving somebody a whole VM with several giga bytes you simply share the Vagrantfile. By executing

vagrant up

in the directory where the Vagrantfile is placed, will setup the VM like described in the Vagrantfile. Executing “vagrant up” the very first time can take a couple minutes. Later on the command will simply start the VM. The advantages are clear.

  • A Vagrantfile can be easily committed to a git repository which enables version control.
  • Sharing a Vagrantfile is much easier than sharing a several giga byte image.
  • Updating a VM means updating text in a Vagrantfile.
  • The provisioning of a VM is reproducible.

Vagrant

VM Providers

Vagrant is supporting multiple virtualisation technologies. For example VirtualBox, VMWare, EC2 and many more. By default Vagrant is using VirtualBox because it’s for free. Please make sure that VirtualBox is installed before you start using Vagrant.

Vagrant box

A Vagrant box is a preconfigured VM instance which can be used by Vagrant. In the Vagrantfile there is always a reference to a Vagrant box. https://atlas.hashicorp.com is a hub for Vagrant boxes.

Vagrantfile

The Vagrantfile has a reference to a Vagrant box and describes how to provision the VM instance. This here is a very simple example of a Vagrantfile.

Vagrant.configure(2) do |config|
config.vm.box = “ubuntu/trusty64”
end

This Vagrantfile is referencing the “ubuntu/trusty64” Vagrant box, which is a minim. installation of Ubuntu 14.04 LTS.

The Vagrantfile can also contain information about Networking, RAM, UI and a bootstrap script which will be executed by running vagrant up the very first time for a Vagrantfile.

Vagrant SSH

After running

vagrant up

the VM can be accessed with

vagrant ssh

This will you directly login into the VM without any password. It’s that easy to get started with Vagrant.

Provisioning

Until now we just started a simple Ubuntu virtual machine. Now we come to the interesting part. The provisioning. In the Vagrantfile we can describe what else should be installed and executed on vagrant up. Assume we want to install MariaDB Server 5.5 on the VM. That’s how it would look like.

Vagrant.configure(2) do |config|

  config.vm.box = “ubuntu/trusty64”

  config.vm.provision “shell”, inline: <> /etc/environment
    sudo echo “LANGUAGE=en_US.UTF-8” >> /etc/environment
    sudo echo “LC_ALL=en_US.UTF-8” >> /etc/environment
    sudo echo “LC_CTYPE=en_US.UTF-8” >> /etc/environment

    sudo export DEBIAN_FRONTEND=noninteractive
    sudo debconf-set-selections <<< ‘mariadb-server-5.5 mysql-server/root_password  password rootpass’
    sudo debconf-set-selections <<< ‘mariadb-server-5.5 mysql-server/root_password_again password rootpass’
    sudo apt-get install -y mariadb-server

  SHELL
end

running vagrant up the first time will execute the provision part and install MariaDB Server 5.5. Beside that it will set the encoding to en_US.UTF-8.

Conclusion

Assume you want to ensure that everybody on your team is working with the same version of MariaDB Server and with the same encoding. In that case you can simply setup a Vagrantfile for that which exactly describes the version and encoding of MariaDB. That way everybody is using the exact same backend. Beside that the Vagrantfile can be part of the regular source code and checked in into VCS (git | svn). Upgrading MariaDB or changing the encoding can be simply done by updating the Vagrantfile. The changes can be committed back to VCS and the team members only have to run

vagrant destroy
vagrant up

to update their environment.

Vagrant is a great way to ensure that everybody is using the exact same software.

Here is another article how to setup a complete dev. environment with Vagrant.

One thought on “Vagrant Up

Leave a comment