Setting Up Vagrant On Ubuntu

Setting Up Vagrant On Ubuntu

This is going to be a fun tutorial in setting up Vagrant on Ubuntu Linux. Vagrant is the excellent software by HashiCorp, created by Mitchell Hashimoto. It’s purpose is to make development environments easy. Vagrant enables the software developer, devops engineer, or system administrator to create lightweight and fully reproducible software environments in virtual machines. In this vagrant virtualbox tutorial, we’ll work on an Ubuntu Desktop Virtual Machine and install Virtualbox as well as Vagrant on this VM. From there, we will provision a Virtual Machine within our Ubuntu Desktop, making for a Nested Virtualization configuration.

Summary of Commands Used  
sudo apt-get install virtualbox (installs VirtualBox)
vboxmanage -v (lists version of VirtualBox installed)
sudo apt-get install vagrant (installs vagrant)
vagrant -v (lists version of Vagrant installed)
vagrant init (initialize a new Vagrantfile)
vagrant up (launches the Vagrant environment)
vagrant ssh (SSH into VM)
uname -a (print linux distro information)
vboxmanage list runningvms (list VMs running in VirtualBox)
vagrant reload (restarts VM and loads new Vagrantfile)

Install Virtualbox On Ubuntu

The first step we will complete is to install Virtualbox on our Ubuntu system. A key benefit of Vagrant is that it abstracts virtualization. In order to to this, we will need to make use of a hypervisor on Ubuntu to run the Virtual Machines Vagrant will provision. The most popular provider for Vagrant is VirtualBox which is a cross platform virtualization software from Oracle. To install VirtualBox on Ubuntu desktop, open a terminal and type sudo apt-get install virtualbox like so.

sudo apt-get install virtualbox

apt-get install virtualbox

Confirm VirtualBox is Installed

Once everything finishes from the first step and you are back at the command line, you can check the version of VirtualBox that is installed on Ubuntu by typing vboxmanage -v.

vboxmanage -v

vboxmanage -v

Install Vagrant On Ubuntu

With VirtualBox now installed, we can move forward and install Vagrant. At the terminal you can type sudo apt-get install vagrant to complete this task.

sudo apt-get install vagrant

apt-get install vagrant

Confirm Vagrant is Installed

We can also check to make sure Vagrant installed successfully from the terminal via the vagrant -v command.

vagrant -v

vagrant -v

Create Your First Vagrant Provisioned Virtual Machine

From the home directory, we will create a new ubuntu directory to work in.  Once we cd into that new directory, type vagrant init hashicorp/precise32 and hit enter.  This is where the magic starts to happen.  With this command a new configuration file of sorts is placed in the directory named Vagrantfile.  The command tells us we are now ready to run the vagrant up command to create our first virtual environment.

vagrant init

vagrant init
Paying attention to this important message, let’s have a look at the Vagrantfile itself.

The Vagrantfile

Bring Up The Default Machine

Let us now bring this puppy online. Type vagrant up at the command line and watch the magic unfold.

vagrant up

vagrant up
It will take a little bit of time for the image that contains Ubuntu Precise Linux to download. What Vagrant is doing in this step is to create and launch a virtual machine running Ubuntu Linux. Everything is configured for you automatically, including SSH. When the process completes, you can SSH with the vagrant ssh command from the Guest into the Virtual Machine. Let’s try that now.

vagrant ssh

vagrant ssh
You are now logged into the Ubuntu Precise Linux Virtual Machine via SSH from the Host machine. Notice the new prompt of vagrant@precise32:~$. Go ahead and type a few linux commands to test things out.

uname -a

uname -a
The output of this command is Linux precise32 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux, so we know that the VM is working perfectly. Finally, we can log out of the SSH session, and just list and confirm running virtual machines via VirtualBox. We can do that with the vboxmanage list runningvms command like so.

vboxmanage list runningvms

vboxmanage list runningvms

Awesome! Well how about that?! A fully functional Ubuntu Virtual Machine that was provisioned with just a handful of commands in Vagrant.

Adding a GUI Interface To The Virtual Machine

Vagrant boots up the virtual machine in a headless state. That is to say, it has no GUI of it’s own, and the only way to log into the VM is with SSH. This can be configured in the Vagrantfile by uncommented a few lines and then reloading the VM. Uncomment the section of the Vagrant file like so.

With this modification to the Vagrantfile, we can now reload the virtual machine with the vagrant reload command.

vagrant reload

vagrant reload with gui
During this reload of the virtual machine, the newly booting VM will launch it’s own GUI you can log into without the need for SSH. The user name and password are both vagrant. From here, we can interact with the virtual machine using all the same commands as if we were connected via SSH. One final way for us to prove this Virtual Machine is in fact alive and well is to launch the Virtualbox GUI itself. To do so, just type virtualbox at the terminal. Notice a new GUI launches that gives you information about the running virtual machine. We can see the name, operating system, base memory, boot order, acceleration, video memory, IDE controller and more.
launch virtualbox gui on ubuntu

Setting Up Vagrant On Ubuntu Summary

Well there you have it, a full installation of Vagrant on Ubuntu along with provisioning and launching our first Virtual Machine via Vagrant with VirtualBox as the provider. We covered installing VirtualBox from the command line then confirmed it was installed properly. Next up we installed Vagrant and also confirmed it was installed properly at the command line. Next up we made use of the Vagrant commands vagrant init, vagrant up, and vagrant ssh to provision, launch, and log into a new Virtual Machine!