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.
- Download Ubuntu Server 12.04.4 LTS.
- Open VirtualBox and click New.
- Give the virtual machine a Name: ubuntu-server-12.04.4-lts-x86_64.
- From the Type dropdown menu choose Linux.
- From the Version dropdown menu choose Ubuntu (64 bit).
- Under Memory size, leave RAM at 512 MB (Vagrant can change this on-the-fly later).
- Under Hard drive, select Create a virtual hard drive now, and click Create.
- Under File location, leave the default name.
- Under File size, change the size to 40.00 GB.
- Under Hard drive file type, select VDI (VirtualBox Disk Image).
- Under Storage on physical hard drive, select Dynamically allocated, and click Create.
- The virtual machine definition has now been created. Click the virtual machine name and click Settings.
- 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….
- Navigate to where the ubuntu-12.04.4-server-amd64.iso was downloaded, select it, and click Open.
- Go to the Audio tab and uncheck Enable Audio.
- Go to the Ports tab, then go to the USB subtab, and uncheck Enable USB Controller.
- Click Ok to close the Settings menu.
- 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
.