About 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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Create an Ubuntu VM Using Vagrant and Virtual Box"

  1. Bastian says:

    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/.

Leave a Reply


− one = 1



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close