How to make Vagrant performance not suck

Vagrant is an invaluable tool for creating a standardized virtual environments that make it incredibly easy to bring on new developers. Instead of requiring users to install Postgres, Redis, Elasticsearch, etc to be able to run and develop on your app, you just give them 3 simple steps (hopefully they don't even need the first two):

Download & install VirtualBox

Download & install Vagrant

run vagrant up && vagrant ssh from the project's folder

The problem I've run into over and over again is that about 1 out of every 3 people who go through this process end up complaining to me that running the app inside of their Vagrant box is painfully slow. The following is what I've learned from troubleshooting this issue over and over again.

Use the NFS, Luke

By default, VirtualBox shares files between the host and guest operating systems using its own built-in sharing mechanism. While this method works across all types of hosts, it's incredibly slow, at least on Unix hosts. The solution is to use NFS, which is much faster. How much faster, you ask? Simply turning on NFS doubled my Rails app's performance, based on number of requests it was able to serve over a 2 minute period. You can read more about the benchmark methodology and results at the end of this article.

To enable NFS, add the following to your Vagrantfile:

# Required for NFS to work, pick any local IPconfig.vm.network:private_network,ip:'192.168.50.50'# Use NFS for shared folders for better performanceconfig.vm.synced_folder'.','/vagrant',nfs:true

Important caveat: most of this is not strictly Vagrant's fault; the blame lies with VirtualBox, if anything. However, I do wish the Vagrant documentation had a section on performance, because, while the information on NFS is in there, it seems fairly inconsequential until you actually benchmark and realize you're getting half the performance that you could be getting.

Use 1/4 system memory

Most folks just don't seem to bother with telling Virtualbox to use more than the default amount RAM, which is understandable because it's hard to come up with something that works regardless of different host systems. It took me a while, but I put together the following, which should set these values based on each individual host system:

Update: I used to recommend upping the number of CPU cores used by Vagrant, but it has been shownseveraltimes that adding more virtual cpu cores to a Virtualbox VM actually decreases performance. Bummer.

Use vagrant package

The typical Vagrant box setup process involves downloading a base box (usually lucid64 or precise64) and installing the required packages with a provisioner like Puppet or Chef. I've found that there's little value in dealing with the hassle of writing the necessary provisioning scripts. It's much easier to fire up a base box, install everything manually, exit out of the ssh session, and then run vagrant package --output NAME. This will give you a NAME.box file that you can upload to your host of choice (I used S3) and add to your Vagrantfile like so: