In this blog post, I’m going to present a simple, step-by-step introduction to a great tool called Vagrant. (Originally published on Medium.com, 11-Nov-2018)
As a Linux Low-Level developer, I always find myself in need for multiple Linux VMs to fiddle with.
Whether I need to check something on multiple distributions, because my newest hack to probe something in the Kernel will probably burn the VM and anything else around it or simply because I’m running on a MacOS laptop.
After using native VirtualBox for many years, creating, managing and deploying machines, reverting to snapshots or whatever I decided to try out Vagrant, which seems quite mature and widely used nowadays. So what is Vagrant? By their own words:
“Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.”
In practical terms, their primary goal seems to be improving the workflow of using VMs, so that it is more convenient, faster and easily shareable. Vagrant integrates with many VM providers and exports a simpler API for working with the actual VMs, including setting them up, cloning them and of course using them.
After a week or so of using it, I realized Vagrant really did speed things up during my day-to-day development process, and better yet, helped me help other teams in my development group integrate with my product much better. So let’s see where the magic happens.
Step 1- Choosing a Vagrant Box
Firstly, we want to choose ourselves a Box (The naming convention for a Vagrant image). Luckily, we have a vast selection of Boxes eagerly waiting for us to abuse them at their website.
Now, assuming we chose ubuntu/xenial64
, let’s go and create our local environment:
cd $HOME
mkdir vm_ubuntu16
cd vm_ubuntu16
vagrant init ubuntu/xenial64
Our last operation created a Vagrantfile
that may be used for provisioning and configuration of the guest machine. We’ll talk about this a bit later down the road.
Step 2 – Creating the Vagrant Box
Now, let’s initialize the guest machine (If the machine already exists, this command starts it):
vagrant up
Once it’s online, let’s connect to it using ssh:
vagrant ssh
At this point, we’re running inside the guest machine and we can do anything we want, just like with any other VM.
When you finished working on the VM, or for any other reason, you can either suspend or shut down the machine, using one of the following command:
vagrant suspend # Put it to sleep vagrant halt # Shut it down entirely
NOTE: As you can see, all the vagrant commands should be executed from within the local environment directory (In our example $HOME/vm_ubuntu16
)
Step 3 – Sharing files between the Host and the Box
As we mentioned before, we can use the Vagrantfile
for configuration and provisioning of the guest machine. Right now, we’ll be using it to share a single directory between the Host and the Guest machines.
First, let’s shut down the guest machine. Then, open Vagrantfile
using your favorite text editor and find the following line:
# config.vm.synced_folder "../data", "/vagrant_data"
Now, to enable syncing, we should:
- Remove the comment mark (‘#’) from the beginning
- Then set
../data
to be the directory that we want to sync on the Host machine - And finally set
/vagrant_data
to be the directory that we want to sync on the Guest machine
At this point, just power the guest machine back up and check that everything works.
That’s it, hope you enjoyed and found this helpful! Find more useful stuff on my main page 🙂