Sunday 9 June 2013

Development Environment Setup Part 2 - Vagrant

Continuing on from Part 1 we now have our base requirements for setting up a new development environment. Using Git Shell/CMD on Windows (or terminal on Mac) create a new folder my_vagrant_folder where we will store the configuration for our soon-to-be-created virtual machine.

Go into that directory and an initialise the vagrant configuration file using the following commands.


 $ cd my_vagrant_folder  
 $ vagrant init precise32 http://files.vagrantup.com/precise32.box  

Vagrant will output some information telling you that it has created a Vagrantfile within that directory. A Vagrantfile contains setup information for a virtual machines provisioning and configuration. It's Ruby based but contains simple enough line by line configuration variables that you can easily modify it without having a background in Ruby.


The above vagrant init command defines a base-configuration precise32 using the box package found in that URL. It's a default Ubuntu Precise virtual machine provided by Vagrant. You can also '$ vagrant init' without  the specific commands to see the default values that go into a Vagrantfile when it's not provided configuration options. You won't be able to proceed further until you fill that information in.


Once you are happy with the Vagrantfile, or using the precise32 one provided above, it's time to get Vagrant to tell VirtualBox to import that Ubuntu box and set it up locally.


 $ vagrant up  

This is also the command you will use when in this folder from now on to turn your virtual machine on via vagrant. It sets up port 2222 by default so you can SSH into the machine to manage it. Other useful commands are '$ vagrant suspend' and '$ vagrant reload' when you want to turn off the VM and completely reload it (for example, if you have changed your Vagrantfile configuration to include new port-forwarding options)


I usually like having port 80 (HTTP) and 8080 for tomcat redirected for web-development. For consistency sake I make port 80 redirect to 1987 or something similar because of Unix-systems locking out redirects on registered ports below 1024 for applications not run as root (virtualbox manual).  This can be done in the Vagrantfile like so:


 config.vm.network :forwarded_port, guest: 8080, host: 8080  
 config.vm.network :forwarded_port, guest: 1987, host: 80  

Run the following to reload the VM if it is currently running so the changes take effect.

 $ vagrant reload 

The Vagrant Documentation includes a lot of good information about this, setting up private networks and creating synchronised folders for easy sharing between the VM and your host system. Although I tend to utilise SCP over this. (SCP is a tool for securely coping files between devices via SSH, remember for your VM you will use port 2222 not 22 for SSH)

Synchronised folders might be helpful if you prefer not to use SCP and/or more comfortable using FTP/SFTP to copy resources across. Depending on your VM setup (it's NAT by default) you will run into one hassle after another port-forwarding the correct ports to establish a FTP handshake then working out which upper-range port it decides to use for the data transfer. (A bridged network helps)


Once your virtual machine is running we can move onto setting up GitHub.


No comments:

Post a Comment