Vagrant is a command line tool for managing virtual machines. Vagrant commands are generally quite sensible and often a guess at what the command might be gives the rights of others you answer, but there are rather a lot of them. This post picks out some basic vagrant commands and works through the installation and basic usage of vagrant.
What Is Vagrant?
Vagrant is a Hashicorp tool for building and managing virtual machines, whicih acts like a wrapper around different virtual machine providers to allow a consistent interface. Vagrant is configured using simple plain text files, which are easy to share and put under version control.
Installation and Setup
Vagrant itself is easy to install on Windows, OSX and Linux systems.
Installing a Virtual Machine Provider
Vagrant does not work with virtual machines directly – it is more of a wrapper or interface to different virtual machine ‘providers‘ which actually run and control the virtual machines. You will need to install a provider on your machine to allow vagrant to work with it.
A good provider to start with is Virtualbox. Virtualbox is available for many different systems, and there are many Vagrant boxes available for it.
Working With Vagrant Boxes
Add A Box
Vagrant uses its own versions of virtual machines, known as ‘boxes‘ which have already been configured to allow vagrant to work with them. Hashicorp, who created Vagrant, maintain a repository of vagrant boxes. The ‘vagrant box add‘ command is used to select and download boxes from this repository. Boxes are identified by the account name in the repository and the name of the box.
$ vagrant box add USER/BOX
$ vagrant box add ubuntu/trusty64
Which will add the ‘trusty64’ box from the user ‘ubuntu’ to your system.
Add a Particular Version of Box
Some boxes are regularly updated, so to be sure you are using the right version (such as for compatibility reasons) you may want to specify an exact version. You can do this with:
$ vagrant box add USER/BOX --box-version VALUE
$ vagrant box add ubuntu/trusty64 --box-version 20171205.0.1
List Boxes Available on Your Machine
By default, vagrant will look on your local machine for boxes. You can see which boxes are available with this command:
$ vagrant box list
Remove A Box
If you download the wrong box, or just want to remove an old one, you can do this with:
$ vagrant box remove
$ vagrant box remove ubuntu/trusty64
Where To Find New Boxes
Hashicorp maintains a repository of pre-built vagrant boxes, and you will probably find the box you need on there. The Hashicorp repository has boxes from many different users, and Hashicorp recommend either using one of their official boxes, or boxes made as part of the Bento project. You may find, however, that you need to look around if you are not using one of the more popular operating systems providers.
Working With Vagrant Virtual Machines
This section is a whistle-stop tour of working with vagrant from setting up the vagrant machine to stopping and removing it.
The essential part of working with vagrant is the ‘Vagrantfile’ which describes the type of machine to build, and how to configure it. The presence of a vagrant file in a folder gives vagrant enough to work from to build and manage virtual machines.
You can create a template vagrant file by running
which will create a ‘Vagrantfile’ in the current directory, containing a number of commented out options.
Using ‘vagrant init’ isn’t strictly necessary for creating a vagrantfile, and you may prefer to create one yourself. An example of the contents of a simple Vagrantfile is below:
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" end
Vagrantfiles are written using ruby syntax, but a deep knowledge of ruby is not essential. This kind of basic Depending on your requirements you may need to make some adjustments, such as making sure the provider is correct and so on.
Start The Vagrant Virtual Machine
Once you have a Vagrantfile in your project folder you can start the Vagrant virtual machine. This is as simple as running:
Vagrant virtual machines are typically started in the background, so there may not be obvious indications that the machine is running.
Connect to a Running Vagrant Machine
Once your vagrant machine is running then it is possible to connect to it using this command:
which will connect to your machine via ssh using vagrant’s default user ‘vagrant’. You can logout from this session with the ‘logout’ command.
Stop a Running Vagrant Machine
Once you are finished with a running machine you can stop it with this command:
which is similar to manually shutting down the machine. If you want to start the machine again, you can run ‘vagrant up’ again.
Remove a Vagrant Machine
If you no longer need your vagrant machine at all, you can remove it with:
The Vagrantfile for your project will still exist, and you can create it again by running ‘vagrant up’