In this post, i want to write something about how to bring up a VM with Vagrant. Basically Vagrant uses pre-built images to create a development environment, most of these images have a very minimal working footprint. The images once downloaded can be reused any number of times and all these can be done with couple of command line statements. So let’s jump in…..
Vagrant manages all the pre-built images called ‘boxes’ in Atlas, previously called as VagrantCloud. The names of the images are to used as is while creating a Vagrantfile, which is basically a configuration file for the VM.
First step is to create a directory for the Vagrant boxes. I am creating a directory on my Desktop for the ease of access and i am going to name it as VagrantDir and then initialize the directory using vagrant init. Initializing a directory creates a vagrant file in the directory which we later use for configuring the VM.
Now that we have a vagrant file, we need to add the ‘box’ of our choice to the vagrant directory. In my case i am using the ubuntu/trusty32 box. Use the below command for the same. This will download the base image of the ubuntu 32 bit version to the vagrant directory.
The box names on Atlas catalog are namespaced, meaning it has a username and box in the name. In our example ubuntu is the username and trusty32 is the box. We can add boxes using the local files and URL as well.
vagrant box add ubuntu/trusty32
Once the download is complete, we need to edit our vagrantfile to use the box as our base image for the project. The configuration settings are commented out in the vagrantfile and can be modified as per our requirements, below are the minimal things that needs to be changed.
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/trusty32" end
Now we are ready to bring the VM up using a simple command line “vagrant up”. Once the VM comes up, we can ssh to it using “vagrant ssh”. Vagrant box should boot up in less than a min.
We do not see anything in specific while the VM turns on as we run the box with no GUI. Once you ssh to the vagrant, we should end up in the ubuntu terminal. Use Ctrl+D or logout to quit from the ubuntu box. To destroy the resources used by the vm, just use the command line “vagrant destroy” on the host terminal. Remember, destroy is not going to delete the base image file that we downloaded from the Atlas.
Finally, few ways to shutdown the vagrant vm’s
$vagrant destroy - completely removes the traces of machines $vagrant halt - gracefully shutsdown the machine preserving the disk contents $vagrant suspend - pauses the vm in the current and can be resumed by $vagrant resume
Hope this post was informative. Thanks!