System ProgrammingLearn How to Set Up a Vagrant Development Environment

Learn How to Set Up a Vagrant Development Environment

Vagrant Development

In this article, we will aim to achieve two objectives. The first objective will be to briefly introduce Vagrant. The second objective will be to demonstrate how to set up a Vagrant environment on Ubuntu.

We can simply look at Vagrant as a tool that helps developers automate the creation of virtual machines (VMs) and VM environments. Once you create your Vms, you can then avail them to developers as cluster nodes. As a developer, you specify instructions using a domain specific language (DSL) that will create your required VMs and if needed, configure them. When you specify your instructions using DSL any time you run them you will get the same results. This ability to recreate exactly the same results is beneficial to developers interested in having multiple development environments and those who would like to share development environments with others.

Vagrant inter-operates with other tools such as VirtualBox from Oracle, VMWare from VMWare Inc and HyperV from Microsoft. You specify your provisioning scripts in a special file known as Vagrantfile which relies on Ruby and associated plugins. Vagrant enables you to centralize script sharing or include your Vagrantfile within your source code to simplify development environment set up.

To successfully use Vagrant, there are three components that you need to understand. The first component is providers. Providers is the mechanism used by Vagrant to enable actual virtualization because Vagrant lacks virtualization functionality. Some of the tools that enable virtualization are VirtualBox which is bundled with Vagrant, VMWare Fusion and Hyper-V among others. The second Vagrant component is Boxes. Boxes are the images that are relied on by Vagrant to create the environment as per the instructions you give. A box may only be composed of the OS that you will use or it may be composed of the OS and other applications that you need. A box may support a single provider such as VirtualBox or it may be versioned to support multiple providers such as VirtualBox and VMWare Fusion, among others. The third Vagrant component is Vagrantfile. The Vagrantfile holds the instructions you specify through DSL on how the environment will be set up including the number of VMs, VM type, provider and networking among other settings.

After introducing Vagrant, we will proceed to demonstrate how to set up a Vagrant development environment on Ubuntu. Before we can install Vagrant, we need to install VirtualBox. You can either install VirtualBox version provided by Ubuntu or you can install the version provided by Oracle. To install the version offered by Oracle use the command below.

sudo sh -c "echo 'deb http://download.virtualbox.org/virtualbox/debian '$(lsb_release -cs)' contrib non-free' > /etc/apt/sources.list.d/virtualbox.list" && wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add - && sudo apt-get update && sudo apt-get install virtualbox-5.1

install vbox
To start Virtualbox, execute thE command virtualbox at the terminal.
start vbox
You can then use the VirtualBox Manager presented after starting Virtualbox to create virtual machines. An example is shown below where the VM name, operating system, memory and hard disk are specified.
create box
Navigate to this website https://www.vagrantup.com/downloads.html and download a version of Vagrant that is suitable to your environment. In this tutorial, we are using Ubuntu 16.04 64 bit so we need to download 64 bit Debian. Make sure you have uninstalled any Vagrant gems before using the installer to install vagrant. It is important to note Vagrant gems are no longer updated and supported. Move to the directory where Vagrant was downloaded and install it. The commands below show how to install from the Downloads directory.

cd ~/Downloads

sudo dpkg -i ~/Downloads/vagrant_1.9.2_x86_64.deb

install vagrant
To bring up a functional virtual machine based on ubuntu 12.04 LTS, we use the command shown below.

vagrant init hashicorp/precise64

vagrant up

inti vagrant
You can always tunnel into your machine using vagrant ssh. To terminate your virtual machine, you use the command vagrant destroy.

After installing Vagrant we will move on to demonstrate how to set up a project. Before we can work on a Vagrant project, we need to create a Vagrantfile. The Vagrantfile has two purposes. The first purpose is to specify the project root directory so that configuration files can be specified relative to the root directory. The second purpose of a Vagrantfile is to specify machine type, resources needed, required software and rules that will be used to access software.

To set up a Vagrant directory, a specialized command is available. The commands below show you how to create a directory and set it up to be used with Vagrant.

mkdir setting_up

cd setting_up

vagrant init

init dir
The command used to initialize a directory will create a Vagrant file in the current directory containing comments and examples, which you can modify to suit your needs. The initialization command does not require you to create a new directory; you can use it to initialize an existing directory.

Instead of requiring you to start building a virtual machine from nothing Vagrant provides building blocks in the form of Boxes. In the project, set up process after creating the Vagrant file we need to specify a box. To add a box we use the vagrant box add command and specify the box name. HashiCorp hosts a box catalog here where you can search and download boxes. For example to add the xenial 64 bit box we use the command below.

vagrant box add ubuntu/xenial64

add box
In this post, we introduced key concepts you need to understand before using Vagrant. We demonstrated how to install Virtualbox and Vagrant. Finally, we demonstrated how to set up a Vagrant project.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -