Vagrant is a command-line tool that makes it easy to build and manage virtual machine environments. Developers use it to create a local setup that matches the production server exactly. This removes the classic “works on my machine” problem.
By default, Vagrant works with VirtualBox, Hyper-V, and Docker. You can add support for other providers like VMware, KVM, or AWS through plugins.
This guide will show you how to install Vagrant on Ubuntu 18.04 using VirtualBox as the provider. It also covers how to spin up your first virtual machine and use the most common Vagrant commands.
<strong>Prerequisite:</strong> You need sudo access on your Ubuntu machine.
Follow these three steps to get Vagrant installed and running.
Vagrant needs a provider to create virtual machines. We will use VirtualBox, which is available in the default Ubuntu repositories.
Install it with:
bashsudo apt install virtualbox
If you want the latest version of VirtualBox from Oracle’s own repositories, check the official VirtualBox download page for instructions.
The Vagrant version in the Ubuntu repositories is outdated. Instead, download the latest release directly from the official HashiCorp website.
At the time of writing, the latest version is 2.2.6. Always check the Vagrant downloads page to confirm the current release before running these commands.
Update your package lists first:
bashsudo apt update
Download the Vagrant package using curl:
bashcurl -O https://releases.hashicorp.com/vagrant/2.2.6/vagrant_2.2.6_x86_64.deb
Install the downloaded package:
bashsudo apt install ./vagrant_2.2.6_x86_64.deb
Check that Vagrant installed correctly by printing the version number:
bashvagrant --version
Output:
Vagrant 2.2.6
With Vagrant ready, you can now set up a development environment in just a few steps.
Start by creating a project directory and switching into it:
bashmkdir ~/my-first-vagrant-projectcd ~/my-first-vagrant-project
A Vagrantfile is the configuration file Vagrant uses to describe how your virtual machine should be set up. It is written in Ruby and sits at the root of your project folder.
Initialize a new Vagrantfile using the vagrant init command. You also specify the box you want to use. Boxes are pre-built machine images that Vagrant downloads and uses as a base. You can find public boxes at the Vagrant Cloud box catalog.
This example uses the centos/7 box:
bashvagrant init centos/7
Start the virtual machine:
bashvagrant up
Vagrant will download the box if it is not already cached, then boot the machine. Your project directory is automatically mounted inside the VM at /vagrant, so any file you edit on the host is available inside the machine straight away.
Once your machine is running, these are the commands you will use most often:
bashvagrant ssh
This opens an SSH session directly into the running VM. No passwords or keys required.
bashvagrant halt
This shuts down the running machine cleanly while keeping all the VM files on disk.
bashvagrant destroy
This stops the machine and removes all the resources created for it. Your Vagrantfile stays in place, so you can rebuild it at any time with vagrant up.
You now have Vagrant installed and a working virtual machine running on Ubuntu 18.04. The combination of Vagrant and VirtualBox gives you a powerful local development setup that is easy to share with your team. Leave a comment below if you run into any setup issues.
Both git fetch and git pull talk to a remote repository, but they do very different things to your…
Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…
Email is still one of the most important communication channels inside modern applications. Password resets,…
Nginx is a high-performance web server and reverse proxy trusted by some of the largest…
ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…
When you share a server with a team or investigate unexpected activity, the first question…