Getting Started with Vagrant on Linux

Vagrant Linux Feature

Modern operating systems, including Linux, have developed over recent years to bring operating system virtualization to the humble PC. Running multiple operating systems using a hypervisor allows developers, system administrators, and tinkerers to set up small, dispensable, virtual environments to test things out and learn. As virtualization becomes more and more popular, more and more pieces of software come out that promise to make your life easier. Vagrant is one of those pieces of software. Today, we’ll be talking about getting started with Vagrant on Linux: what Vagrant is, how to get it installed on your base system, and the many ways it can be useful to you.

What Is Vagrant

Vagrant is a tool which works with virtualization software like Virtual Box and VMware to provide an easy way to create, configure and reproduce virtual machines with a known state. It allows for pre-configured virtual machines, or boxes, to be pulled from the Vagrant Cloud, initialized, and run on your system. It makes it very simple for anybody to pull down an image, run a couple of simple commands, and have a virtual server to mess around with.

Installing Vagrant

You can download the zip file of Vagrant from the Downloads page. Extract it to find an app image. Make it executable and run it.

Alternatively, you can install it via your distro’s repositories:

Ubuntu/Debian/Ubuntu-based distros:

sudo apt install vagrant

Fedora:

 sudo dnf install vagrant
Vagrant Linux Apt Install

Vagrant can work with Hyper-V, VMWare, Parallels, VirtualBox, and libvirt. For this tutorial, I’ll be using libvirt (instructions on how to configure libvirt), as it’s the hypervisor built into the Linux Kernel.

To install the Vagrant libvirt provider, use one of the following commands:

sudo apt install vagrant-libvirt

or

vagrant plugin install vagrant-libvirt
Vagrant Linux Apt Install Vagrant Libvirt

Starting Your First Vagrant Box

To get Vagrant up and running, you have to add a box, create a directory for the virtual machine (VM) and run two Vagrant commands: one to initialize the system and one to start the VM.

First, add a box to your system:

vagrant box add centos/7 --provider=libvirt

Make sure to add your provider as a flag. You can find more boxes here.

Next, create a directory and cd into it:

mkdir vagrant-test
cd vagrant-test

Now, initialize Vagrant:

vagrant init centos/7

As part of the initialization phase, Vagrant will create a file called “Vagrantfile” in the current working directory (e.g. vagrant-test1). You can create as many directories and initialize Vagrant as many times as you like. Each directory and Vagrantfile represents one virtual machine. These different VMs can be based on the same box (e.g. precise32) or on different boxes.

To start the VM, run:

vagrant up

Once booted, the virtual machine is running in a headless mode (without a monitor or virtual screen), and you can only connect to it via SSH. You can do this via a normal SSH client from any other machine on your network, or you can use the built-in ssh command. If you want to use another SSH client, note that the correct IP address is the IP address of the host machine (the PC running Vagrant and your provider) but on a different port. During the bootup, Vagrant will show how port 22 (for SSH) has been forwarded. It will likely be to port 2222.

To use the built-in ssh command, type:

vagrant ssh

You are now connected to the VM. To leave the SSH connection, type “exit” or press Ctrl + D.

Stopping Your First Vagrant Box

To stop a running VM, use:

vagrant halt

and to delete the VM, use:

vagrant destroy

When a VM is destroyed, the base operating system (from the .box file) remains stored internally in Vagrant, and further VMs can be started whenever necessary without Vagrant downloading the .box file again.

In just two commands (vagrant init and vagrant up), Vagrant allows you to boot up a fully-functional, SSH-accessible virtual Linux machine. Advanced configuration happens via the Vagrantfile. You can find more details in the Vagrant documentation.

Make sure you check out some of our other virtualization content, such as what is virtualization and why you should use it and how to get started with Virtual Machine Manager on Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

John Perkins

John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.