Virtual Machines and Vagrant

If you remember a few weeks ago Chris did a piece on development tools. This week he’s back to discuss Virtual Machines and Vagrant!

If you remember a few weeks ago we did a piece on development tools. Well this week I want to expand on two in particular, why they are helpful and how you use them. To use Virtual Machines you need a fair bit of power, we would suggest you would not try this unless you have at least 8Gb of memory and at least 5Gb of disk space. While you’re reading this if you have not already go download and install VirtualBoxVagrant and optionally Atom for your machine. If you already have a preferred editor then don’t worry about Atom but all of the examples will be given using Atom. Throughout these tutorials look out for the following conventions.

  • Bold text – Indicates a button or input to enter into a programme
  • Monospaced - Indicates text to be entered into a terminal

Most of the applications that I build for the internet sit on machines running one form of Linux or another and I want to be sure that there is the minimal hassle when I deploy my application from my machine and onto the live box and this is where I use a virtual machine (VM). Having a virtual machine allows me to have a completely different operating system and manipulate it without putting my own system at risk. I have in the past (whilst using Windows) used it to install software which I am unsure about, maybe it would install spyware or a virus or trojan, well that’s no problem, all I would do is just delete the virtual machine and all of that is gone, for good. Virtual machines can also have saved states, so for instance if I wanted to upgrade a bit of software or make a major change to the system that I was unsure about. I can create a virtual machine and save a state before making the change, make the change and if it all goes horribly wrong I can just load the previous state. Think of it like a massive undo button. You might be interested to know that Microsoft provides free Virtual Machines of their operating systems with Internet Explorer installed which if you’re developing a web application on anything other than a Windows will help you debug issues in Internet Explorer. However, I will be showing you how to install Linux on one and then making that install a truckload easier with Vagrant.

By now you should have VirtualBox installed so let’s have a look at it.

VirtualBox Main Screen
VirtualBox Main Screen

This is the main screen where you can create a new VM. When we click New we get given a dialogue box so let’s go ahead and fill that in, give your machine a sensible name and then choose Linux and in my case I have chosen Ubuntu 64 bit if your not running a 64-bit system then you might want to choose 32bit here. Then you’re asked how much RAM do you want to give the virtual machine. This is why we advised that you at least have 8GB of Ram, let’s choose 1,512MB for our machine, next on the list is Hard Disk, from the options select “Create a virtual had disk now” and then select VDI and proceed once more to select that the size be “Dynamically allocated”. Next, you give the file a name and state how much space you need, we would advise going with about 8GB here. So with all of those changes made you will see that you now have one entry in your list.

VirtualBox Manager with one machine
Our new VM

We could start our machine right now but it’s got nothing to load. All we have done is create a virtual computer within your computer, we still need to install it with an operating system so let’s go and download Ubuntu 14.04.

Before we go any further you might be asking what is Ubuntu? Why choose Ubuntu? Why choose 14.04? Well, these would be three very good questions all of which I shall answer.

What is Ubuntu?

Ubuntu is a form of Linux.

Why choose Ubuntu?

It’s a very well respected and widely used form of Linux. It uses a lot of good stable bits of software and has a great amount of support. It is an excellent introduction to Linux if you have never used it before.

Why use 14.04?

As of the time of writing 14.04 is the current Long Term Service (LTS) edition, although we are expecting a new LTS to be released within the next few days/weeks. An LTS is very useful because it is supported for a much longer time than other versions, usually, the software that is released to it is less experimental too. I’m choosing an LTS just because I know as a professional it’s generally easier to administer which will make the next few tutorials a bit easier perhaps.

So let’s install it. Go back to VirtualBox and click on your newly created machine, then hit the Settings button. You will be presented with the following screen.

Follow these instructions

