Create a Vagrant Base Box based on Ubuntu

By | 30/10/2015

Introduction

In this post, we took the first steps with Vagrant. We have been using an image called ‘precise64’ which could be found at the following location: http://files.vagrantup.com/precise64.box. While this is certainly very nice to get started, I was wondering what it would take to fully create and define your own environment. So that’s what this post is all about.

First off, go ahead and install a normal Ubuntu in Virtual box. I consider this outside the scope of this blog post as I imagine this is fairly easy for most people. Let me give some headstart in case completely new to you:

  • Download Ubuntu ISO
  • Open Virtualbox
  • Create a virtual machine using this ISO (choose a VMDK file)
  • Create a user called ‘vagrant’
  • Install the guest editions
  • Install Openssh server
  • Install the software you would like to see in the resulting Vagrant box file
  • Add vagrant user to sudoers file (see below for more explanation on this one)
  • Install Vagrant public keys to your Ubuntu instal
  • The last two steps might need some more detail so here it comes:

    Add user to the sudoers file

        $ sudo su -
        $ visudo
        # Add the following line to the end of the file.
        vagrant ALL=(ALL) NOPASSWD:ALL
    

    Install Vagrant public keys

    The reason this is needed is to provide the capability to SSH into our virtual machines later on using SSH.

        $ mkdir -p /home/vagrant/.ssh
        $ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
        $ chmod 0700 /home/vagrant/.ssh
        $ chmod 0600 /home/vagrant/.ssh/authorized_keys
        $ chown -R vagrant /home/vagrant/.ssh
    

    If all goes well, you should have a running virtual machine in Virtual Box. As such this has nothing to do really with Vagrant itself. See below screenshots how it will look like.

    Screenshot_VirtualBox

    Ubuntu_Screenshot

    Creating a Vagrant box file

    macbookpro:ubuntu wim$ VBoxManage list vms
    "Ubuntu Development Vagrant Base" {919810de-3097-4845-9bec-45844850e066}
    
    macbookpro:ubuntu wim$ vagrant package --base "Ubuntu Development Vagrant Base"
    ==> Ubuntu Development Vagrant Base: Exporting VM...
    ==> Ubuntu Development Vagrant Base: Compressing package to: ~/Dropbox/Technologies/Vagrant/ubuntu/package.box
    
    macbookpro:ubuntu wim$ vagrant box add "Ubuntu Development Vagrant Base" package.box
    ==> box: Box file was not detected as metadata. Adding it directly...
    ==> box: Adding box 'Ubuntu Development Vagrant Base' (v0) for provider:
        box: Unpacking necessary files from: file:///Users/wauterw/Dropbox/Technologies/Vagrant/ubuntu/package.box
    ==> box: Successfully added box 'Ubuntu Development Vagrant Base' (v0) for 'virtualbox'!
    
    macbookpro:ubuntu wim$ vagrant init "Ubuntu Development Vagrant Base"
    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.
    

    After applying the init file, you will see that a Vagrantfile has been created.

    macbookpro:ubuntu wim$ ls -al
    total 6015432
    drwxr-xr-x@ 6 wauterw  staff         204 Nov  3 13:55 .
    drwxr-xr-x@ 9 wauterw  staff         306 Oct 29 15:28 ..
    drwxr-xr-x@ 3 wauterw  staff         102 Oct 29 16:00 .vagrant
    -rw-r--r--@ 1 wauterw  staff        3043 Nov  3 13:55 Vagrantfile
    -rw-r--r--@ 1 wauterw  staff  3079888994 Nov  3 13:32 package.box
    

    Next step is to uncomment a couple of lines in the Vagrantfile.

        
        config.vm.provider "virtualbox" do |v|
           v.gui = true
        end
    

    Now everything should be ready to launch the virtual machine via Vagrant:

    macbookpro:ubuntu wim$ vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'Ubuntu Development Vagrant Base'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Setting the name of the VM: ubuntu_default_1446555586668_78070
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
    ==> default: Forwarding ports...
        default: 22 => 2222 (adapter 1)
    ==> default: Booting VM...
    ==> default: Waiting for machine to boot. This may take a few minutes...
        default: SSH address: 127.0.0.1:2222
        default: SSH username: vagrant
        default: SSH auth method: private key
        default: Warning: Connection timeout. Retrying...
        default:
        default: Vagrant insecure key detected. Vagrant will automatically replace
        default: this with a newly generated keypair for better security.
        default:
        default: Inserting generated public key within guest...
        default: Removing insecure key from the guest if it's present...
        default: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    ==> default: Mounting shared folders...
        default: /vagrant => ~/Dropbox/Technologies/Vagrant/ubuntu
    

    And since we setup also SSH server on the Vagrant base box, we can ssh into the newly created machine

    macbookpro:ubuntu wim$ vagrant ssh
    vagrant@vagrant-VirtualBox:~$
    vagrant@vagrant-VirtualBox:~$