Install Ubuntu Virtual Machine in Minutes with Vagrant

In two minutes, you could have an SSH connection to your new virtual Ubuntu server.
Vagrant allows you to define your virtual host in a simple text file. It can then automatically install your new virtual machine and setup automatic ssh login. Vagrant is great for testing – it’s not suitable for production servers.
Following this short tutorial is easy if you know Linux command line and sudo. On first use, you must install Vagrant and Virtualbox. Later, creating a new virtual machine takes just minutes.

Install Vagrant

$ sudo apt-get update
$ sudo apt-get -y install virtualbox vagrant

If you are using Ubuntu 14.04 LTS, you might need to install latest packages from upstream.

Create Vagrantfile

This is the configuration for your virtual machine. It will automatically download the ready made base system to speed up your work. Even though here we use a ready made binary from third party, you can also create a base system from scratch and verify everything.

$ nano Vagrantfile

Use this ready-made Vagrantfile with all the candy

  • A shared directory between host and guest
  • Provisioning shell script, installs some tools as an example
  • Port Forwarding: you can access guest OS web server from host OS. Just browse to http://localhost:10080
  • Base image (the installed guest OS) is automatically downloaded
# Copyright 2015 Tero Karvinen http://TeroKarvinen.com
# vim: filetype=ruby
$tscript = <<TSCRIPT
set -o verbose
echo "See you on http://TeroKarvinen.com"
apt-get update
apt-get install w3m curl
TSCRIPT
Vagrant.configure("2") do |config|
	config.vm.box = "ubuntu/trusty64"
	config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/14.04/providers/virtualbox.box"
	config.vm.define "server" do |server|
		server.vm.hostname = "server"
		server.vm.provision "shell", inline: $tscript
		server.vm.synced_folder ".", "/vagrant", disabled: true
		server.vm.synced_folder "shared/", "/home/vagrant/shared/", create: true
		server.vm.network :forwarded_port, guest: 80, host: 10080
	end
end

Boot & Log In

First boot takes a couple of minutes, as Ubuntu image is automatically downloaded.

$ vagrant up

That’s it. You can now log in, everything is set up

$ vagrant ssh

Are you in? Congratulations, you can create virtual machines in minutes.
When you are done, ‘exit’ to get back to your host OS. To destroy the guest machine (and all files in it), ‘vagrant destroy’. If you need something from the guest OS, move it to /home/vagrant/shared/. You can then access it on the host OS.

Posted in Uncategorized | Tagged , , , , , , | Comments Off on Install Ubuntu Virtual Machine in Minutes with Vagrant

Comments are closed.