Create an Ubuntu VM Using Vagrant and Virtual Box

I have been using Vagrant for some time with virtual box to play around with vms on my ubuntu machine.
Vagrant is a tool to help create and provision VirtualBox machines.Few of the reasons for using it would be:

1.) The development environment can be isolated from all the other junk that accumulates on my primary computer.
2.) The development environment can be tuned to match a production server environment as closely as possible.
3.) Provisioning scripts define the machine configuration in code. This means the configuration is repeatable and versionable.
So, lets play with it.

I am using Ubuntu 13.04.
First we need to install Vagrant using :

sudo apt-get install vagrant

This will install virtual box 4.2 and other dependencies like ruby (in case you don’t have already)
We can verify the installation using command :

anirudh@anirudh-xebia:~$ vagrant -v
Vagrant version 1.0.3

Now that we have Vagrant,VirtualBox,Ruby installed. Lets try and provision a vm.
We would try and install Ubuntu 12.04 32 bit on virtual box using vagrant.
Choose a location where you need to put your Vagrant file, I made a folder myVMs in home
The command :

anirudh@anirudh-xebia:~/myVMs$ vagrant init precise64 http://files.vagrantup.com/precise64.box

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

We are using the command vagrant init and specifying the version of ubuntu and the path to the pre-configured virtual box vm for it.
Lets open this Vagrant file and see what it has inside it:

........
.......
# Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "precise32"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
........
........

As we can see that syntax of Vagrant files is Ruby.
Here, we see the arguments which we passed in vagrant init populated.
So, what is this “Box” ?
Boxes are the skeleton from which Vagrant machines are constructed. They are portable files which can be used by others on any platform that runs Vagrant to bring up a working environment.
Boxes are provider-specific, so you must obtain the proper box depending on what provider you’re using.
We can use the command vagrant box to configure and add the boxes.
Pretty straight forward.

We can find the list of URLs for the type of base box required at http://www.vagrantbox.es
Also, if we don’t want to give the URL in VagrantFile we can add the box using the command :

$vagrant box add name url

name is a logical name by which the box is referenced from the Vagrantfile. You can put anything you want here, but know that Vagrant matches the config.vm.box directive with this name in order to look up the box to use.Url is the location of the box. This can be a path to your local filesystem or an HTTP URL to the box remotely.

To view all the added boxes use the command :

$vagrant box list

Next, Lets get this machine up by using vagrant up command.
Go to the location where Vagrant file is generated. (In our case it is in ~/myVMs/)
And run the following command :

vagrant up

This will download the vm box from the location specified.
After running the above two commands, you’ll have a fully running virtual machine in VirtualBox running Ubuntu 12.04 LTS 64-bit. You can SSH into this machine with

vagrant ssh

and when you’re done playing around, you can remove all traces of it with

vagrant destroy

With Vagrant, vagrant up is all you need to work on any project, to install every dependency that project needs, and to setup any networking and synced folders so you can continue working from the comfort of your own machine.

About these ads
Tagged with: ,
Posted in continuous delivery
3 comments on “Create an Ubuntu VM Using Vagrant and Virtual Box
  1. […] easiest way to learn chef is by using chef-solo with Vagrant. See my previous post on Vagrant to install vagrant and know more about […]

  2. […] Installation: Vagrant works on Linux, Mac OSX and yes on Windows as well. To get more details on how to install vagrant go to the documentation site here. Or for a more detailed installation on ubuntu refer my blog here. […]

  3. […] Vagrant works on Linux, Mac OSX and yes on Windows as well. We can download the installers based on the choice of OS from here. The complete documentation can be found here. Or for a more detailed installation on ubuntu refer my blog here. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 112 other followers

%d bloggers like this: