LinuxCommandLibrary

vagrant

Create and manage virtual machines

TLDR

Create a Vagrantfile in the current directory with the base Vagrant box

$ vagrant init
copy

Create a Vagrantfile with a box from the Vagrant Public Registry
$ vagrant init [ubuntu/focal64]
copy

Start and provision the Vagrant environment
$ vagrant up
copy

Suspend the machine
$ vagrant suspend
copy

Halt the machine
$ vagrant halt
copy

Connect to the machine via SSH
$ vagrant ssh
copy

Output the SSH configuration file of the running Vagrant machine
$ vagrant ssh-config
copy

List all local boxes
$ vagrant box list
copy

SYNOPSIS

vagrant <command> [<args>]

PARAMETERS

init
    Initializes a new Vagrant environment by creating a Vagrantfile in the current directory.

up
    Starts and provisions the Vagrant environment according to the Vagrantfile, creating or resuming the VM.

ssh
    Connects to the running Vagrant machine via SSH. Use with vagrant ssh-config for manual SSH.

halt
    Gracefully shuts down the running Vagrant machine. The machine state is saved.

suspend
    Suspends the running Vagrant machine, saving its current state to disk for quick resume.

resume
    Resumes a suspended Vagrant machine, restoring it to its previous state.

destroy
    Stops and deletes all resources created by Vagrant for the current environment, including the VM.

provision
    Runs the provisioners again on a running Vagrant machine without restarting it.

reload
    Halts, reloads the Vagrantfile configuration, and reprovisions the machine.

status
    Outputs the current state of the Vagrant machine(s) in the current environment.

box
    Manages boxes (base images) that Vagrant uses. Includes subcommands like add, list, remove, update.

global-status
    Lists all active Vagrant environments on the system, regardless of the current directory.

DESCRIPTION

Vagrant is an open-source tool by HashiCorp designed to build and maintain portable, reproducible development environments. It aims to reduce environment setup time and ensure consistency across development teams. By providing a single workflow for managing virtual machines, Vagrant abstracts away the complexities of various virtualization providers like VirtualBox, VMware, Hyper-V, and even Docker.

Developers define their environment's configuration in a simple text file called a Vagrantfile, specifying the base operating system (a "box"), network settings, shared folders, and provisioning scripts (e.g., for installing software like web servers or databases). Once configured, a single vagrant up command creates and provisions the entire environment, ready for development.

This approach eliminates the "it works on my machine" problem, as everyone on the team operates within an identical, disposable virtual environment. Vagrant environments are lightweight, easy to share, and can be quickly torn down and recreated, making them ideal for testing, onboarding new team members, and ensuring a consistent development experience.

CAVEATS

Vagrant relies heavily on underlying virtualization providers (e.g., VirtualBox, VMware), which must be installed separately. While powerful for development, it is not designed for production deployment. Performance can vary depending on the host machine's resources and the complexity of the guest environment. Debugging provisioning issues can sometimes be challenging without direct VM access.

VAGRANTFILE

The central configuration file (named Vagrantfile) is written in Ruby syntax and defines every aspect of the Vagrant environment, including the base box, network configurations, synced folders, and provisioning scripts. It is the primary way users interact with and customize their Vagrant environments.

BOXES

Vagrant "boxes" are the base images from which virtual machines are cloned. They are pre-packaged operating system environments that significantly speed up the creation of new VMs. Users can find and add boxes from the official Vagrant Cloud or create their own custom boxes.

HISTORY

Vagrant was initially created by Mitchell Hashimoto in 2010 as an open-source project to streamline the setup of development environments. It gained rapid popularity due to its simplicity and ability to standardize development workflows across teams. The project was later incorporated into HashiCorp, the company co-founded by Hashimoto, becoming a flagship product. Its development has continued steadily, adding support for various virtualization providers and cloud platforms, solidifying its role as a key tool in DevOps and modern software development practices.

SEE ALSO

virtualbox(1), docker(1), ssh(1), ansible(1), puppet(1), chef(1)

Copied to clipboard