VAGRANT: EASILY CONFIGURE AND REPRODUCE VIRTUAL MACHINES
WHAT ARE VIRTUAL MACHINES?
A virtual machine is a computer file, an image, that through the use of a virtualization software can be used to emulate an operating system within another operating system.
A virtual machine allows users to run an operating system in an app window and behaves like full, separate computer.
WHAT IS VAGRANT?
Vagrant is an open-source software that enables users to create and configure virtual environments.
With Vagrant you can easily configure and run virtual machines. Your only requirements are Vagrant and a virtualization software such as VMware, VirtualBox, Parallels or Hyper-V.
Compared to the tedious process of actively setting up different virtual machines, Vagrant allows you to set up machines by specifying configurations in a file called a Vagrantfile and starting the machine with a single command.
SETTING UP OUR FIRST VAGRANT MACHINE
- Install the latest VirtualBox (We are using VirtualBox because it is free). Installing VirtualBox is beyond the scope of this tutorial. You can download or find installation instructions here.
- Install Vagrant.
Windows: Download installer package file from here and install Vagrant.
Linux: Download the correct package file from here depending on your Linux distribution and install with a package manager or the following commands.
3. Create a Vagrantfile file.
A Vagrantfile file serves two purposes in a Vagrant box:
- Marks the root directory of a Vagrant project.
- Describes the configuration of the virtual machine. This includes how much resources are allocated to the virtual machine, network configurations, what software to install and how you’ll access the virtual machine (shared folders).
You don’t have to create a Vagantfile file from scratch. Depending on the use of your virtual machine you can select a virtual machine from https://app.vagrantup.com and initialize the virtual machine with the command vagrant init.
For example; In the above image we can see that there is an Ubuntu 14.04 virtual machine for VirtualBox that was released 3 months ago(ubuntu/trusty64). To initialize the machine we will create a folder for it, move into the folder and initialize it.
The command will automatically place the Vagrantfile file in the folder you ran the command in.
I advise you to create a folder for each virtual machine you want to create.
4. Run Vagrant up.
Run the command Vagrant up in the folder in which the Vagrantfile file is located. This will setup and start your virtual machine.
NB: If it is the first time you are installing a machine, depending on your internet speeds, the process may take a while since some downloads have to be done.
After the process is done the virtual machine should be up and running.
To confirm that it is running you can open VirtualBox and you’ll see it listed as “ubuntu_trusty_default_*“. Selecting show will launch an app window that will allow you to log in to the terminal. The default credentials should be vagrant:vagrant.
However, vagrant machines are not ideally accessed through VirtualBox. You can ssh into the virtual machine and use shared folders to access it. But logging in from the app window should do for now.
Shutting down the vagrant box
To shutdown a vagrant box run:
$ vagrant up
To suspend a vagrant box run:
$ vagrant suspend
To restart a vagrant box run:
$ vagrant reload