Press enter to see results or esc to cancel.

Improving Vagrant performance

Vagrant is a great tool to automate the set up of development environments utilising a virtual machine. Unfortunately, a virtual machine will rarely perform faster than a local install of your development environment. There are plenty of settings to tweak, and we have compiled some common ones that cause issues that are easily fixed.

Increasing allocated CPU cores and RAM in Vagrant

As a start, you should always increase the allocated CPU cores and RAM in your Vagrantfile. This should be based on the resources available on your team’s computers, and the resources required to run your application. As an example, you can increase the resources available to the virtual machine to 4GB RAM and 2 CPU cores by including the below snippet in your Vagrantfile.

Solving slow download speeds in Vagrant/VirtualBox

If you can identify network issues in your virtual machine such as slow download speed, there are a some settings you can try tweaking to optimise this. A common issue is DNS resolution, and this can usually be fixed by including the below snippet in your Vagrantfile.

These 2 settings will force DNS requests by the VM to use the host DNS, rather than external DNS servers.

Enabling multiple cores in Vagrant/VirtualBox

If you have assigned multiple CPU cores to VirtualBox in your Vagrantfile, you should also enable IO APIC so that the virtual machine can make use of the additional cores. You can enable IO APIC by including the below snippet in your Vagrantfile.

If you do not enable IO APIC, you may notice the virtual machine under performing, and using high amounts of CPU in the guest machine (because of lack of availability of the other cores).

Faster folder synchronisation in Vagrant/VirtualBox

The default shared folder setup within Vagrant/VirtualBox can be slow. Enabling NFS on the shared folder can result in huge performance improvements, especially if your application utilises lots of disk caching (such as Symfony and Laravel). You can enable NFS on the default folder setup in Vagrant by including the below snippet in your Vagrantfile.

Faster Vagrant provisioning

For complex application, provisioning your box with the initial “vagrant up” command can take a long time. Fortunately, it’s not necessary to do this for every member of your team, on every computer that will be used for development. You can use the following command to create a filename.box file, which is the already provisioned box.

vagrant package –output filename

You should store this file somewhere suitable, then include the URL in your Vagrantfile. When you next run “vagrant up”, it will download the already provisioned box instead of creating a new one and running the provisioning scripts. You can also include file paths (such as on your local network) as the box_url parameter.

Know any other ways of improving performance of Vagrant/VirtualBox? Leave a comment below!

  • Haris

    Great tips. How did you measure the performance? I would like to measure it and see if it is working or not.

    • One way you could measure before and after performance is with the HTTP benchmarking tool wrk. https://github.com/wg/wrk

      Example:

  • The main problem with latency in Vagrant is network issues, I think. Despite everything, I’ve been unable to get rid of a 7 second latency in every action in WordPress under Vagrant. The problem is, I’ve not found anything better for local WordPress development.

    • I might be late, but I found that updates of lots of files in synchronized tmp folders were responsible for those delays. At least that was true for my Rails app, YMMV.

      Try to configure your app so it will use another path for tmp folders, caches and the like. The sync of thousands of small files slows down Vagrant a lot.