DevOps

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.
 

Reference: Create an Ubuntu VM Using Vagrant and Virtual Box from our JCG partner Anirudh Bhatnagar at the anirudh bhatnagar blog.

Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Bastian
10 years ago

I would discourage from using the Ubuntu Vagrant package (which is *pretty* outdated) and taking the latest installer from the website instead: http://downloads.vagrantup.com/.

Back to top button