Getting started with VirtualBox and Vagrant
A quick and easy guide to setting up a local Linux environment (using Vagrant on Ubuntu 16.04)
I have simplified the process down to the following steps:
- Install VirtualBox
- Install Vagrant
- Create a local directory for Vagrant
- Install Ubuntu box
- Run vagrant up and provisioning your virtual machine
- Update Vagrantfile
1. Installing VirtualBox
- Head on over to this download page to get the latest version of VirtualBox.
- Download and follow the installation prompts.
NB. At this point, Mac users may encounter (as I do) a warning telling you that the installation has failed. Not to worry if this occurs as there’s a simple solution:
- First, eject the VirtualBox image from: Finder > Devices.
- Then go to System Preferences > Security & Privacy and select ‘Allow’ to make the change:
- With the exception now allowed, if you download the .dmg file again you should achieve success.
2. Installing Vagrant
With VirtualBox now installed, let’s turn our attention to Vagrant. Head over to their download page to grab the latest version. Just as you did for VirtualBox, you will simply need to download and install, following and responding to the prompts.
3. Create a local directory
Create a local directory and then make a shared
folder::
$ mdkir ~/ubuntu
$ cd ~/ubuntu
$ mkdir shared
4. Install Ubuntu Box
You will then need to use Vagrant to build and start your Virtual Machine using an existing image. You can find Vagrant Boxes on the Vagrant Cloud. ubuntu/trusty64
is one of the most popular boxes, so we will go ahead and install this.
$ vagrant box add ubuntu/trusty64:
5. “Vagrant up"
and provisioning your Virtual Machine
Now you can get your box up and running with:
$ vagrant init ubuntu/trusty64:
$ vagrant up
If this has gone to plan, you should be able to log into your new virtual machine using:
$ vagrant ssh
This will give you the following shell prompt on your virtual machine:
vagrant@vagrant-ubuntu-trusty-64
6. Updating your Vagrantfile
Now you will remember that we created a shared
folder in our local directory. You will also notice that there isn’t currently a subdirectory called ‘shared’ in the vagrant home directory.
We can go ahead and change this by updating our Vagrantfile (while we are there we will add some additional config to install python, emacs, git etc. ):
# -*- mode: ruby -*-
# vi: set ft=ruby :Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.box_version = "0.0.1"
config.vm.synced_folder "shared/", "/home/vagrant/shared"config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y git
apt-get install -y emacs
apt-get install -y python3-pip
apt-get install -y pylint3
apt-get install -y golang
apt upgrade -y
SHELL
end
Open up the Vagrantfile in your ~/ubuntu
directory and replace the contents with the above. Once you have added your changes, you will need to run:
$ vagrant reload
This restarts vagrant machine and loads any new Vagrantfile configuration. It is the equivalent of running vagrant halt
followed by vagrant up
. Now when you vagrant ssh
, you will see that there is a shared
subdirectory. This is a mount of ~/ubuntu/shared
on your mac, so files you create in one place should magically appear in the other!
Some basic commands
Sometimes you will need to stop, pause, or destroy your virtual machine. That’s why you will need to be familiar with a few commands:
To turn your VM on, navigate to the directory with your Vagrantfile:
$ vagrant up
To pause your VM, navigate to the directory with your Vagrantfile:
$ vagrant suspend
To turn your VM off, navigate to the directory with your Vagrantfile:
$ vagrant halt
To destroy your VM, navigate to the directory with your Vagrantfile:
$ vagrant destroy
These are just a few of the basic commands you will need to get by, but for a much more in-depth manual, check out the docs from Vagrant.
Next Steps
Congratulations, you’re now logged in and set up on your Ubuntu virtual machine.
Now you should be able to effortlessly create VMs using Vagrant and you can easily SSH right into your very own virtual machine running Ubuntu 16.04