Developing with local tools (IDE, Sublime/Coda/etc) but testing code on a remote machine or local VM can be painful because of the effort required to push the updates to the machine running the application. Vagrant solves this problem with the concept of Synced Folders. For me, this is easy to relate to VMware’s Shared Folders feature of Fusion/Workstation. And as it turns out, this is exactly what’s happening – the Shared Folders feature of the provider is what enables this functionality. This feature allows a local folder to be mirrored with a folder in the guest OS of a virtual machine. What this accomplishes is that very little effort is required to make updates using local tools, but have the changes reflected in the development environment.
Setting up Vagrant Synced Folders
Synced folders are actually enabled by default; the project directory (the one your Vagrantfile lives in) will be shared with the guest OS at
/vagrant. That’s handy enough for getting files back and forth. But the real power of this feature can be shown with a simple example. Let’s say the running Vagrant machine is an Apache server. By sharing the DocumentRoot folder with the host OS, development can be done on the same files that are serving the web page. This means no pushing and pulling to see the changes you’ve made reflected live. Setting up Synced Folders for a given objective is simple: just add the
config.vm.synced_foldermethod to the project’s Vagrantfile, specifying the path on the host machine and then the path on the guest machine.
As an example, on my machine I’m using a Vagrant box to do WordPress development for this site. I’m able to play with CSS and make changes to the site’s appearance without tinkering with the live version. Testing new plugins offline is also something I do with this. In the Vagrantfile for my testing box, the following configuration shares the website files:
config.vm.synced_folder "www/", "/srv/www/", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]
The extra options at the end set permissions on the directory, and specify options to pass to the
mount command. This allows me to use Sublime Text 3 and other tools on my MacBook Pro to manipulate the files actively being served from the web server I have spun up. In a more complex environment, you may want to enable/disable the Synced Folder based on conditionals during project setup. Disabling the share can be accomplished by appending
disabled: trueto the line.
Depending on the environment, a Shared Folders feature for the provider may not be available. But not to fear! A Synced Folder configuration can also be set up to use NFS for Linux machines (or RSync when nfsd is not available on either the host or guest) and SMB for Windows machines. This means that with few exceptions, Synced Folders can be configured in every environment.
See the full official documentation on Synced Folders here, and don’t forget that the project root is already synced to
/vagrant! Next up, a brief look at Vagrant networking.