Organize your development environments with Vagrant
I. Initial thoughts
– Are you using Windows/MacOs but your production server is Centos/Debian?
– Are you using Ubuntu 14.04 with built-in stable PHP5.5 but your server version is PHP 5.3?
– Are you working on many projects, in which one requires Python 2.7, one requires Python 3.*, one MySQL 5.1, one MySQL 5.6, but you want to work with the same environment with the production server?
Well, you’re not alone, we’re all the same.
And in the past, we often find ourselves using VirtualBox or even Vmware to create a full OS which is same as our production server. To be honest, it works. But it’s also so heavy, and resource-consuming, really!
And that’s where Vagrant comes to rescue.
Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the “works on my machine” excuse a relic of the past.
II. Purpose
– Create identical development environments for everyone on our team
– Easy to use, easy to configure
– Lightweight
III. Let’s start
1. Definition:
Let’s share the understanding of these terms:
– Host machine: The system you’re running on
– Guest machine: The system you ‘virtualize’, and then install targeted OS (for ex: Centos 6.5, Debian 7.0…) inside
– Box: The term is used by Vagrant.
Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as boxes in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile – Vagrant Official Document
There are many official/supported box which is created and upload for our use. We will use these provided box, instead of creating one from scratch.
2. Installation:
– Vagrant – find installer for your current
– VirtualBox – find installer matches your system (distribution and architecture as well)
Note: VirtualBox is one provider for Vagrant. You can choose another provider, for example: VMWare…
3. Download pre-built Box
– Search from Vagrant Cloud Website, choose one, for example ‘hashicorp/precise32’ (Ubuntu 1204)
– $ vagrant box add hashicorp/precise32
(Or your desired box)
It will download this box for the first time. Later, we can re-use it for any projects.
4. Usage
– CD to the project dir
– $ vagrant init
(After this command, a file name ‘Vagrantfile’ will be created inside your project dir. This file is supposed to be put to your Version Control System. This is Vagrant configuration file [] https://docs.vagrantup.com/v2/vagrantfile/)
– Open newly-created Vagrantfile, edit the box from base to the right box
– $ vagrant up
– $ vagrant ssh
So easy! Let’s create amazing tools!!
5. Port forwarding
Open Vagrantfile, find the line to configure ‘config.vm.network’
– $ vagrant reload
This is needed for loading new configuration
– Install your webserver (apache, nginx)
– In your host machine, open url: localhost:8080/
This is nginx’s default page on my Centos, configured with port 8081
IV. Troubleshooting
1. Error message:
Failed to mount folders in Linux guest. This is usually because
the “vboxsf” file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
Solution:
– Upgrade VirtualBox
OR
(In host machine)
– $ vagrant plugin install vagrant-vbguest
– $ vagrant ssh
(In guest machine)
– $ sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
(Ref: Github issue)
OR
(In host machine)
– $ wget http://download.virtualbox.org/virtualbox/4.3.14/VBoxGuestAdditions_4.3.14.iso
– $ sudo mv VBoxGuestAdditions_4.3.14.iso /usr/share/virtualbox/VBoxGuestAdditions.iso
V. FAQ
1. You said it’s ‘lightweight’. How ‘light’ is it?
It depends on your chosen box. For some recommended boxes I have been using (chef/Centos, chef/Debian, Ubuntu Server),
it takes approximate 400 MB memory (configurable) and 20 GB disk space (roughly configurable, for advanced users).
2. It sounds really cool, but it’s new, I’m afaid I can’t find a box for my purpose
Vagrant is >4 years old already. It’s mature and widely adopted. You can easily find thoudsands of boxes (including even
Arch Linux, Windows 8.1, FreeBSD, OpenBSD….)
– Vagrant Cloud Website
– VagrantBox.es Website
VI. Summary
Vagrant is cool, and I’m pretty sure you’ll like it. It should make our development life much easier.
For further discussion, or you need any help on using Vagrant, please leave comment here or
drop me an email at tuan_nh@technology-technology.jp. You’re welcome.