The story behind ‘vagrant up’ – Vagrant 101

You’re a curios tech guy, who wants to test every new Linux package, or a tech blogger who has an impulsion to write a step-by-step guide for installation of a seemingly complex tool/software, or simply a developer who needs to test some new stack with all the operating system level dependencies, all you need is a lightweight, quickly up, portable development environment. The good news is we have Vagrant – a simple to use virtual machine management tool. With Vagrant setting up a fresh development sandbox is just a matter of few commands – at max 3 step process:
  1. Setup – download and install Vagrant – Installing Vagrant on CentOS 7.
  2. Configure – Create a single file i.e. ‘Vagrantfile’, to specify the specs for the virtual machine you want Vagrant to spawn, and run vagrant init <box-to-use>;
  3. Work – simply run ‘vagrant up‘, and a clean development environment will be there.


Some necessary vocabulary

technically Vagrant is itself just a wrapper or management utility on top of virtualization software such as VirtualBox, KVM, or VMware, and around configuration management software such as Chef, Puppet, Ansible, etc. The biggest obstacle newbies face while exploring Vagrant is the vocabulary (which is the case with almost all the tech docs). Here are the main terminologies one needs to be clear about, to get easy with Vagrant:

  • Providers  providers are the basic virtualization technology Vagrant use, as stated above Vagrant itself is just a utility above theses softwares e.g. VirtualBox, KVM, or VMware, etc. The initial Vagrant only supported a single provider i.e. VirtualBox, in later releases (1.1) support for KVM, VMWare, Hyper-V, etc was also added. Since version 1.6, Vagrant also support Docker. Since version 1.6, Vagrant natively supports Docker containers, which in some cases can serve as a substitute for a fully virtualized operating system.
  • Provisioners: are the softwares Vagrant use to configure and provision the VMs. Vagrant supports all the mainstream configuration management tools such as Puppet, Ansible, or Chef, etc. The various supported provisioning softwares – https://www.vagrantup.com/docs/provisioning/
  • Boxes – this term must be disturbing you in almost every post you read about Vagrant. Box (Vagrant Box) is simply a configuration for spawning virtual machines using Vagrant i.e in conventional sense it’s just a template for creating a VM. We can set machine configuration ourselves, as per need, or can simply use any of the publicly available boxes (templates). The vagrant box command utility is available for managing boxes – official docs for vagrant box command. There’s public Vagrant box catalog ‘Discover Vagrant Boxes‘ – makes it easy to search and use Vagrant boxes created by the community. Also check this ‘vagrant.es‘, the website lists boxes, people have been nice enough to share publicly. For more – official vagrant box docs.
  • Vagrantfile – where we specify the configuration for vagrant, about what type of virtual machine we want, once set, executing vagrant up will have our clean development environment up and running.  A single Vagrantfile is required per project. Usually there must be Vagrantfile in the directory you’ll be executing vagrant up (or other vagrant command) – but in case it didn’t find any the, Vagrant will climb up the directory tree to find one. For example: If you executed vagrant in /home/ahmed/projects/foo, it look for a Vagrantfile in the following order, starting from current directory and moving up, until it finds one:
/home/ahmed /projects/foo/Vagrantfile
/home/ahmed /projects/Vagrantfile
/home/ahmed /Vagrantfile
/home/Vagrantfile
/Vagrantfile
This lookup approach lets you execute vagrant command from any directory in your project. You can also specify and fix the starting directory by setting the environment variable VAGRANT_CWD (in your shell start-up file i.e. ~/.bachrc). For more – official Vagrantfile docs.

Few examples of configuration

For specifying the box to use – precise64  is a publicly available basic Ubuntu 12.04 (32 and 64-bit) box good for minimal use cases (you see it almost every Vagrant post):
config.vm.box = "precise64"
For specifying the provider: Not mandatory as you can specify at run time using –provider flag on with vagrant up command, and it’ll have priority over the one set in Vagrantfile. Apart from it we can set the VAGRANT_DEFAULT_PROVIDER environmental variable in our shell start-up file, it’ll have next priority. Last we can set provider in our conf file:
config.vm.provider :virtualbox
For setting the configuration management tool:
config.vm.provision :puppet
A sample Vagrantfile.
For detailed understanding – Vagrantfile explained

A simple example

Step 1:

Install Vagrant – for this check the previous post ‘Installing Vagrant on CentOS 7

Step 2:

in case of standard box (template) just vagrant init {user}/{box_name} will do – it’ll just create the Vagrantfile in the directory:
$ vagrant init hashicorp/precise64
 vagrant init

Step 3:

the provisioning step – executing ‘vagrant up’ for the very first time (for a project or after setting Vagrantfile) will look for the box, in case not found it’ll add it (download it), once added it’ll create and provision the environment/VM. On next instances it’ll just boot/resume the VM (unless the –provision flag is explicitly provided. ).
$ vagrant up
vagrant up
Status command to check the available VMs and their status i.e. powered off or running:
$ vagrant status
dpy_6.6
Finally, let’s SSH/sign-in into our new VM:
$ vagrant ssh
vagrant ssh
Shutting down VM: simply type exit to leave the ssh session.
vagrant@precise$ exit
For graceful shutdown – use halt command:
$ vagrant halt
vagrant halt
Verify the status:
vagrant status

Leave a Reply

Your email address will not be published. Required fields are marked *