We've been using Vagrant (with VirtualBox) at LETO for a few years now, and it has been really helpful in different ways. In this blog post, I'll talk about these benefits and also on how to set it up.
Less than 10 minutes to start working on the project
It's a great developer experience if you can start working on the project within 10 minutes.
vagrant up && vagrant ssh and you have the latest code up and running. Especially if you have sophisticated setup. I remember a project where it took more than 4 hours to set everything up manually. Manual setup only leads to frustration.
Better dependency management
If you have to run some old database, you can safely install it on the virtual server, and it won't impact other servers.
Plus, it's much easier to run services in the virtual servers as you can shut them down and don't worry about extra processes running on your laptop.
It's also helpful when you can't update the dependency on the server and have to run old version for months, even years.
You can run many servers the same time
This one won't be relevant to most developers, but when managing many projects I found it a huge win. When it boots, vagrant automatically detects if the port is available and assigns a new port if it's not. Depending on the resources of your laptop you can run 3-4 different projects without any issues.
Working with the same dev environment as your production.
This was a huge win for us. If run the code without virtualization you might have a system library and the server works fine. But when you deploy the code everything breaks, you start debugging everything on a staging server or worse, on production.
Mimic production environment
If you have multiple types of servers (one database and few web servers) you can recreate the same setup in a vagrant and allow these servers to talk to each other over the private network. You won't be running it every time, but it's been useful on more complex setups. It helps to spot and debug weird network issues.
Download and install
Once it's done, confirm you can access vagrant via CLI
Initial project setup
Let's add a Ubuntu 16.04 server:
vagrant init ubuntu/xenial64. If you want different OS, check them on Hashicorp's Atlas.
Init command will place a Vagrantfile in your current directory. Check the comments in this file, they are a great start to understanding how to configure it. It's unlikely that you will be using
init command in the future, most likely you will have just one Vagrantfile which you will be copying over.
vagrant up to actually run the server. Once it's booted you can ssh into it with
In the Vagrantfile you will need to make few changes to use it for web development.
- Port forwarding
By default, Vagrant doesn't forward any ports. I.e. if you run nodejs server you won't be able to access it from your local machine. To access it add forwarded_port:
config.vm.network "forwarded_port", guest: 3000, host: 8080. This command will forward port 3000 running in the virtual server to port 8080 on your OS.
- Share files between guest and host machines
If you launch Vagrant with default config it doesn't sync folder, so you won't be able to access any files from your host machine. To add the sync add:
config.vm.synced_folder "./host_data", "/vagrant_data"This line will sync
host_datafolder on your host machine with
To ensure that your team members can use the same virtual server make sure you add provisioning. Which is a set of commands that runs when you first run
vagrant upor run
vagrant provision, by default Vagrant adds following example in the comments:
config.vm.provision "shell", inline: <<-SHELL sudo apt-get update sudo apt-get install -y apache2 SHELL
But you can use any other provisioning software - from Ansible and Chef to Docker. We prefer Ansible, it's not ideal, but it does the job.
vagrant plugin install vagrant-vbguest- it will install a vagrant plugin that will monitor your VirtualBox Guest Additions and install the latest version if required automatically. VB Guest Additions is responsible for stuff like file system sync, if it's not installed or version isn't correct this sync between guest and host might fail.