Friday, January 24, 2014

Introduction to Vagrant on Linux

Modern operating systems, including Linux, have developed over recent years to bring operating system virtualization to the humble PC. Although virtualization itself isn’t new technology and Linux isn’t the only OS that can run virtual machines, the combination of open source technologies along with mature hypervisors means that Linux is able to offer advanced virtualization solutions. Among these solutions is Vagrant – a tool which works with virtualization software like Virtual Box and VMware to provide an easy way to create, configure and reproduce virtual machines with a known state. The ability to easily create multiple virtual machines in a known configuration can help with testing, software development and deployment. It is also useful in small offices or at home (for hobbyists) as test machines can be easily created and allowing experimentation with new software or configurations.
What Vagrant does is take a base guest operating system, say Ubuntu Linux, and create virtual machines that can be started, stopped and destroyed with simple commands. When  a virtual machine is initially created, it can be put into a predefined state – software can be installed, files can be created or copied onto it and other administration tasks can be performed.
To install Vagrant on Linux, visit the downloads page and pick the correct download for your distribution. The project offers packages for Debian and Ubuntu as well as Red Hat, CentOS and Fedora. There are 32-bit and 64-bit versions available. If you aren’t sure whether you have installed a 32-bit or 64-bit version of Linux then use “uname -a” to find out.
For Ubuntu you can install the .deb file using:

sudo dpkg -i vagrant_1.4.3_x86_64.deb
 
Replace “vagrant_1.4.3_x86_64.deb” with the version that you downloaded.
For Vagrant to work, you need to install VirtualBox (or VMware). Download the correct version for your distribution from the VirtualBox Downloads page. Oracle provides packages for Ubuntu, Debian, SUSE, Fedora and others.
For Ubuntu you can install the VirtualBox .deb file using:

sudo dpkg -i virtualbox-4.3_4.3.6-91406~Ubuntu~precise_amd64.deb
 
Replace “virtualbox-4.3_4.3.6-91406~Ubuntu~precise_amd64.deb” with the version that you downloaded.
The quickest way to get Vagrant up and running is to create a directory for the virtual machine (VM) and then run two Vagrant commands, one to initialize the system and one to start the VM. First create a directory and cd into it:

mkdir vagrant-test1
cd vagrant-test1
 
Now initialize Vagrant:

vagrant init precise32 http://files.vagrantup.com/precise32.box
 
This tells Vagrant to initialize and use “precise32″ as the base operating system (called a box). The third parameter tells Vagrant where it can find the files for that box. In case you aren’t familiar with Ubuntu’s code names, precise32 means Ubuntu 12.04 LTS 32-bit. Vagrant also offers official boxes for Ubuntu 12.04 LTS 64-bit (precise64) and Ubuntu 10.04 LTS 32-bit and 64-bit (lucid32 and lucid64). There is also a list of community provided boxes at  http://vagrantbox.es.
As part of the initialization phase, Vagrant will create a file called “Vagrantfile” in the current working directory (i.e. vagrant-test1). You can create as many directories and initialize Vagrant as many times as you like. Each directory and Vagrantfile represent one virtual machine. These different VMs can be based on the same box (i.e. precise32) or on different boxes.
To start the VM run:

vagrant up
 
vagrant up - first time
If this is the first time that a virtual machine has been run with the selected box, then Vagrant will download the .box file from the URL provided and boot it. This may take a few minutes, depending on the speed of your Internet connection, but subsequent boots will be much quicker as the .box file is only downloaded once. If you get a warning about the “guest additions” versions not matching, you can safely ignore it. Any files in the VMs working directory on the host machine will be available in the “/vagrant” directory on the VM. By default, that means just the Vagrantfile.
Once booted, the virtual machine is running in a headless mode (without a monitor or virtual screen) and so you need to connect to it via SSH. You can do this via a normal SSH client from any other machine on your network, or you can use the built-in ssh command. If you want to use another SSH client, note that the correct IP address is the IP address of the host machine (the PC running Vagrant and VirtualBox) but on a different port. During the boot up Vagrant will show how port 22 (for SSH) has been forwarded, it will likely be to port 2222.
To use the built-in ssh command, type:

vagrant ssh
 
You are now connected to the VM. To leave the SSH connection, type “exit” or press “CTRL + D”.
vagrant ssh
To stop a running VM, use

vagrant halt
 
and to delete the VM, use

vagrant destroy
 
When a VM is destroyed, the base operating system (from the .box file) remains stored internally in Vagrant and further VMs can be started whenever necessary without Vagrant downloading the .box file again.
In just two commands (“vagrant init precise32...” and “vagrant up“), Vagrant allowed you to boot up a fully functional, SSH accessible virtual Linux machine. Advanced configuration happens via the Vagrantfile and you can find more details in the Vagrant documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.