DEV Community

Cover image for Exploring Vagrant: Shared Folders - Part 5
Syam SV
Syam SV

Posted on

Exploring Vagrant: Shared Folders - Part 5

In a Vagrant environment, one of the most important features is the ability to seamlessly share files between the host machine (your computer) and the virtual machine (VM) that Vagrant creates. This shared folder functionality streamlines development by allowing you to work on your project files on your host machine while having them automatically available within the VM. By default, the folder where the Vagrantfile is located is automatically shared to the /vagrant folder in the VM's filesystem.

Basic Shared Folder Configuration

To create a custom shared folder between your host and VM, you'll use the config.vm.synced_folder directive within your Vagrantfile. This directive specifies the path on the host machine and the corresponding path on the guest VM where the folder will be shared.

Here's an example of how you can use the config.vm.synced_folder directive within a Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "your-box-name"

  config.vm.synced_folder "host_folder_path", "guest_folder_path"
end
Enter fullscreen mode Exit fullscreen mode

In this example, replace "host_folder_path" with the absolute path of the folder on your host machine that you want to share. Replace "guest_folder_path" with the absolute path of the folder inside the VM where you want the shared content to be accessible.

Example:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048
    vb.cpus = 2
  end 
  config.vm.provision :ansible do|ansible| 
    ansible.playbook = "playbook.yml"
  end 
  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.synced_folder "./data", "/home/vagrant/data"
end

Enter fullscreen mode Exit fullscreen mode

Setting up a shared folder between your host machine and a Vagrant VM is a powerful feature that enhances your development workflow. It allows you to work with your project files using your preferred tools on your host machine while seamlessly making those changes available within the VM environment. By leveraging the config.vm.synced_folder directive in your Vagrantfile, you can easily configure and customize your shared folder setup to suit your project's requirements.

Top comments (0)