ThorneLabs

Create a Ubuntu Server 12.04 LTS Vagrant Base Box from Scratch Using VirtualBox

• Updated March 17, 2019


Vagrant makes it easy to spin-up local virtual machines using VirtualBox or VMware Fusion. There are many Vagrant Boxes available to use immediately after downloading and installing Vagrant. However, I prefer to know exactly how my virtual machine image is created. This post will walk you through creating a Ubuntu Server 12.04.4 LTS Vagrant Base Box from scratch using VirtualBox.

If you prefer VMware Fusion to VirtualBox, read how to create a Ubuntu Server 12.04.4 LTS Vagrant Base Box from scratch using VMware Fusion.

Install VirtualBox

First, download and install the latest version of VirtualBox.

Install Vagrant

Second, download and install the latest version of Vagrant.

Prepare the Ubuntu Server 12.04.4 LTS Virtual Machine

The following steps were written for VirtualBox 4.3.8 and may differ for other versions.

  1. Download Ubuntu Server 12.04.4 LTS.
  2. Open VirtualBox and click New.
  3. Give the virtual machine a Name: ubuntu-server-12.04.4-lts-x86_64.
  4. From the Type dropdown menu choose Linux.
  5. From the Version dropdown menu choose Ubuntu (64 bit).
  6. Under Memory size, leave RAM at 512 MB (Vagrant can change this on-the-fly later).
  7. Under Hard drive, select Create a virtual hard drive now, and click Create.
  8. Under File location, leave the default name.
  9. Under File size, change the size to 40.00 GB.
  10. Under Hard drive file type, select VDI (VirtualBox Disk Image).
  11. Under Storage on physical hard drive, select Dynamically allocated, and click Create.
  12. The virtual machine definition has now been created. Click the virtual machine name and click Settings.
  13. Go to the Storage tab, click Empty just under Controller: IDE, then on the right hand side of the window click the CD icon, and select Choose a virtual CD/DVD disk file….
  14. Navigate to where the ubuntu-12.04.4-server-amd64.iso was downloaded, select it, and click Open.
  15. Go to the Audio tab and uncheck Enable Audio.
  16. Go to the Ports tab, then go to the USB subtab, and uncheck Enable USB Controller.
  17. Click Ok to close the Settings menu.
  18. Finally, start up the virtual machine to begin installation.

Install Ubuntu Server 12.04.4 LTS

You can install the operating system manually, using a Ubuntu Preseed file, or using a Kickstart Profile. I will be providing steps to install the operating system manually and using a Kickstart Profile.

Take note, no where in the following steps do I install the VirtualBox Guest Additions. So far, I have not found a need for them. Feel free to install the VirtualBox Guest Additions if you need them.

Follow the steps in one of the following two sections, Manual Installation or Kickstart Profile Installation.

Manual Installation

Install the operating system however you like. Most of the default options can be used.

Once the operating system has finished installing and booted, perform the following post-install steps to make it work with Vagrant.

Login as the vagrant user (password is vagrant).

Install additional repository packages:

sudo apt-get install openssh-client wget curl git man vim ntp

Allow user vagrant to use sudo without entering a password:

sudo bash -c 'echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers'

Create the .ssh keys folder:

mkdir -m 0700 -p /home/vagrant/.ssh

If you want to use your own SSH public/private key then create an SSH public/private key on your workstation (you may already have), and copy the public key to /home/vagrant/.ssh/authorized_keys on the virtual machine.

Otherwise, if you want to use the SSH public/private key provided by Vagrant, run the following command:

curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys

Clean up apt:

sudo apt-get clean

Clean up the tmp directory:

sudo rm -rf /tmp/*

Clean up the last logged in users logs:

sudo rm -f /var/log/wtmp /var/log/btmp

Clean up history:

history -c

sudo history -c

Shutdown the virtual machine:

sudo shutdown -h now

Once the virtual machine is shutdown, open Settings for the virtual machine.

Go to the Storage tab, select Controller: IDE, and click the green square with red minus icon in the lower right hand corner of the Storage Tree section of the Storage tab.

Click OK to close the Settings menu.

Next, jump to the Create the Vagrant Box section.

Kickstart Profile Installation

I used this Kickstart Profile to automate the build.

Start up the virtual machine and when the Ubuntu boot menu appears, select your language, hit the F6 key to bring up Other Options, hit the Esc key, and append the following:

ks=http://public.thornelabs.net/ubuntu-server-12.04-lts-x86_64-vagrant-box.txt

Hit the Enter key and wait for the installation to finish.

At the end of the install, shutdown the virtual machine, and again open the Settings for the virtual machine.

Go to the Storage tab, select Controller: IDE, and click the green square with red minus icon in the lower right hand corner of the Storage Tree section of the Storage tab.

Click OK to close the Settings menu.

Next, jump to the Create the Vagrant Box section.

Create the Vagrant Box

Make sure the value of the base command line switch matches the name of the virtual machine in VirtualBox:

vagrant package --output ubuntu-server-12.04.4-lts-x86_64.box --base ubuntu-server-12.04.4-lts-x86_64

Adding the Vagrant Box

Add the newly created Vagrant Box to vagrant (this will copy the Vagrant Box to another location):

vagrant box add ubuntu-server-12.04.4-lts-x86_64 ubuntu-server-12.04.4-lts-x86_64.box

In addition, the VirtualBox virtual machine can be deleted.

Create a Vagrant Project and Configure Vagrantfile

You can have as many vagrant projects as you want. Each will contain different Vagrantfiles and different virtual machines. So, create a directory somewhere to house your Vagrantfile and associative virtual machines:

mkdir -p ~/Development/vagrant-test

cd ~/Development/vagrant-test

Create the Vagrantfile:

vagrant init ubuntu-server-12.04.4-lts-x86_64

You now have a Vagrantfile that points to the ubuntu-server-12.04.4-lts-x86_64 Base Box you just created.

If you are using your own SSH private/public key, and not the SSH private/public key provided by Vagrant, you need to tell Vagrantfile where to find your SSH private key, so add the following to your Vagrantfile:

config.ssh.private_key_path = "~/.ssh/id_rsa"

Lastly, if you do not want Shared Folders setup between your workstation and virtual machine, disable it by adding the following to your Vagrantfile:

config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

Start Using Vagrant

Spin up your first virtual machine:

vagrant up

If everything spins up properly you can see the status of your virtual machine using vagrant status, you can SSH into your virtual machine using vagrant ssh, or you can destroy your virtual machine using vagrant destroy.

If you found this post useful and would like to help support this site - and get something for yourself - sign up for any of the services listed below through the provided affiliate links. I will receive a referral payment from any of the services you sign-up for.

Get faster shipping and more with Amazon Prime: About to order something from Amazon but want to get more value out of the money you would normally pay for shipping? Sign-up for a free 30-day trial of Amazon Prime to get free two-day shipping, access to thousands of movies and TV shows, and more.

Thanks for reading and take care.