Why should I use Vagrant instead of just VirtualBox?

234

109

I've been using VirtualBox for many years to create a development environment.

A lot of my colleagues are talking about Vagrant and many people seem very excited about it but I just can't seem to grasp the benefits of it.. seems to me like it's a series of new commands to learn in order to do the same things I did with Virtualbox.

With VirtualBox, I install and configure an environment perfectly, then I can package it up as an OVA or whatever and share it with other office users. You can take a snapshot in VirtualBox if something goes wrong.

Puppet and chef aren’t really part of Vagrant, they’re their own thing right?

So yes, what benefits specifically does Vagrant offer over VirtualBox on it's own?

John Hunt

Posted 2013-04-17T14:39:32.630

Reputation: 2 739

11Two years on I'm now using Vagrant every day for development - it's great! My team uses it and it's solved our dev environment issues. – John Hunt – 2016-02-10T10:04:35.733

1opinionated here: If you never had any trouble setting up PXE servers, know VBoxManage by heart, know how to setup nfs in less than a minute etc. and also like directly managing build chains.. there's close to no point at all. Value goes up with each part of the puzzle you're missing. The widespread use isn't from puppet integration but because people could security-hole infested images right off the internet. no matter what they tell you. – Florian Heigl – 2016-04-16T00:20:55.413

1I also found vagrant far more useful in a multi-developer team whereas previously it was just me. I imposed vagrant on my team and discovered that's where the value comes in. – John Hunt – 2016-05-06T10:53:50.257

23.5 years on and I now use docker every day for development. It's good, but a different beast to vagrant... Depending on your requirements I believe vagrant is still a better development platform for teams - it's just much easier to work with. – John Hunt – 2017-08-18T08:33:16.177

54 years on and I've discovered docker is terrible. Long live Vagrant. – John Hunt – 2018-02-05T15:00:33.003

3your comments are so making this much more fun – Florian Heigl – 2018-02-06T17:44:02.727

15 or 6 years on, I've discovered I have to use docker because everyone else does. The new shiny stuff isn't so shiny in reality.. I don't feel people ever really got to grips with the old stuff yet.. However, it does have its place.. Being able to spin up a quick dev LEMP stack is quite nice – John Hunt – 2019-01-14T10:54:52.107

1Just being able to avoid the enormous amount of wasted space that are VirtualBox snapshots which must be kept inside the VM's directory. – chb – 2019-05-08T04:08:16.537

Answers

154

This is a big question, so I'm going to break it up into three sections.

Vagrant

Vagrant is used to set up one or more virtual machines by:

  • Importing pre-made images (called "boxes")
  • Setting VM-specific settings (IP address, hostnames, port forwarding, memory, etc.)
  • Running provisioning software like Puppet or Chef

Note that it doesn't install software or set up the machine past loading the VM and setting VirtualBox settings. Think of it as a scripting engine for VirtualBox.

Here are some reasons I've seen for using Vagrant over just VirtualBox.

1. Set Up Multi-VM Networks with Ease

Most of the Vagrant power-user content I've read has been about setting up multiple VMs at the same time. Vagrant gives you a single config file to set these up, enabling you to launch all of them with one command.

Say you've configured three VMs to network with each other using static IPs on the 192.168.1.* subnet. You find yourself in a location that is already using that subnet to hand out IP addresses, and your VMs now conflict. With Vagrant, you can simply edit the Vagrantfile and reload the VMs, whereas with VirtualBox you'd have to open the settings for each VM, if not boot each VM and change them inside.

2. Source Control

By putting the settings in a text file, it enables the configuration to be put under source control. Made some changes last week and accidentally broke the image? Just revert the changes and reload the VM. You can accomplish this with VirtualBox snapshots, but it will take up much more space than just a Vagrantfile.

3. Various Platforms

There's a large number of boxes available at sites such as http://vagrantbox.es. This enables you to try various OSes or distributions, applying the same provisioning to set up similar environments. This can help with testing or adding support to new platforms, and would be time-consuming using just VirtualBox.

There are a lot of arguments for using provisioning software, as well as using image snapshots. For additional discussion, I'll point you to Stephen Nelson-Smith's excellent article How to Build 100 Web Servers in a Day.

Adam Lukens

Posted 2013-04-17T14:39:32.630

Reputation: 1 664

8Still, I do not understand why I should use vagrant in development environment! Because, it's expensive ( if you want to use VMWare! ) We can prepare a virtual machine like base boxes which is used by vagrant then get clone by VMware to setting up another virtual machine. First and foremost, we can control all virtual machine settings by vmrun command that offered by VMware even IP address changes or running a script inside the guest machine! – Oğuz Çelikdemir – 2014-07-14T08:33:52.380

3@OğuzÇelikdemir That's a fair point. Vagrant just puts an easier front-end to VMWare with regards to bringing up a new machine. Behind the scenes, it's using vmrun to control it. I had always used VirtualBox, and thus don't have any experience with the VMWare backend. – Adam Lukens – 2014-07-17T19:34:07.353

1

