Overview
Vagrant needs a virtualization provider to create environments. We'll use Docker as our provider because it's fast, lightweight, and essential for modern DevOps.
The setup order is:
- Install Docker (the provider)
- Install Vagrant (the orchestrator)
- Install the Docker plugin for Vagrant
Installing Docker
Create a script called install-docker.sh:
#!/bin/bash
# Update system packages
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Update package list
sudo apt-get update
# Install Docker
sudo apt-get install -y docker-ce
# Verify installation
docker --version
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Add current user to docker group (avoids needing sudo)
sudo usermod -aG docker $USER
echo "Docker installed successfully! Log out and back in for group changes to take effect."Make it executable and run:
chmod +x install-docker.sh
./install-docker.shAfter installation, log out and back in, then verify:
docker --version
# Output: Docker version 25.x.x, build xxxxxxx
docker run hello-world
# Should pull and run the hello-world containerInstalling Vagrant
# Add HashiCorp GPG key and repository
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
# Install Vagrant
sudo apt update && sudo apt install vagrantVerify the installation:
vagrant versionExpected output:
Installed Version: 2.x.x
The Vagrant CLI
Vagrant is controlled entirely from the command line. View all available commands:
vagrant --helpEssential Commands
| Command | Purpose |
|---|---|
vagrant init | Create a new Vagrantfile |
vagrant up | Start and provision the environment |
vagrant ssh | Connect to a running machine via SSH |
vagrant halt | Stop the machine |
vagrant destroy | Delete the machine completely |
vagrant status | Show current machine state |
vagrant reload | Restart and reload configuration |
vagrant validate | Check Vagrantfile for errors |
vagrant global-status | Show all Vagrant environments |
Getting Help on Any Command
vagrant <COMMAND> -h
# Examples:
vagrant up -h
vagrant ssh -h
vagrant destroy -hInstalling the Docker Plugin
Vagrant needs a plugin to communicate with Docker:
vagrant plugin install dockerExpected output:
Installing the 'docker' plugin. This can take a few minutes...
Installed the plugin 'docker (0.4.0)'!
Verify installed plugins:
vagrant plugin listPlugin Management
# Install a plugin
vagrant plugin install <plugin-name>
# Remove a plugin
vagrant plugin uninstall <plugin-name>
# Update a plugin
vagrant plugin update <plugin-name>
# List all plugins
vagrant plugin listUpdating Vagrant
Since we installed from the HashiCorp repository, updates come through apt:
sudo apt update
sudo apt upgrade vagrantVerifying Everything Works
Let's do a quick sanity check:
# Docker is running
docker ps
# Vagrant is installed
vagrant version
# Docker plugin is available
vagrant plugin list | grep dockerIf all three commands succeed, you're ready to create your first Vagrant environment.
Summary
- Docker is installed first as the virtualization provider
- Vagrant is installed from HashiCorp's official repository
- The Docker plugin connects Vagrant to Docker
- The
vagrantCLI has subcommands for every lifecycle action - Use
vagrant <command> -hfor help on any command
Next Steps
With both tools installed, we're ready to create our first Vagrant environment. In the next lesson, we'll write a Vagrantfile and deploy our first container.