RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Explore Advanced Vagrant Features

Learn about Vagrant's complex features, such as synced folders, provisioning scripts and running multiple virtual machines at the same time.


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"

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: "script.sh"

Vagrant.configure("2") do |config|
  config.vm.provision "shell", path: "https://example.com/provisioner.sh"

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
  config.vm.box = "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
  config.vm.network "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
    web.vm.network :private_network, ip: ""

  # 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
    db.vm.network :private_network, ip: ""

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.

Vojislav is a web developer, designer and entrepreneur, based in Belgrade, Serbia. He has been working as a freelancer for more than 6 years, having completed more than 50 projects for clients from all over the worlds, specializing in designing and developing personal portfolios and e-commerce websites using Laravel PHP framework and WordPress content management system. Right now, he works as a full-time senior web developer in a company from Copenhagen.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date