Use Rsync to synchronize files to a Vagrant machine

Dec 17, 2016 Vagrant 中文版

Vagrant not only is easy-to-use but also provides us a way to sync files to Vagrant machines through different mechanisms such as NFS, SMB and Rsync. This post goes into how to utilize Rsync to automatically sync files to a Vagrant machine under macOS.



Prerequisite

Luckily, Rsync is already installed in your Mac machine, but it might be a little bit out-of-date. If you like to use the latest version, you can use Homebrew to upgrade. To check the current version:

rsync --version


Enable Rsync for Vagrant machine

Open your Vagrant machine configuration (Vagrantfile), and then add below setting into it:

 ...

  config.vm.synced_folder "/path/to/local/folder", "/path/to/vm/folder",
    type: "rsync",
    rsync__exclude: [".git/", "*.swp"]

 ...

To make it right, you have to specify two absolute paths for vm.synced_folder. In addition, you can exclude files or folders by setting rsync__exclude.

Start auto-sync

When running up a Vagrant machine, the auto-sync won’t start. You have to execute a command to activate auto-sync after the machine is up, like this:

# Run up a Vagrant machine
vagrant up

# After the machine is up, and then start auto-sync
vagrant rsync-auto

If you did everything right, it will start to sync files once you make any changes. The performance and stability of this built-in auto-sync are just okay. Want a better solution? We can install a Vagrant plugin.


You might also like:




If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)

zeckli.devforgalaxy@gmail.com   © 2015-2019 zeckli, thanks to Jekyll and GitHub.