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

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at

  # Every Vagrant development environment requires a box. You can search for
  # boxes at = "hashicorp/precise32"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # "private_network", ip: ""

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # for more information.
  # config.push.define "atlas" do |push|
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2

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

  config.vm.provider "virtualbox" do |vb|
     # Display the VirtualBox GUI when booting the machine
     vb.gui = true
     # Customize the amount of memory on the VM:
     vb.memory = "1024"

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 in to a new Virtual Machine!