Getting Started with Vagrant for Local Development: Lesson 12 of 20

Provisioning (vm.provision): How to customize the box

Up Next

Quick Recap / Regroup

Autoplaying in 7 seconds!


Provisioning (vm.provision): How to customize the box

Okay, this is great. We created a mini web server locally on our computer using Vagrant. But, Vagrant is supposed to be easy!

Maybe there's a way to automate all this? There is!


Your Vagrantfile can run shell commands after it mounts the VM and builds your "box".

Table of Contents

    Provisioning simply refers to "building" or "adding to" the original box file. So, for example, you could automatically install Apache.

    There's a couple of ways to do this. We can just do this with bash or shell provisioning just like we were.

    Shell Provisioning (Inline)

    You can write your bash commands inline for shell provisioning to work. You do this directly in the Vagrantfile.

    config.vm.provision "shell", inline: <<-SHELL
        apt-get update
        apt-get install apache2 -y

    Now, on vagrant up, your Vagrant will install apache. See how we're creeping closer to full automation?

    Shell Provisioning (Path)

    Another way is to have all your shell scripts in a file. For example:

    # Update Packages
    apt-get update
    # Install Apache
    apt-get install apache2 -y

    Then, in your Vagrantfile, you can reference it like so:

    config.vm.provision "shell", path: ""

    When I do this, I like to turn "root privileges" off. Mainly because I had an issue installing something (I think) Node once using this method. So:

    sudo apt-get update
    sudo apt-get install apache2 -y
    config.vm.provision "shell", path: "", privileged: false