Before we move on, it's important to keep your Vagrant setup in check.
In this video we will cover a few commands to manage your Vagrant environment, and recover disk space you may have entirely forgotten about. This is pretty important if you are using a busy laptop with a relatively small hard disk (MBP users, I feel your pain.)
The virtual machines that Vagrant creates for us, assuming you are using VirtualBox or VMWare, should be thin-provisioned. This is a fancy term for saying that should we ask for a brand new Ubuntu server with a 20gb drive, the underlying file that really contains our server may only use ~2.5gb of space on disk. We will only consume more space - up to 20gb - as we write files to our VM.
However, if you spin up multiple instances of the same box, each one will consume that ~2.5gb minimum, and the box file itself will be ~500mb on average. The numbers sound inconsequential given todays hard disk capacities, but if left unchecked, it can add up.
Thin provisioning may work the same for other Providers, but I have no experience with them so can't say for sure.
The following are the important commands I think all Vagrant users need to know about.
You've done your
vagrant up and then jumped on to your shiny new box with
But how do you exit your vagrant box, back to your dev machine?
Shutting Down A Running Vagrant Machine
There are two ways to 'stop' a running Vagrant machine. You can either suspend or halt.
vagrant halt shuts down the Vagrant machine - as though you shut down the machine cleanly, for example running a
sudo shutdown now from the terminal in Ubuntu.
If the machine can't gracefully / cleanly shut down, you can instead do a :
vagrant halt --force
Which, as the command would imply, forces the shutdown regardless. This is like holding down the power button on your physical machine.
Once the machine is powered down, the underlying virtualbox files remain on your hard disk until next required.
vagrant suspend also stops the running machine exactly where it is and also saves the entire contents of the RAM of the virtual machine also. This takes extra disk space on your local / development machine. Think of this like shutting the lid on your laptop - as soon as you open the lid again, your machine is up and running without needing to boot.
The advantage of this is that you can pause your server whilst it is doing something. This comes in handy more often than you might think.
Once a vagrant machine has been suspended, to resume you would run:
To continue from where you left off.
vagrant status can be used if you are unsure what the current status of your available virtual machine is. However, I personally find myself simply checking the list inside VirtualBox, more out of habit than running / remembering yet another command.
Those are the main day-to-day tasks I use when dealing with running Vagrant boxes, but more commands can be found on the official Vagrant Docs.
Vagrant Box Management
Vagrant has the concept of a 'Box' and a 'machine'.
This is initially quite confusing.
I like to think of it as a 'machine' is brought out of a 'box'.
Just like in the olden days when you would have to buy a brand new shiny bit of tin to install and run your server on. Your new server would arrive in a big cardboard box which you would unpack and et voilà, your new machine is ready for your chosen OS.
You can read more about Vagrant Boxes on the Vagrant Docs.
This analogy isn't perfect as after all, we can keep pulling more and more machines out of a Vagrant box... until we run out of disk space that is.
Managing Disk Space
It's lovely to be able to pull down any Vagrant box we like and have the resulting machine up and running in very short order.
But each time we do so, we end up with another chunk of our disk space consumed.
To keep this in check we have the command:
This can be run at any path on your system / terminal. It will output a list of all the available / downloaded Vagrant boxes currently on your system.
The ones to know about are:
vagrant box list - shows all the boxes on your system
vagrant box remove your/boxName - removes a given box from your system
This should be enough to reduce box sprawl.
You can also run:
vagrant box update
Which will update the Vagrant box itself, but not any machines that have been provisioned / built from that box in the past.
This is handy if you make a change to the base image. The base image shouldn't change too frequently. Any config changes are best managed in your provisioning setup.
You can read more about the available
vagrant box commands on the official Vagrant Docs.
One thing that doesn't seem to be on the docs is where on your system the Vagrant box files will be stored.
And on linux / osx:
After using Vagrant for a while, you may be surprised how many boxes you find in those locations ;)
Note, these are hidden folders.
To find the machine files, the easiest way is to open your VirtualBox and right click / cmd click the machine, then 'Show in explorer' or 'Reveal in finder'. VMWare has an identical system, with slightly different wording.