Complete guide for creating Vagrant boxes with VirtualBox

by Shusain ·
Published September 19, 2018 · Updated September 19, 2018

Vagrant is tool for building & managing virtual machines environment, especially development environments. It provides easy to use & easy to replicate/reproducible environment built on top of technologies like Docker, VirtualBox, Hyper-V, Vmware , AWS etc.

Vagrant Boxes simplifies software configuration part & completely resolves the ‘it works on my machine’ problem that is usually faced in software development projects. Vagrant, thus increases the development productivity.

In this tutorial, we will be creating Vagrant Boxes on our Linux machines using the VirtualBox.

Pre-requisites

-Vagrant runs on top of a virtualization environment, & we will be using VritualBox for that. We already have a detailed article on “Installing VirtualBox on Linux”, read the article to setup VirtualBox on system.

Once VirtualBox has been installed, we can move forward with Vagrant setup process.

Installation

Once the VirtualBox is up & running on the machine, we will install the latest vagrant package. At the time of writing this tutorial, the latest version of Vagrant is 2.0.0. So download the latest rpm for vagrant using,

Once the installation is complete, we will move on to configuration part.

Configuration

Firstly we need to create a folder where vagrant will install the OS we need, to create a folder

$ mkdir /home/dan

$ cd /home/dan/vagrant

Note:- Its always preferable to creating vagrant boxes on your home directory as you might face permissions issue with a local user.

Now to install the Operating system like CentOS, execute the following command,

$ sudo vagrant init centos/7

or for installing Ubuntu, run

$ sudo vagrant init ubuntu/trusty64

This will also create a configuration file in the directory created for keeping the vagrant OS, called ‘Vagrantfile’. It contains information like OS, Private IP network, Forwarded Port, hostname etc. If we need to build a new operating system, we can also edit the file.

Once we have created/modified the operating system with vagrant, we can start it up by running the following command,

$ sudo vagrant up

This might take some time as it operating system is being built with this command & its downloading the required files from Internet. So depending on the Internet speed, this process can take some time.

Once the process completes, you than manage the vagrant instances using the following command,

Start the vagrant server

$ sudo vagrant up

Stop the server

$ sudo vagrant halt

Or to completely remove the server

$ sudo vagrant destroy

To access the server using ssh,

$ sudo vagrant ssh

you will get the ssh details while creating Vagrant Boxes (refer to screenshot above).

To see the vagrant OS that has been built, you can open the virtual box & you will find it among the Virtual machines created in the VirtualBox. If you are not seeing your machines in VirtualBox, open virtualbox with sudo permissions & Vagrant Boxes should be there.