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.
Vagrant.configure(2) do |config| # ... config.vm.provider "virtualbox" do |v| v.memory = 4096 v.cpus = 2 end end
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.
Vagrant.configure(2) do |config| # … config.vm.provider "virtualbox" do |v| # … v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] v.customize ["modifyvm", :id, "--natdnsproxy1", "on"] end end
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.
Vagrant.configure(2) do |config| # ... config.vm.provider "virtualbox" do |v| # ... v.customize ["modifyvm", :id, "--ioapic", "on"] end end
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.
Vagrant.configure("2") do |config| # ... config.vm.synced_folder ".", "/vagrant", type: "nfs" end
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.
Vagrant.configure("2") do |config| # ... config.vm.box = 'filename' config.vm.box_url = 'http://your-url.com/filename.box' end
Know any other ways of improving performance of Vagrant/VirtualBox? Leave a comment below!