Adding a Shell Script to Vagrant

Recently we were working with our simple Vagrant and VirtualBox configuration to create a new Ubuntu 14.04 server which will be the baseline for some more blogs I’m building. This is where we start to add a little more to the build process to illustrate some concepts that we are going to work on going forward.

Because shell scripts are a great tool for doing build work on the Linux system, we don’t need to care about what our source environment is. Even if you are running on Windows, we can create a BASH shell script to use with Vagrant because we are working with the guest target, not the host.


We have to know whether you are starting with a brand new configuration, or if you already have the lab built from the first post. If you do have it built, we are going to remove the original machine so that you can see the build process again with our shell script.

To remove the original VirtualBox lab machine, we need one simple command which is vagrant destroy ubuntutest

Much like the first post, you’ll see that I prepend my commands with sudo because I don’t allow my regular OS X user to have root access. You may not need that for your configuration.


Now that we have a clean slate to work from, we will create a file in the same folder as our Vagrantfile and name it which is the extension for a shell script file.

The file will be simple, and look like this:


This is a small file that will run the apt-get update, then install Git, and then create a new file named vagrant_worked.txt in the root of the filesystem.

Once you’re done creating and saving your file, you’re ready to add the new shell script process to your Vagrantfile which will add it to the build process.

It’s as easy as this added line to your existing file: ubuntutest.vm.provision :shell, :path => “”


Now we save our update and we do the classic process of kicking off our build by using our vagrant up command. This time, you will see much more happening in the build process as the shell script actually runs on the console. You can see here that there is a line trigger the provisioning of the shell script at the bottom:


Once our machine is provisioned, we can use the vagrant ssh ubuntutest to open and SSH session and confirm that the script ran with a simple cat /vagrant_worked.txt to see the file contents that we had in our script process:


Et voila! It worked 🙂

This is a simple example, but we will be using this handy shell script provisioning feature much more in future posts.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.