Sync Your Project Directory To Your VirtualBox VM

One of the things I enjoy about Laravel’s Homestead is the automated syncing that happens between your local project directory and the virtual machine, so any changes you make locally are immediately transferred to the VM on save, thus making changes quick and easy. I wanted to set up the same thing for a VM of a non-Laravel project recently.

I started by firing up the VirtualBox GUI, accessing the settings for my individual VM (let’s call it vm-name) and went to the section labeled Shared Folders. This is where you can set up links between your VM and local directories on your machine. Clicking on the “add new” icon, I proceeded to enter the path to my project directory: /Users/joshanthony/projects/project-name and the name of the folder I wanted to store things in on the VM: project-name. After that, I ticked the “Auto-mount” and “Make Permanent” properties; “Make Permanent” will make the shared directories available on the next boot and “Auto-mount” will handle mounting the directory. I then clicked “OK” to save the changes.

At this point, my project directory will be shared with the machine; however, it will be stored in a location of VirtualBox’s own choosing (the location will depend on what OS the VM is running). I’d like to store it in /var/www/vhosts, the VM’s websites directory. VirtualBox’s GUI doesn’t allow you to change that, so it’s time to get in the console!

I entered the following commands:

VBoxManage guestproperty set vm-name /VirtualBox/GuestAdd/SharedFolders/MountDir /var/www/vhosts
VBoxManage guestproperty set vm-name /VirtualBox/GuestAdd/SharedFolders/MountPrefix /

For both commands, we need to specify the name of the virtual machine we want to change; in this case, vm-name. The first command changes the directory our shared folders mount to. That might seem like enough, but by default VirtualBox also adds a prefix to the paths all mounted directories (sf_, for me), which in this case would result in our shared directory being saved to sf_var/www/vhosts, which isn’t what I want at all. Thus, the second command changes the mount prefix to a simple /; it might seem we can just set it to an empty string, but this just causes the property to be reset to its default value of sf_, so we want to use the slash.

When I was searching for how to do this, I found a number of suggestions that involved running the above commands on the VM itself using VBoxControl instead of VBoxManage; however, for me this resulted in failure due to the properties being marked guest read-only. That’s why I went the VBoxManage route.

Once the commands are run, all that should be needed is to reboot the VM, and things should work! If they don’t the most likely culprit is the web server user not having permission to access the project directory. Running sudo usermod -G vboxsf -a [server-user] (where server-user = the server’s username) should resolve this issue.

Now I have my local project directory synced with its web directory on the VM, and any changes I make locally get instantly reflected on the VM. Real-time changes FTW!