Why you should be virtualising your development environments
04 Jul 2013
Within my daily workflow I can work on 5-6 projects over the course of the week and the likelihood of anyone remembering the exact dependencies and requirements for each environment is close to impossible. In addition to this, I need a development environment that will replicate the production environment so I spend less time debugging inconsistencies and more time shipping code. The solution? Virtualisation.
There are multiple methods of virtualising environments however I am going to cover using vagrant as it is simple with nil to very minimal performance overhead.
Vagrant is a CLI tool for interfacing with VirtualBox so be sure to download it prior to trying to use vagrant. I am going to be using my mac however the instructions should be pretty well cross platform.
Vagrant is available for download however it is easiest to install via RubyGems.
Adding a base box
Once that has been installed successfully, you will need to add your box which will house your configuration and OS for the virtual machine. Luckily, there is vagrantbox.es which allows you to get started with a pre-configured base box with an OS already on it. If you are feeling adventerous or need to make your own, there is documentation for creating a custom one! I will be using a default Ubuntu Lenny 32bit base box within the guide so remember to swap it out for your actual box path and name.
To make the box available on your system, you need to add it to vagrant. Easy enough:
You should see some output similar to the following. This is Ruby grabbing the box on your behalf so it can be used within your project. Don’t worry where you do this, it will make it available globally.
Creating the Vagrantfile
The Vagrantfile is the source of configuration for the vagrant instance. You can configure anything to do with the box via this single file. Everything from forwarding ports to sharing directories can be achieved and is all backed by solid documentation. To create a Vagrantfile within your project using your newly added box, simply change to the desired directory and run:
Here is an example of a Rails application with just port 3000 forwarding onto the virtual machine.
Getting onto your new machine
Once the Vagrantfile has been generated and you are happy with it, all you need to do is SSH into the machine using vagrant ssh (be sure you’re in the correct directory with your Vagrantfile when trying this).
Once inside, you can start your services, get new projects rolling, etc. You can also visit your site by visiting http://localhost:3000 (providing the application is running of course :p ). Most people will be fine leaving it at this stage but if you need a solution for multiple developers or other services, you might need to look at integrating puppet or chef into your workflow for provisioning tools.