@AdamLukens, Thanks for the detailed comments. What you mentioned sounds interesting.. but I wonder if you are aware of the VboxManage command? https://www.virtualbox.org/manual/ch08.html

Pretty much everything you mentioned can be created as a script by passing the specific ip/parameter to vboxmanage controlvm or a similar command in a shell script and then version control that..

So how does vagrant improve over virtualbox?

– alpha_989 – 2018-03-06T15:33:58.757

@alpha_989 I agree. There seems to be no unique point for using Vagrant. Just for convenience. – Niklas – 2018-04-23T08:47:28.290

27

In addition to the excellent answer given by Adam, Vagrant ties everything all together. Although Chef and Puppet (and Salt and shell scripts and whatever other provisioner you want to use) are separate things, Vagrant ties it all together and makes it work with just a vagrant up.

That one command will

  1. just start the VM if that's all that is needed, but it'll also
  2. create the box from your specified base box if that is also not done yet, but if you don't even have the base box on your machine it'll first
  3. fetch it from its URL and download it to your machine.

You don't have to think about all that. Let's say you're switching to a different project, one started by a coworker. You just checkout the code from your repo, and run vagrant up, not worrying about downloading ISOs or installing anything, or wondering which version of which distro you need to use for that particular client, or whether you have a copy of a VM that's already got everything you need.

