Development series: Vagrant

Racheal Mwatela
4 min readApr 4, 2019

“Simple things should be simple, complex things should be possible.” -Alan Kay

I have always been a curious soul. For a while, I have been hoping to work on docker, I even had tutorials ready for just that but I guess I got caught up. So when I learnt that we will be doing something closely related to docker I was excited (how cool is my job!).

My initial reaction when I had to start on vagrant was fear: that this was going to be difficult. I tried to learn different technologies before but forego them because I was a newbie in programming. A few months later, it’s different and this time I have people to show me the right path to follow.

So, what is Vagrant?

Vagrant (by Hashicorp) is a tool that allows you to create and manage virtual development environments easily. If you are working on multiple projects and require different dependencies, web servers, etc for each project, Vagrant is the tool for that. It gives you the freedom to have different disposable environments for your individual projects. It does not matter the OS your team members use as long as you share your environment, all they need is a vagrant up command and voila!

To begin off, Vagrant requires Virtualbox to be installed.

Virtualbox allows you to create and manage multiple Operating Systems in one machine. Your current OS in your machine will be your host OS while guest OS is installed on Virtualbox.

PS: I would recommend anyone to first go through virtualization to get a better understanding of vagrant.

Check out this site on the installation of Virtualbox in Ubuntu:

  1. https://tecadmin.net/install-oracle-virtualbox-on-ubuntu/
  2. Also, check out Oracle official documentation for latest releases https://www.virtualbox.org/

Once you are done with the installation, on your terminal, type the command:

virtualbox

this will start the Virtualbox UI giving you access to create a virtual machine.

Click on the start button at the far left to create a virtual machine(I prefer saying installing a guest OS…but to each his own).

I installed Alpine as my guest OS just to see how Virtualbox works. According to Alpine’s official website, Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox.

I used this tutorial for installation. It is super detailed!

  1. https://www.youtube.com/watch?v=1_bsycXrFcI

Installing vagrant

To install find the right package from here.

I use Ubuntu, therefore, had a difficult time when I tried to install and run the unzipped file. Installing from the package manager worked for a while till I realized I couldn’t run some of the commands such as vagrant share

I did find a workaround by installing the .deb file from this link (still from the official documentation).

Once you are done with the installation, run vagrant to verify your installation was successful.

Vagrant ‘crash course’

To initialize vagrant, you can create a directory that you would want your project to exist in and run vagrant init <box_name> command :

$ mkdir my-vagrant
$ vagrant init ubuntu/trusty64

This will create a vagrant file that we will set up all our configurations. In most cases, a vagrant file contains the following:

a. Box

config.vm.box = "ubuntu/trusty64"

The value is the name of the box image that will be installed in Virtualbox. In our example, ubuntu/trusty64 is our box. You can get more boxes here

b. Provider

config.vm.provider “virtualbox” do |vb|vb.gui = true`

This is where the virtual machine will run. Virtualbox is the default provider for Vagrant.

c. Synced folder

config.vm.synced_folder "../data", "/vagrant_data"

Synced folder synchronizes the files between the guest/virtual machine and the host machine. In the example above, “../data” directory in the host OS could be accessed as “/vagrant_data” in the guest OS

d. Provisioning

config.vm.provision “shell”, inline: <<-SHELLapt-get updateapt-get install -y apache2

Provisioners are important when you want to install software. It automates the process for you so that it is easy to set up and use.

e. Network

vagrant.vm.network "forwarded_port", guest: 80, host: 8080

Networking gives us access to our guest/virtual machine through port forwarding and other networking options. This allows your host machine to see your box.

Vagrant commands

Some of the basic commands I have found to be useful include:

vagrant up : Starts the vagrant environment

vagrant ssh : Enables you to access the virtual machine

vagrant share : Share and work on environments with your team members

vagrant reload : This command allows you to restart your machine with the new configurations changes made on vagrant file.

vagrant destroy : This command removes all traces of the guest machine from your system

vagrant hault : This command allows for your machine to shut down but maintain all your content in the virtual machine.

vagrant suspend : This command saves the current state of your virtual machine. Once you vagrant up you can pick up from where you left.

There so much you can do with vagrant that I am yet to cover but you can’t underestimate its power as a tool!

--

--

Racheal Mwatela

Hey, I am a Software Engineer with a passion for mentorship and career development