Tutorial

How To Use Vagrant on Your Own VPS Running Ubuntu

Published on July 30, 2013
How To Use Vagrant on Your Own VPS Running Ubuntu

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Vagrant

Vagrant is a powerful open source software for configuring and deploying multiple development environments. It is designed to work on Linux, Mac OS X, and Windows and although it comes with VirtualBox for the virtualization needs, it can be used also with other providers such as VMware or AWS.

This tutorial continues a previous one in which we looked at installing Vagrant and setting up your first simple guest machine running Ubuntu on top of your existing DigitalOcean VPS. It therefore assumes you have followed the steps covered in it and are familiar with its lessons.

Boxes for VirtualBox

As I mentioned in the previous tutorial, Vagrant comes with VirtualBox installed by default as its provider. This means that the box we added (precise32) is specifically made for VirtualBox and won't work with other providers. The precise32 box is an image for a guest machine running Ubuntu Precise 12.04 32 bit. However, there are other boxes you can add that come with different operating systems.

The Vagrant github project gives you the official links to the other boxes you can add to your VirtualBox powered Vagrant. There are 3 more available: precise64 (the 64 bit version of Ubuntu Precise 12.04), lucid32 and lucid64 (for the Lucid distribution of Ubuntu 12.04). These are the official ones.

There is, however, a community website with lots of other boxes available for VirtualBox and various other providers. This list, however, has not been verified by the Vagrant project and are to be used at your own risk. But if you want to add any of these to your Vagrant, follow the same procedure as you did in the previous tutorial.

Automated Provisioning

Now that you know what boxes are and how to add them to your Vagrant, you are probably wondering what's next? You can configure a Vagrant file to use a box to quickly power up a guest machine running Ubuntu, but then?

Lets say you want to install Apache on the Ubuntu guest machine you played with in the previous article. You can SSH into it and do it manually. However, this means that whenever you or anyone else wants to work with this guest machine after destroying it, all these operations need to be performed over and over.

One of the cool things about Vagrant is automatic provisioning. It means that you can specify a shell script to be run every time you use the vagrant up command. So let's say that we want our guest machine to always be deployed with Apache installed on it. To do this, we can put all the necessary commands to be performed into a file in the project root folder (next to the Vagrantfile):

nano bootstrap.sh

Inside this newly created file, paste the following script:

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
rm -rf /var/www
ln -fs /vagrant /var/www

This script installs Apache and links the /var/www folder to the /vagrant folder (the one that gets synced with the host machine project root folder, if you remember). This way you can edit the files for the Apache server from your host machine and you don't need to worry about synchronization.

Next, we need to edit the Vagrantfile and specify that when the guest machine is being deployed, it should use this script:

nano Vagrantfile

In this file, right below where we specified the default box to use, add the following line:

config.vm.provision :shell, :path => "bootstrap.sh"

This basically tells Vagrant to use the shell provisioner and run the script located in that file (the path to which being relative to the project root folder of the host machine).

Save the file and run the vagrant up command to deploy a new guest machine which will now come with Apache installed. If you are already running the guest machine, you can reload it with the following command:

vagrant reload

This will restart the machine and run the provisioning. You will now have Apache installed on the guest machine every time you run vagrant up or vagrant reload.

Networking

The purpose of installing Apache on your guest machine is obvious: you want a VPS to serve pages to a browser. But there is no way yet to access the guest machine from the browser. Enter the networking and port forwarding features of Vagrant.

Port forwarding lets you specify a port on your host machine to be shared with the guest machine. This means that if you point your browser to the address of your host using a particular port, you'll end up on the guest machine. To do this, navigate on your host machine to the Vagrant project root folder and edit your Vagrantfile:

nano Vagrantfile

Under the lines you added above when you did the provisioning, paste in the following line:

config.vm.network :forwarded_port, host: 4567, guest: 80

This specifies that if the port 4567 is requested on the host machine, it should route the request to port 80 on the guest machine (which is the default Apache port). Save the file and run vagrant up if the guest machine is not already running. If it is, you can use the vagrant reload command.

If you used the bootstrap.sh script I showed you above, you made it that the Apache server installed on the guest machine will find its files to serve in the /vagrant folder of the guest machine which in turn is synced with the project folder on your host machine. So go to whichever of these folders is closest to you and create a test index.html file with some random text in it.

Now if point your browser to your host machine and specify the port 4567, the index.html page should be served by the Apache server of the guest machine. If you have installed Vagrant locally, try the following url:

http://127.0.0.1:4567

If you are using it on a remote server, try the following:

http://192.119.208.208:4567/

Make sure you replace the IP with the one corresponding to your remote host machine.

Conclusion

In this tutorial, you learned a bit about what boxes you can use with your Vagrant, how to set up provisioning to run various scripts automatically with every vagrant up command and you saw how to use port forwarding to access the guest machine from the browser. These are powerful features indeed, but Vagrant gives you even more if you know what you are doing.

In the next tutorial, we will look at how to add a new provider to Vagrant, namely to use DigitalOcean droplets as the guest machines you deploy. For this, we will need to install a Vagrant plugin.

Vagrant—Article #1

Vagrant—Article #2

Vagrant—Article #3

Article Submitted by: Danny

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

@migzter Check out this video, it answers your question: What does #!/usr/bin/env bash do? - YouTube

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
March 1, 2014

What does

#!/usr/bin/env bash

do?

Great article!

Suggestion: There may need to be an update to this, based on the latest docs [http://docs.vagrantup.com/v2/getting-started/provisioning.html].

In order to provision the host, I needed to run: <code> vagrant reload --provision <code>

~ Kyle J

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel