Speed up Vagrant Synced Folders

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"

with:

  • "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.

Vagrant NFS

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:

  1. It will add an entry in the nfsd’s configuration file /etc/exports on your host.
  2. It will reload the NFS server daemon which will read the /etc/exports and accept connections.
  3. 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):
    • "udp" -> "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):
    • "sync" -> "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

You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us

  • Stevo

    Hi Hugo! Thanks for your post! I was wondering if you know if these changes would speed up a synced folder if the host is Mac/Unix. Cheers, Stefan

  • Hugo Lime

    Hi Stefan, I just tested these change on my recently acquired MacBook Pro. The speed to write a file went from ~100MB/s to 300MB/s! Enjoy! Hugo.

  • cesarZubillaga

    hi, i am having this problem when executing this command:

    mount -t vboxsf -o rw,vers=3,tcp,uid=1000,gid=1000

    unknown mount option `vers=3′

    i remove it and then

    unknown mount option `tcp’

    I am looking forward to configure correctly the NFS sync folder so the developments are faster, right now the writing time is so low and my projects are impossible to dev.

    Thanks

  • Hugo Lime

    Hi @cesarzubillaga:disqus, I think that if you want to mount the folder with the mount command, you need to use nfs instead of vboxsf as the mount type.

  • cesarZubillaga

    Hi Hugo,

    Thanks for your reply, my environment is (maybe that helps):

    – Windows 10
    – Vagrant 2.0.1
    – VM 5.2.2

    Vagrantfile:

    Vagrant.configure(2) do |config|
    config.vm.box = “ubuntu/xenial64”
    config.vm.provision :shell, path: “bootstrap.sh”
    config.vm.provider “virtualbox” do |vb|
    # # Display the VirtualBox GUI when booting the machine
    # vb.gui = true
    #
    # # Customize the amount of memory on the VM:
    vb.memory = “8192”
    vb.cpus = 4;
    vb.name = “UBUNTU”
    end
    config.vm.synced_folder “/home/vagrant/code”, “C:Userscesarvagrantbucherer-midas-nfs”,
    type: “nfs”,
    mount_options: [‘rw’, ‘vers=3’, ‘tcp’],
    linux__nfs_options: [‘rw’,’no_subtree_check’,’all_squash’,’async’],
    create: true
    config.vm.network “private_network”, ip: “192.168.56.105”
    end

    Could you reproduce it?

    Thanks a lot.

  • Hugo Lime

    I don’t think linux__nfs_options are useful on Windows. The shared folder should be mounted automatically when doing a vagrant up.