Skip to main content

Managing Synced Folders

1. Creating Basic Synced Folder

Option Values Description
source ./local/path Path on the host machine to sync from
destination /vagrant/remote/path Path on the guest machine to sync to
type nfs, smb, rsync Sync method (default is virtualbox)

Example: Basic synced folder configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./src", "/app/src"
end
Note: By default, Vagrant uses VirtualBox's shared folders which are slow. For better performance, consider using nfs or rsync.

2. Disabling Default Sync

Option Values Notes
config.vm.synced_folder nil Disables default synced folder

Example: Disable default synced folder

Vagrant.configure("2") do |config|
  config.vm.synced_folder nil
end

3. Using NFS Synced Folders

NFS (Network File System) synced folders provide high-performance file sharing between the host machine and the Vagrant guest. They are particularly useful for large codebases or when performance is critical.

Option Values Description
type "nfs" Specifies the synced folder type as NFS
source "/host/path" Path on the host machine to sync
destination "/guest/path" Path on the guest machine to sync to
nfs_export true | false Enables or disables NFS export (default: true)
nfs_version 3 | 4 NFS protocol version to use (default: 3)

Example: Basic NFS Synced Folder Configuration

# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  config.vm.synced_folder "./app", "/var/www/app", type: "nfs"
end
Note: NFS requires the host machine to support NFS exports. On macOS and Linux, this is typically enabled by default. On Windows, additional configuration may be required.
Warning: NFS synced folders do not work on all platforms. Ensure that your host OS supports NFS before using this feature.

Key Considerations for NFS Synced Folders

  • NFS is faster than default synced folders for large files or frequent reads
  • Requires NFS support on the host machine (not available on all Windows systems)
  • Can cause issues with file permissions or ownership
  • Does not support all Vagrant features like mount_options

4. Configuring SMB Synced Folders

Option Values Description
synced_folder smb Specifies the synced folder type as SMB
source /host/path Path on the host machine to sync
destination /guest/path Path on the guest machine to sync to
smb_host 192.168.1.100 IP address of the SMB server (optional)
smb_username user Username for SMB authentication (optional)
smb_password password Password for SMB authentication (optional)
smb_share shared_folder Name of the shared folder on the SMB server (optional)

Example: Basic SMB Synced Folder Configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder "/host/path", "/guest/path", type: "smb"
end

Example: SMB Synced Folder with Authentication

Vagrant.configure("2") do |config|
  config.vm.synced_folder "/host/path", "/guest/path",
    type: "smb",
    smb_host: "192.168.1.100",
    smb_username: "user",
    smb_password: "password"
end
Note: SMB synced folders require the vagrant-smb plugin to be installed on the host machine.
Warning: Ensure that the SMB server is accessible from the guest machine and that proper permissions are set.

5. Using RSync Synced Folders

Option Values Description
type "rsync" Specifies the synced folder type as RSync
source "/path/to/local/dir" Path to the local directory to sync
destination "/vagrant/dir" Path to the synced directory in the guest machine
rsync__args ["--verbose", "--archive", "--delete"] Additional arguments to pass to rsync command
rsync__exclude ["*.tmp", "*.log"] Patterns to exclude from sync
rsync__auto true Automatically sync on changes (default: true)

Example: Basic RSync Configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

Example: Advanced RSync Configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./src", "/app/src", type: "rsync",
    rsync__args: ["--verbose", "--archive", "--delete"],
    rsync__exclude: ["*.tmp", "*.log"]
end
Note: RSync synced folders are ideal for large files or when you need to sync files in both directions. They use the rsync command under the hood, which is efficient for incremental file transfers.

6. Setting Owner and Group

Option Values Description
owner string Username or UID of the owner for synced folders
group string Group name or GID of the group for synced folders

Example: Setting Owner and Group

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./local", "/vagrant", owner: "www-data", group: "www-data"
end

7. Configuring Mount Options

Option Values Description
mount_options ["dmode=777", "fmode=666"] Array of mount options to pass to the underlying file system
type "virtualbox", "nfs", "rsync" Specifies the type of synced folder to use
disabled true, false Disables the synced folder entirely if set to true

Example: NFS Mount with Custom Options

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./app", "/vagrant/app", type: "nfs", mount_options: ["dmode=777", "fmode=666"]
end
Note: NFS mount options are only supported on Unix-like systems (Linux, macOS). On Windows, Vagrant will fall back to the default synced folder implementation.

8. Setting Folder Permissions

Option Values Notes
synced_folder "/host/path" Path on the host machine
guest_path "/guest/path" Path on the guest machine
type "default" | "nfs" | "rsync" Type of synced folder
owner "vagrant" Owner of the synced folder on guest
group "vagrant" Group of the synced folder on guest
mount_options ["dmode=755", "fmode=644"] Options for mounting the folder

Example: Setting Folder Permissions

Vagrant.configure("2") do |config|
  config.vm.synced_folder "/host/app", "/app",
    owner: "vagrant",
    group: "vagrant",
    mount_options: ["dmode=755", "fmode=644"]
end

9. Excluding Files

Option Values Notes
config.vm.synced_folder source, destination, type, exclude Synced folder configuration options

Example: Exclude specific files from synced folder

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./src", "/vagrant/src", exclude: [ "*.log", "temp/" ]
end
Note: The exclude option supports glob patterns to match files or directories.

10. Using VirtualBox Shared Folders

Option Values Description
config.vm.synced_folder source, destination, type Defines a synced folder configuration
type virtualbox Specifies VirtualBox shared folder type

Example: Configure VirtualBox Shared Folder

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./host/path", "/guest/path", type: "virtualbox"
end
Note: VirtualBox shared folders are not supported on all guest operating systems. Ensure the VirtualBox Guest Additions are installed in the guest OS.

11. Configuring Sync Behavior

Option Values Description
type default, nfs, smb, rsync Sync method to use. Default is default (VirtualBox shared folders)
source string Path on the host machine to sync from
destination string Path on the guest machine to sync to
disabled true, false Disables the synced folder if set to true
owner string User to set ownership of synced files
group string Group to set ownership of synced files
mount_options array Options to pass to the mount command (e.g., ["dmode=777", "fmode=666"])

Example: NFS Sync Configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./src", "/app/src", type: "nfs"
end

Example: Rsync Sync Configuration

Vagrant.configure("2") do |config|
  config.vm.synced_folder "./dist", "/var/www/dist", type: "rsync"
end
Note: NFS is recommended for better performance on macOS and Linux, while rsync is more portable across platforms.

Was this article helpful?