I’ve got a new computer recently and the first thing I did was creating a new partition and install my favorite operating system which is Linux (Ubuntu). As a developer I always like to separate working environments as much as possible. For this reason I create virtual machines to setup my projects.

VirtualBox shared folders feature allows me to write code on the host machine and gets it synchronized with guest machine instantly. This keeps the host machine clean while having all project’s dependencies installed on the guest machine.

In this article I am going to show you how to synchronize a folder in your computer with a virtual machine folder. And also going through some issues that you may face with their solutions.

After installing VirtualBox, it’s time to create a new virtual machine. For me I just went and created a new machine and named it devvm then downloaded the latest Ubuntu server and installed it on this machine.

On your computer go and create a folder. For example go to Documents and create foo folder.

Synchronizing host machine folder with guest machine folder

  1. Select your virtual machine (devvm in my case) then click on settings. Go to Shared folders and click on add new shared folder icon.

    • In folder path choose Other and select the folder we created earlier (In my case Documents/foo).

    • In folder name write shared which is the name of the folder to mount on guest machine.

    • Check auto-mount if it’s not already checked and uncheck Read-only if it is checked.

    • Leave mount point empty and then click on OK.

  2. Start the machine

  3. Create the folder that will be synchronized with foo in your virtual machine. in my case I created a folder named bar. Use mkdir ~/bar to create the folder in your home directory.

  4. Install virtual guest utils sudo pip install virtual-guest-utils.

  5. Use mount command to test that the two folder are synchronized. sudo mount -t vboxsf shared ~/bar -o uid=1000,gid=1000

    • echo $UID to get uid value.
    • id -g to get gid value
  6. Create a new file on one side and verify that it gets created on the other side.

Make mounting permanent

Up to this point everything looks good. Yet, you wouldn’t want to mount the folder every time you start your virtual machine. Therefore, we need to make it permanent.

  1. First we need to update /etc/fstab to mount our folder. Add this command on a new line inside fstab

    • /shared /home/<username>/bar vboxsf defaults,uid=1000,gid=1000,rw 0 0
    • username is your Ubuntu username
    • Empty spaces are tabs
  2. We need vboxsf to be present at boot time. this is why we have to add it to /etc/moduels. Inside modules in a new line write vboxsf

  3. Restart the machine and test again without mount command.

Troubleshoot

For security reasons the guest OS is not allowed to create symlinks by default. If you trust the guest OS to not abuse the functionality, you can enable creation of symlinks for a shared folder – “VirtualBox website”

You may work on a project that requires creating symlinks from guest machine to host machine. For example installing some nodejs modules will require this functionality and it won’t function without it. Symlinks are disabled by default.

To enable symlinks run this command inside your computer VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1 Where VM_NAME is the virtual machine name (devvm in my case) share name is the folder name which is shared

Watching files or directories can be unreliable, and in some cases impossible, on network file systems (NFS, SMB, etc), or host file systems when using virtualization software such as Vagrant, Docker, etc. – “Nodejs.org file system API docs”

When you use Browsersync for example to watch files and folders then it won’t function with the default configuration. This tool and others use fs.watch (Nodejs file system function) to watch changes but as mentioned above it is not going to work on this case. Enabling polling on these tools to use fs.watchFile will fix the issue. Polling is not specific to Nodejs thus, this will work for other libraries too.