Explore Advanced Vagrant Features

Explore Advanced Vagrant Features

In First Steps with Vagrant, you learned how to install Vagrant, set up and run a basic virtual machine. Now, we will cover a few more complex features, such as synced folders, provisioning scripts and running multiple virtual machines at the same time.

Synced Folders

Synced folders allow us to share folders and files between the virtual machine and the host machine. The changes are immediate and are applied in both directions???changes made on the host machine will be visible on the virtual machine and vice versa. Synced folders are mapped in Vagrantfile:

Vagrant.configure("2") do |config|  # other config here  config.vm.synced_folder "home/myuser/sites", "/var/www"end 

The first path is the directory location on the host machine, while the second path is the location of the directory on guest machine (virtual machine). The default owner of the folder on the virtual machine is ?vagrant? (the root user), so we often need to manually specify the owner:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data" 

If multiple synced folders are needed:

config.vm.synced_folder "home/myuser/sites", "/var/www", owner: "www-data", group: "www-data"config.vm.synced_folder "home/myuser/documents", "/home/vagrant/documents" 

We can also change the file and directory permissions:

config.vm.synced_folder "home/myuser/public", "/home/vagrant/public", :mount_options => ["dmode=777", "fmode=666"] 

Sometimes the default shared folder implementation (Virtualbox shared folders) will have high performance penalties. In those cases, it is possible to change syncing mechanism to NFS, RSync or SMB. Read more about it here.


Vagrant offers a wide variety of boxes. A box is a downloadable virtual box, which contains an operating system and some pre-installed software. In most cases, one of the many available boxes will fit your needs. But sometimes, you might want to install additional software or change the configuration. That?s the situation in which you will use a provisioning script, i.e. a script that will automatically execute predefined commands upon starting the virtual machine.

A provisioning script is usually a shell script, which can be written inline in Vagrantfile, saved to an external file or called from an external link:

Vagrant.configure("2") do |config|  config.vm.provision "shell", path: ""endVagrant.configure("2") do |config|  config.vm.provision "shell", path: ""end 

Ansible, Chef, Docker and Puppet provisioners are also available.

Creating Multiple Virtual Machines

Using this feature, we can accurately represent and test systems with multiple servers. As with all other features, they are configured in Vagrantfile:

Vagrant.configure("2") do |config|  # This configuration applies to all virtual machines  # Set the box to Ubuntu 14.04 64-bit OS = "ubuntu/trusty64"  # Map port 8080 on the host to port 80 on the virtual machine  # http://localhost:8080 should open the default Apache page on the virtual machine "forwarded_port", guest: 80, host: 8080  # This configuration is applied only to the virtual machine named ?web?  config.vm.define "web" do |web|    # LAN IP address for the virtual machine :private_network, ip: ""  end  # This part is applied only to the second virtual machine named ?db?  config.vm.define "db" do |db|    # LAN IP address for the virtual machine :private_network, ip: ""  endend 

To run a virtual machine, you would enter vagrant up web or vagrant up db. To SSH into a virtual machine, you would use vagrant ssh web?or vagrant ssh dbNext Steps Explore available Vagrant boxes?or learn how to make a custom one.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist