Friday, November 15, 2013

Vagrant + VirtualBox + Ubuntu for linux development

The combination of Vagrant, VirtualBox and Ubuntu allows for some interesting potential.  First of all, its a simple way to build and deploy cloud images similar to Amazon Web Service's AMIs, but all on your local machine.  It is also capable of customizing the virtual machine settings through a configuration file leaving us the opportunity to create full linux development desktop experiences.  Finally, it allows us to run linux containers (such as docker) on Windows and OS X environments in an extremely simple way.

The use case I will be showing in this post is for people who prefer bare metal installs of either Windows or OS X, but would like to have a full screen linux environment such as ubuntu running gnome.  With Vagrant and VirtualBox, this is universally possible.

First, please head to http://git-scm.com and download the git installer.  For windows, make sure to install the unix tools, its worth it.  The installer for windows will get openSSH installed which is required by Vagrant.

Next step:  Install VirtualBox.  Once virtual box is installed, also install the extension pack.

Finally, Install Vagrant.


Once all the dependencies are installed, open up a shell window and lets get started.  We will use a base cloud image from Ubuntu 13.04 found on http://vagrantbox.es.  Vagrant works in project directories, so first create a project folder.  Inside the project folder we will initialize a default configuration file and start an instance.  Once the instance is ready we will install ubuntu-gnome-desktop.

$ cd ~
$ mkdir ubuntudesktop; cd ubuntudesktop

$ vagrant box add ubuntu http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box

$ vagrant init

$ vagrant up




Once you issue vagrant up, a new virtual machine will be provisioned inside virtualbox.  Once the instance loads, vagrant will complete its tasks.  At this point we can use vagrant to SSH into the instance using public keys.

$ vagrant ssh



We have lift off.  Now lets install the desktop environment.  While inside the virtual machine instance through SSH, do the following:

vagrant@vagrant-ubuntu-raring-64:~$ sudo apt-get update; sudo apt-get -y install ubuntu-gnome-desktop

Wait for a while at this point.  Its going to install quite a bit of stuff (1.6GB to be exact).  Once it finishes, exit the ssh session.  Now that we are back on the host terminal session, lets halt the virtual machine and replace its configuration file with a more appropriate desktop capable set of configurations.

$ vagrant halt

Edit the Vagrantfile located inside the current working directory, which should be your project folder.  Replace its contents with the following:


Now that we have updated our configuration, start the instance back up and be amazed.  Yesss.

Now, login as vagrant (password vagrant).


4 comments:

  1. Greetings fellow Badger:

    Great post! It was enough to inspire me to create this: https://github.com/vDevices/Vagrant-LinuxDesktop

    I'm currently trying to adapt your Vagrantfile for use on a Windows XP 32-bit host; and have a couple of questions for ya:

    1.) Where did you find info. on the "--graphicscontroller" & "vboxvga" options (I don't see 'em listed in virtualbox.org/manual/ch08.html/); and
    2.) Is there a particular reason why you did not follow/use the VirtualBox Remote Display Protocol, described in virtualbox.org/manual/ch07.html/?

    TIA,

    -Pablo

    ReplyDelete
  2. Hi the information on the virtualbox specific options are located in chapter 8: http://www.virtualbox.org/manual/ch08.html. Its in there, just search for it under 8.2. Commands overview.

    I didnt use the RDP because it was more sluggish than using the console provided by virtualbox itself. Also, the virtualbox console has extensions for 3d acceleration as well as multiple full screen and clipboard support for copy/paste.

    I originally did use the RDP setting, but found it to be more restrictive than using what was already part of virtualbox.

    ReplyDelete
  3. Base box you are mentioning times out during download, so I used trusty64 base box image and follow above step, but even after installing ubuntu-gnome-desktop and doing vagrant up, I am getting VM's shell opened via virtualbox, why's that ?

    ReplyDelete
  4. Hi Monark. You are right to use trusty64; this blog post is now pretty out of date. As to why your GDM isn't loading I am not sure. Have you tried manually starting it? "service gdm start". Ill take a look at this project when I have a moment and commit any necessary fixes.

    ReplyDelete