Menu

Set up development environment with Vagrant

When you join a new company, to start to contribute to the code base, you will need
to set up a development environment on your local machine. Most of the time,
the team will give you the bootstrap script to do that. But sometimes, that
magical script does not exist, and that is the time you know you should introduce
the team your bootstrap script. In this article, I shared what I learned
while building a script by using Vagrant.

Vagrant

Vagrant is a ruby-based tool to manage virtual machines.
Vagrant using Virtual-box underlying.

This tool helps create many virtual machines easily. If you want to read more about vagrant, you should go to vagrant home page.

To install vagrant in Mac OSX, firstly you need to install RUBY.
I recommend you should using rvm when using ruby.

To install rvm, you need gcc which already is installed when we install Xcode.
In this case, I use Xcode 4.3.3 from Apple Store.
But after getting Xcode, I did not find out gcc. To fixed it, open Xcode 4.3.3,
go to Preferences -> Downloads -> Choose install “Commandline tool for Xcode”.
After it finishs, you got your magical GCC :-).

Vagrant divides virtual machines to many projects. Each project can contain many machines. Each project is a store in a configuration file Vagrantfile:

$> mkdir debian
$> cd debian
$> vagrant init

A Vagrantfile has been placed in this directory. You are now
ready to vagrant up your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com for more information on using Vagrant.

Now change Vagrantfile to set up our new virtual machine.

Vagrant::Config.run do |config|
# using debian as virtual environment to build
config.vm.box = 'debian'# Share an additional folder to the guest VM. The first argument is# an identifier, the second is the path on the guest to mount the# folder, and the third is the path on the host to the actual folder.
config.vm.share_folder "srv", "/srv", "/Users/knt/srv"# Forward guest port 80 to host port 4567# forward_port is a method which takes two arguments:# + guest port - The port on the virtual machine.# + host port - The port on your local machine you want# to use to access the guest port.
config.vm.forward_port 80, 4567# Config virtual machine memory
config.vm.customize ["modifyvm", :id, "--memory", 512]
end

After modifying Vagrantfile, it is time to create our first virtual environment.
NOTE: More details about Vagrantfile configuration, please take a look at
vagrant document.

$> vagrant ssh
Linux vagrant-debian-squeeze 2.6.32-5-686 #1 SMP Wed Jan 12 04:01:41 UTC 2011 i686
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Feb 2407:02:08 2011 from 10.0.2.2
[email protected]:~$

Cool !!!. Let ’s config it to code

Config virtual machine

Set up utility tools

Firstly, vagrant let us ssh to VM without a password. To make our own password on
VM, we change ‘root’ and ‘vagrant’ user password

Change the hostname

Honestly, I do not like the name “vagrant-debian-squeeze”.
It ’s too long to remember. I just want to change it “debian”
“vagrant-debian-squeeze” is “hostname” of VM. To change it in debian,
we using bellow command:

$> vim /etc/hostname
# Change to hostname to "debian"
$> vim /etc/hosts
# Add new line
127.0.0.1 debian
# we add this line to solve issue "unable to resolve hostname"
# Make it active in next login session
$> /etc/init.d/hostname.sh start