Vagrant: Reproducible Dev Environments with VirtualBox
It’s 2012, and we’re still struggling with the "Works on my machine" syndrome. One dev is on a Mac, another on Windows, and the production server is Ubuntu. Setting up a new project takes days of installing MySQL, Redis, and specific Ruby versions.
Vagrant is changing that. By using a simple Vagrantfile, we can spin up a consistent VirtualBox VM that exactly matches production.
The Vagrantfile
Everything is defined in Ruby. You check this file into Git, and everyone on your team can just run vagrant up.
# Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/precise64"
# Forward a port from the guest to the host
config.vm.network "forwarded_port", guest: 80, host: 8080
# Share a folder from the host to the guest
config.vm.synced_folder "./app", "/var/www"
# Provision with a shell script (or Chef/Puppet)
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2 php5
SHELL
end
Why it's a win
- Isolation: No more "polluting" your main OS with different database versions.
- Parity: Your dev environment is actually Linux, even if you’re using Windows.
- Speed:
vagrant upand you’re ready to code.
In 2012, if you aren't using Vagrant, you're spending too much time on sysadmin work and not enough time on code. Let VirtualBox handle the heavy lifting!