You don't even need to worry about whether they've set things up using Chef or Puppet or just shell scripts. (Okay, so you might need to do a bundle install or otherwise make sure you've got everything installed, but still not a big deal.)

By tying everything together, and providing a unified interface for it all, it can make all but the simplest use cases much much easier. At first you may feel like you're just relearning a new way to do what you're already doing, but once you get deeper into Vagrant use you'll discover you can do much more with much less effort. It'll be well worth the initial time investment.

iconoclast

Posted 2013-04-17T14:39:32.630

Reputation: 2 752

8But it's a hell of a lot easier and faster for me to send a developer a presetup VM image than it is for them to install vagrant, learn how to use it, and deal with any of the myriad problems that come up, particularly when supporting windows boxes. I just really DO NOT get it, either. Seems to be nothing but fanboys. – hopeseekr – 2014-11-26T01:40:35.583

5Installing Vagrant is unbelievably easy these days. Anyone who can't install it can't possibly be trusted to write code. Having a Vagrantfile in your source code that the developer checks out is actually far easier than sending a VM. The Vagrantfile defines the setup, including the URL from which to download the base VM (which it then configures). If you don't initially need to make configuration changes, then you're all set. But as you work on your project, you'll likely need to make changes. Do that in your Vagrantfile. – iconoclast – 2014-11-26T03:33:24.140

5Since you're keeping the Vagrantfile with your code, all developers joining the project will automatically get the changes you make to your VM, since you're making them in the Vagrantfile. Configuring your Vagrantfile doesn't need to be difficult. You can use shell provisioning if you find the Chef and Puppet to be overkill (which they often are). If you can enter the shell commands in the VM, then it's a tiny hurdle to set up shell provisioning commands in the Vagrantfile. It will take a small up-front effort and save you lots of headaches in the long run. – iconoclast – 2014-11-26T03:33:46.430

3@hopeseekr I too was thinking this. I've kept reading trying to see the benefit. I think I finally get it. I've been reading a book on Python networking. Testing is a complex setup imitating machines you can use to learn. You can build a VM w/Linux, do it by hand, and I'm one person, so that's ok. I Installed Vagrant on Windows and entered three Vagrant commands to call up his image to use on VirtualBox. What if I had 50 PCs? long time to setup with snapshots. What if there are changes? 50 updates? For me I was able to start working immediately without setting up everything. – johnny – 2015-10-30T16:36:27.740

@johnny It's simple. Provide a download link for a preconfigured VM. Then just download it on every computer. I swear to you that that will be far less error-prone than using vagrant 50 times. And faster. – hopeseekr – 2015-11-01T12:53:00.233

4and when you need to change something on all 50 computers? – iconoclast – 2015-11-02T17:02:49.803

2I just spent another 4 hours trying to get an android-dev VM up and running via Vagrant, without success (It wouldn't install the Android SDK). Then I found a virtualbox image and voila! Up and running in 10 minutes, counting the 8 minuets to download the 2 GB. I stick by my words from almost 2 years ago! – hopeseekr – 2016-04-26T04:48:06.970

2So if I fail at A, but succeed at B, I can conclude that B is better? There's no logic there at all. If you don't need the benefits that Vagrant offers, or don't have the patience or ability to get it working, then don't use it. But that doesn't mean that not using it is a better approach for everyone. Also, you can use the VirtualBox image as the base image for Vagrant. They are *not mutually exclusive*. – iconoclast – 2016-04-27T20:32:09.243

4The discussions here are really about premature optimisation. Dont use vagrant if you dont need it. Use vagrant when you find creating VMs is becoming tedious. – cammil – 2016-05-27T08:57:50.410

If you have multiple developers and you have to share a common environment configuration, and you'll ever have to change it, then the time to adopt Vagrant is as early in the project as possible. In other words: the time to choose the tool that solves your problem is as soon as you are faced with that problem. Don't use Vagrant if you don't have a problem it solves, but don't wait until you've already wasted lots of time, either. "Premature" = before you know you have a problem, not before you've suffered because of the problem. – iconoclast – 2016-05-27T20:38:26.140

@hopeseekr Some of our devs had quite a few issues installing it too - there are some odd quirks on Windows, and then your VM doesn't have the right vbox drivers etc.. it's not quite as simple as it appears for sure.

Having said that, I think the benefits outweigh the issues. It feels like Vagrant is still a bit rough around the edges (as though no windows guys ever test it..) – John Hunt – 2016-06-03T09:20:19.767

@cammil, are you aware of the vboxmanage command? Except for downloading the VMs', pretty much everything I have heard so far can be done through the vboxmanage command and put into a shell script.. is that not true? – alpha_989 – 2018-03-06T15:36:45.663

9

Ability to integrate chef or puppet in with VM provisioning is key. Most Vagrant users will tell you they run 'vagrant provision' and occassionally 'vagrant reload' much more often than 'vagrant up' or 'vagrant destroy'. These tasks indicate the real work is not in spinning up/down VMs but 'managing' them after the fact.

To pose a better question (posed by proficient Chef users, anyway) might be why use Vagrant and not knife with the appropriate plugin (will get to virtualbox plugin, in a moment)? For instance, passing argument values stored in a data bag to a knife plugin is (way) more intelligent, flexible and manageable than juggling one giant Vagrantfile. I typically define my 'dynamic' resources like # of CPUs, amount of memory, which OS to deploy, hostname, IP, routes, etc, in chef databag(s) such that I don't need to keep changing my recipe ;-). Editing a databag through Chef web interface is a really easy data entry task I can give to the most junior operators. With Vagrantfile, your forever modiyfing code and believe it or not - code breaks - which pretty much guarantees you'll NOT be handing off simple changes to Operations staff, ever.

Aside from the fact knife does not yet have a plugin for virtualbox (though I envision one in the not too distant future), there are already plugins for most 'enterprise' virtualization products, including vmware, xenserver and just about every major 'cloud' provider, as well. This means knife is far superior to what Vagrant offers if/when you are ready to move beyond virtualbox. For now, Chef community seems happy to let virtualbox users limp along with Vagrant by not integrating virtualbox apis for a knife plugin. There is a knife-vagrant plugin that does allow for use of data bags to pass arguments. But, it still requires vagrant software and it's monolithic Vagrantfile to function.

So, I'll go out on a limb and say Vagrant is definitely NOT 'better' than Chef with knife; but necesary (for now) if you insist on virtualbox and perhaps 'easier' than managing chef with data bags, provided you have a fairly simple environment to manage.

Andrew

Posted 2013-04-17T14:39:32.630

Reputation: 209

8

Here are two more developer use-cases that vagrant simplifies (over "plain" VirtualBox). I didn't see these use-cases called out specifically in the previous answers.

  1. Vagrant helps with the aim of keeping the development and production platforms as close to each other as possible. In an ideal world, you would provision the production environment with THE SAME scripts used to provision the Vagrant VM, minimising surprises at deploy time.

  2. Integration tests and continuous integration: Vagrant is easy to control from tests, and therefore entire multi-machine stacks can be easily controlled from tools such as Jenkins when doing test runs.

Yes, "plain" VirtualBox can be used here too - but the conventions used by vagrant make it more simple to set these scenarios up.

Ben XO

Posted 2013-04-17T14:39:32.630

Reputation: 239

1I seriously just copy the vmx and vmdk from Windows to Linux. Maybe it's different in the virtualbox world, but I doubt it! What's easier? Copying a file or messing with vagrantfiles and the myriad of problems that can go wrong? – hopeseekr – 2014-11-26T01:42:37.173

6To put a finer point on it, Vagrant can be thought of as a virtual definition language that can describe the characteristics of a virtual machine --independent of whether it is hosted under VirtualBox, VMware, Parallels, or what-have-you. See how Vagrant is not "competition to" VirtualBox, but works with it, or beyond it. – MarkHu – 2015-02-14T04:36:57.567

One would typically use a Vagrantfile to describe how a virtual machine is to be set up for a project - the Vagrantfile would be committed to source control (git, etc) with the project, for sharing on GitHub etc. This is much smaller to download than an entire VMDK. – Ben XO – 2018-08-01T10:05:30.533

1

Vagrant abstracts virtual machines so it allows you to easily switch virtual machine implementations. You can switch from Virtual Box to AWS or Digital Ocean. It's like using SQL instead of a database-specific query language.

Vagrant allows developers to set up their environment quickly with only one command and it is exactly the same as everyone else's. This is important in large companies where developers come & go often. It can reduce time to setup from 3 days to 1 hour.

+ What Adam said.

(I still haven't found a use for Chef or Puppet though...)

Chloe

Posted 2013-04-17T14:39:32.630

Reputation: 4 502