Speed up Vagrant Synced Folders
July 11, 2017Hugo Lime2 min read
Do you wish your vagrant synced folders to have better performance? Especially if your host machine is running Linux. Here are some tricks to speed it up.
Set up a synced folder
Vagrant is a convenient tool for quickly setting up your dev server, with just one
vagrant up from the command-line, every team member can test new features locally.
For me, it is also essential to have a shared folder for the app's source code so that I can test my feature by simply saving the file from my favorite IDE and not having to deploy the code into the virtual machine every time.
Setting up a basic synced folder is ridiculously easy as it only requires to add the following line in the Vagrantfile:
config.vm.synced_folder "src/", "/srv/website"
"src/"synced folder path on your host
"/srv/website"synced folder path on your guest
Without additional options, Vagrant will delegate the set up to your virtualization backend, e.g Virtualbox or VMware. Unfortunately, it is sometimes very slow.
Vagrant synced folders provides three alternatives:
- NFS (Network File System): default remote file system for Unix.
- RSync: slower than NFS, you would use it if nothing else works. Plus, rsync is one-way only.
- SMB (Server Message Block): only works with a Windows host.
Using NFS is therefore the best alternative in most situations.
To set up an NFS synced folder you need to have nfsd (NFS server daemon) installed on your host and to specify it in your Vagrantfile as:
config.vm.synced_folder "src/", "/srv/website", type: "nfs"
When you reload your VM with
vagrant reload, vagrant will do three things:
- It will add an entry in the nfsd's configuration file
/etc/exportson your host.
- It will reload the NFS server daemon which will read the
/etc/exportsand accept connections.
- It will connect to your guest machine and mount the remote folder.
Boost your NFS
Now, you might be happy with the default options. But sometimes, especially if you are a Linux user, you might feel that it is too slow. Luckily, Vagrant has a set of available options so let's tweak the NFS configuration a bit.
The NFS options that impact the speed of the synced folder can be separated in two categories:
- Mount options (guest side):
"tcp": The overhead incurred by TCP over UDP usually slows things down. However, it seems that the performance are slightly better with TCP in this particular case. (speed x1.5)
- NFSd options (host side):
"async": in asynchronous mode, your host will acknowledge write requests before they are actually written onto disk. With a virtual machine, the network link between the host and guest is perfect so that there is no risk of data corruption. (speed x3)
If you want to override one option, you also need to write all the other default options. The optimal configuration in my situation is therefore:
config.vm.synced_folder "src/", "/srv/website", type: "nfs", mount_options: ['rw', 'vers=3', 'tcp'], linux__nfs_options: ['rw','no_subtree_check','all_squash','async']
Feel free to test which options work best for you.
With this setup, reloading a page of my app went from 9 to 2 seconds, making my work much easier. Moreover, I can finally access the legacy part of my application which timed out before.
Vincent Langlet, agile web developer at Theodo
Note: File transfer speed can be easily measured with the dd utility :
dd if=/dev/zero of=./test bs=1M count=100