As the picture suggest click Storage and then the Empty cd on the left, finally by the CD on the right-hand side and then click Choose Virtual Disk file. When the file selector comes up go and find the file that you downloaded from Ubuntu which should be called “ubuntu-14.04.4-desktop-amd64.iso” and click OK. If you then click Start then your virtual machine should start to install by asking you a lot of questions about language. Go ahead and follow it through, I’m not actually going to install the system with you, we did this just to demonstrate that you can install any OS on your virtual machine. The great news is that if you do it wrong you can just delete or re-install the virtual machine without it breaking your current system. When I develop an app the first thing I do is put together a virtual machine to work with, I can either work on one machine per app or one machine for all apps. It’s often the case that certain apps need certain bits of software or a certain configuration and having to reconfigure a machine when you mess something up can be a total pain and this is where we introduce Vagrant.

Vagrant is a way that you can make multiple Virtual Machines, give each one go them a unique setup and be able to re-create that setup easily. If you have been following along then you should have already installed Vagrant. As a tool, it is a lot more developer focussed so things are going to get a little technical. First of all, I’ll let you have a think where on your hard drive you are going to put our little experiment. Me personally I have a directory called code in my home directory. Your task is to start a command line tool and then ensure that you are in the directory before you read on any further. I’d love to be able to take you through how to do that for your system but the Internet has multiple resources you can tap into for this and it would make this article about twice as long as it is.  Once in your command prompt in the directory you want to use type:

vagrant init

This will create a file called Vagrantfile which is basically a little text configuration file. If you installed Atom then you should be able to also just type:

atom Vagrantfile

Atom should start to edit the file, if not then start Atom and load the file into it.

There is a lot of information in this file and even  more in the Vagrant documentation but don’t worry too much about understanding it all immediately, you can read more docs in your spacetime by all means. The first thing we need to do is choose which Vagrant, box to install. The file suggests that we can search for one but I happen to know which one I want you to play with. change the line that reads = "base"

to = "ubuntu/trusty64"

Now highlight lines 46 to 52 and from the menu Click Edit > Toggle Comments which will turn the section from grey into some pretty colours. next, change the line that reads

vb.gui = true


vb.gui = false

Save the file but don’t close Atom and let’s try it out. Go back to your command line and check that you in the directory that has the Vagrantfile in and then type:

vagrant up --provider virtualbox

Did it break? Well have no fear we have prepared the file for you so you can just download our sample Vagrantfile, put it in place of the one that you have and try once again to start it by typing

vagrant up —-provider virtualbox

With any luck, your machine has now tried to connect to the internet and download the ubuntu image that we told it to. Once it’s done it will return you to a standard command line. It looks like it did something but also looks like nothing happened right? Well, you should now have a virtual machine running on your system. You can start VirtualBox and probably see it and even access it. From here on in we shall call this the Vagrant box.

There are a few points to note here.

  • When you restart your machine your Vagrant box does not automatically start too.
  • You can start, restart, alter, change and destroy your Vagrant box without causing any damage to your current system
  • It is possible to run a script to completely set up the Vagrant box how you like (guess what the next post in this series is going to be about)
  • You can (and should) shut down the Vagrant box when your not using it to get back memory and processor power.

This Vagrant box we installed is very basic and has no GUI and only the basic software on it. You can shut down the machine through VirtualBox or type this command in the same directory as the Vagrantfile:

vagrant halt

You can access it via VirtualBox, or on a command line again in the same directory you had the Vagrantfile by typing:

vagrant ssh

It’s here that we leave you to experiment for a bit. In the next post in this series, we are planning to get your Vagrant box configured and maybe even able to serve a really simple web application before we dive into the world of development in greater detail. In the meantime, we could really do with your feedback. Did you find this easy to follow? Should we do video tutorials for Mac, Linux and Windows? Let us know through the regular channels of comments on this post or Reddit, FaceBook or Twitter

Love and rockets
– Chris

Author: catharsisjelly

Geek, writer, baker and traveller. Open minded introvert with a slight extrovert streak

5 thoughts on “Virtual Machines and Vagrant”

  1. I thought this was a good intro for virtual tools. I am using Virtual Box for my online course–ethical hacking. It has worked pretty good for me. My favourite price too.

    Liked by 1 person

Comments are closed.