Installing Vagrant and Virtualbox
This post will cover how to get started with Vagrant from downloading the software to launching and destroying your first box.
No specific pre-requisites are specified though if you have never heard of Vagrant you may want to take a look at our Introduction to Vagrant post first.
As mentioned I personally favour virtualbox, but you are welcome to look into using other virtualisation products, however the information and videos below they are all focused on using Virtualbox as the virtualization platform.
Download and install
The following applications are required, feel free to download and install them directly.
- Vagrant: www.vagrantup.com
- Virtualbox: www.virtualbox.org
Alternatively take a quick look at the video below on how to download and install them on Windows.
Installing Vagrant on Windows
Your First Project
Let’s cover off the basics of Vagrant by launching your first virtual server.
Create and Initialise a Directory
Create a new folder to store your lab, then from the new folder initialize Vagrant by typing vagrant init this command will create a new file called ‘Vagrantfile’ which can be used to define how the environment should be build.
Open up the ‘Vagrantfile’ file in your favorite text editor to take a look at what’s what.
The first thing you notice is that the file is written in Ruby, now don’t worry of you don’t have any experience with ruby, for what you’ll be using it for it’s quite intuitive.
As you can see the bulk of the documents is commented out with information and links to help compose the file. At the beginning of the configuration you will see a URL to https://atlas.hashicorp.com/search
Atlas and Boxes
Boxes are prebuild base image virtual machines that are ready to use in vagrant. As noted in the introction post Vagrant or more to the point Hashicorp offer a central repository to download share and store images from. That repository is called Atlas
Following the URL provided will take you to the ‘Discover Vagrant Boxes’ page, where you can search for community posted images that you can use for free. It’s worth signing up for a free atlas account while you’re here especially if you are expecting to contribute back by uploading boxes for others to use.
Once you’ve found a box you want to use in your project you can then simply edit the ‘Vagrantfile’ replacing the ‘base’ in the config line
config.vm.box = “base”
with which ever box you prefer. The boxes are entered in the following format ‘username/box’ which is how they are presented on atlas. so for example if you wanted to use the Ubuntu 14.04 image you would enter:
config.vm.box = “Ubuntu/trusty64”
Alternatively if you want to simply download boxes for future use but not use them in the current project you can use the following command from the cli
vagrant box add ubuntu/trusty64
vagrant box add ubunut/trusty64
this will download them into vagrant for use at a later date.
Launching your first box
Is to launch the box and this can be done by running the command
whereby your box will be downloaded.
Once your box has downloaded next time you need to create that box you will not need to re-download the box. This is the same even if you within a different directory
Finally you should be able to ssh into the new box using
you should now be in a fully fledged virtual machine of the image selected. After you’ve seen enough type ‘exit’ to return to your local machine.
Stopping your environment
Suspend does not shut your machines down, it simply pauses them, to allow you to resume from where you left of at a later date. To use this type vagrant suspend, then to resume the environment at a later date simply type vagrant up. The only think to note is that the boxes do remain in your systems memory while suspended.
Shutsdown the boxes, to do this simply type vagrant halt and to boot them again type vagrant up
Destroy allows you to erase your configurations to do this type vagrant destroy The next time you type vagrant up a fresh image from the box will be used.
NOTE: None of these commands remove the box. In order to that you would need to use the vagrant box remove <name> command which we’ll look at in a future post
Hopefully by this point you have a feel for what Vagrant does in a future post we’ll take a more advanced look into how to configure Vagrantfile for building a more complex lab environment.