Last Updated: September 09, 2019
·
70.73K
· akalyaev

Use NFS to speed up your Vagrant

If you are using Vagrant and facing performance hits while running web-server or unit tests, the solution can be using NFS. It is used for sharing folders between host and guest machines.

Vagrant uses VirtualBox default sharing mechanism, which is very slow. After you'll switch to NFS, file access speed will increase by ~ 10 - 100 times.

Installation

  • On your local machine do $ sudo apt-get install nfs-kernel-server nfs-common portmap. This comes pre-installed on Mac OS X 10.5+ (Leopard and higher).
  • Inside your Vagrant box (guest machine) do $ sudo apt-get install nfs-common portmap

Configuration

Set :nfs option to true when configuring shared folder(s).

config.vm.synced_folder "v-root", "/vagrant", :nfs => true

NFS requires Private network, so you need to switch to it, in case you are using other network options, like :public_network (i.e. bridged).

config.vm.network :private_network, ip: "10.11.12.13"

Benchmarks

Grabbed from here.

VirtualBox Shared Folders:         5m 14s
Host File System:                     10s
Native VM File System:                13s
NFS Shared Folders:                   22s
NFS Shared Folders (warm cache):      14s

19 Responses
Add your response

Quite a pity, what Windows 7 doesn't support NFS. Native Virtualbox method of folder sharing is not performant at all.

over 1 year ago ·

Nice tip, thanks, if you use OS X, don't forget to upgrade your vagrant to the latest version (<= 1.2.4 has some bugs with mounting NFS shared folders).

over 1 year ago ·

You can use freeNFS on win 7: http://freenfs.sourceforge.net

over 1 year ago ·

@luuf didn't know that. Thanks for mentioning it here!)

over 1 year ago ·

Just a note about using NFS with Vagrant, since I had great difficulty getting it to work with the correct permissions.

It seems that NFS mounts your shared directory from the host (desktop) on the vagrant VM using the SAME UID/GID found on the host. This means if your UID/GID on the desktop machine is, for example, 500/500, then the NFS share will be mounted on your Vagrant machine with the same UID/GID, which may not correspond to your vagrant user. In my case, my Vagrant user had a UID/GID of 501/501, since another user was already assigned the UID/GID of 500/500. This caused permission problems since the Vagrant user didn't have access to modify the files in the NFS share.

The easiest way to fix this is to reboot the Vagrant virtual machine into single user mode using the Oracle Virtualbox GUI application (enter the Grub boot menu when starting tup the VM, then append a space and the number 1 to the boot parameters), then modify the UID/GID of the vagrant user to match that of your desktop machine.

over 1 year ago ·

@adamcohen If you have different uid/gids between host and client, try this config parameters in your vagrant file. No need to change ids in your vm:

config.nfs.map_uid = Process.uid

config.nfs.map_gid = Process.gid

over 1 year ago ·

@luuf I've tried to use Vagrant with FreeNFS on Windows, but I can't make it work. On the Vagrant side everything seems to be configured properly, as I've tested it on OSX and NFS works there. On the FreeNFS side there is really not much configuration - you only provide path of a folder to mount and nothing more - so I don't know where I could make a mistake.

Have you managed to succesfully use Vagrant / FreeNFS combo on Windows? If so, please share how to configure it properly. Thanks!

over 1 year ago ·

I'm curious on two things, first being like @velesin & @luuf not there is FreeNFS on Windows, but does anyone know if it is stable and worked a lot with it? Also, any easy way to test to see if NFS is there to use? Cheers

over 1 year ago ·

@velesin I only selected a folder to share under the "server" tab on the freenfs app. Works like it should here.

I also have host-only networking (private network) enabled, and use nfs over that network. On Windows I changed the firewall settings to allow any traffic on that network. Vagrant usually handles the network setup, but check that you can ping between host and client. I am unable to test now, as I moved over to LinuxMint and only run virtual Windows.

over 1 year ago ·

@jeremybass It is stable enough for testing/development, but I would not use it in production. I used it a lot before moving on to LinuxMint.

over 1 year ago ·

Thanks! Useful tip!

over 1 year ago ·

I'm having troubles with running NFS shared directories. The error message is:
"mount.nfs: access denied by server while mounting 192.168.7.1:/home/user/work/my-project"

192.168.7.1 is host machine inside the private network
192.168.7.50 is guest machine inside the private network

Some more specifics on pastebin: http://pastebin.com/Yt5Aj0hE
Host is running Ubuntu 13.10 with Vagrant 1.3.5 and Virtualbox 4.2.16. Any suggestions what I may try next?

over 1 year ago ·

This is also perfect because I had a "need for speed".

over 1 year ago ·

by the way, for those of you new at this (like me)
config.vm.synced_folder "v-root", "/vagrant", :nfs => true
can be found in your Vagrant file

over 1 year ago ·

@fire thank you, its a pity I had too read the whole post to find your answer...

over 1 year ago ·

@szoper I had this problem and solved by adding the guesy IP to the host machines /etc/hosts file

over 1 year ago ·

@elbcoast wow, that's so much easier than my solution! Thanks for the tip!

over 1 year ago ·

@markblackmore please elaborate

over 1 year ago ·

thx

over 1 year ago ·