Maybe you know this situation: Coming to a new project often requires to install a lot of stuff. Databases, Web servers, Dependencies and all the other stuff is starting to clutter your workstation. If you switch often between projects or you just want to try out a new technology for some days this could be quite annoying.
A few years ago, we all thought virtualization will come to the rescue. Just setup a virtual machine with all required technologies, install your favorite IDE and start development inside
the virtual machine. In fact this approach also has it’s problems. Programs in the virtual machine are responding quite slow, the IDE is not very reactive and if you’ve used a virtual machine for a while it is cluttered with a lot of stuff and getting slower and slower.
Lightweight virtualization with Vagrant
A year ago I discovered Vagrant and since then I’m using it heavily for development and trying out new stuff. Vagrant is taking the approach of using virtual machines to the next step. With Vagrant you can setup a fresh virtual machine within seconds and also destroy it within seconds.
There are several possibilities on how to use vagrant. I’m using Vagrant to separate runtime and development environments by setting up virtual machines with runtime environments
but keeping development stuff on the host machine. That means if I’m developing a Java EE application, I’m using vagrant to setup a virtual machine with Java and an application server like Glassfish. The application itself will be developed, as normal, with an IDE on my machine. To let the application run, I will deploy the application on the application server in the virtual machine. To access the application I can use a browser on my machine to access the application server in the virtual machine. Thus, I can keep my workstation free from runtime stuff but at the same time using the native speed of my machine to develop the application.
One important part is that you can start with a fresh installed virtual machine anytime. That means if you’ve messed up something in your virtual machine you can just throw the virtual machine away and create a new one with exactly the same settings within seconds. Vagrant makes it very easy to do this. In fact I’m starting off with a new virtual machine every day to keep it as clean as possible.
How to start
To give you a small overview about how vagrant works it’s best if you’re trying it out with a small example. Let’s try to run an apache webserver within a vagrant managed virtual machine.
After you’ve installed the two programs you can start creating your first Vagrantfile. Please create a file named Vagrantfile in your home directory.
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "phusion/ubuntu-14.04-amd64"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8000" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 80, host: 8000
config.vm.provision :shell, path: "bootstrap.sh"
The Vagrantfile is the core of vagrant and is required to configure a virtual machine. Let’s look at some details
Here we’re telling vagrant to use Ubuntu as the operating system of the virtual machine. When starting the virtual machine, vagrant will download Ubuntu once and stores it for later usage. That means if you start up the virtual machine a second time or building another virtual machine with the same operating system it is already cached and will not downloaded a second time.
This will make the webserver (port 80) which runs inside the virtual machine accessible to your host machine on port 8000.
That means after starting up the virtual machine, this script will be executed on the virtual machine. This can be used to further configure the virtual machine e.g. by installing additional tools.
Now it’s time to create the bootstrap.sh file. Create it in the same directory as your Vagrantfile.
apt-get install -y apache2
This bootstrap file will download and install apache as a webserver on the virtual machine during startup.
Let’s startup the virtual machine by using a terminal/command line in the folder with the Vagrantfile and executing the following command:
Vagrant will now download the Ubuntu image and install the apache webserver. Depending on your internet connection this can take a while.
After vagrant is done you should see the message “Starting web server apache2” on the console.
The virtual machine is now already running in the background and we can test if we can reach the webserver. You can do this by firing up a browser and check if you can reach http://localhost:8000.
If everything works, you should see the default landing page of apache. Congratulations, you’ve successfully setup a virtual machine and accessed it with your browser.
Working with virtual machines
Now that our virtual machine is up and running we can take a closer look inside the machine. To do this you can open an ssh shell to get inside the virtual machine.
This command will directly ssh you inside the virtual machine. You can take a look around, create or edit files, make everything you can do with a normal machine.
Let’s do an experiment and navigate to the folder /vagrant on the file system and list all the files.
You can see the files which were used to create the virtual machine. In fact we’re now in a folder on the host system. Moreover we have full read/write access to this folder (try it by creating a file here). This means we can easily exchange files between our host machine and the virtual machine. It’s possible to share more folders between the host machine and the virtual machine by configuring it in the Vagrantfile.
To exit the ssh mode you can press CTRL+D.
At some time we have to stop the virtual machine and fire it up later again. There are two possibilities, either persist the changes that are done in the virtual machine or throw the virtual machine away and start with a fresh one.
To stop the virtual machine but keep the changes you can use
and later on
to start the virtual machine again. Please note that the boostrap.sh file will not be executed again, the webserver and all of your changes are still there.
If you prefer to throw the virtual machine away and start fresh (like I do) you can use
to get a fresh copy. In this case the boostrap.sh file will be executed and the webserver will be downloaded and installed again. Note that all of your previous changes are wiped away.
There is a way to preserve changes by extracting a new image from a virtual machine (e.g. to preserve installed programs). This image can then be used as a base for other virtual machines but this will go beyond this article (see vagrant package in the documentation).
I tried to give you a quick introduction to Vagrant and I hope you can see the potential of Vagrant. It can be used to quickly throw some programs into a virtual machine, try them out and then wipe away all of it without cluttering your main machine. It is also a very good utility when you’re trying to separate development and runtime environments.
There is a lot more what you can do with Vagrant. Just make sure to check out the documentation.