Installing Docker & Vagrant

20 minLesson 3 of 5

Learning Objectives

  • Install Docker on Ubuntu
  • Install Vagrant on Ubuntu
  • Verify both installations
  • Understand the Vagrant CLI and its subcommands

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:

  1. Install Docker (the provider)
  2. Install Vagrant (the orchestrator)
  3. 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.sh

After 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 container

Installing 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 vagrant

Verify the installation:

vagrant version

Expected output:

Installed Version: 2.x.x

The Vagrant CLI

Vagrant is controlled entirely from the command line. View all available commands:

vagrant --help

Essential Commands

CommandPurpose
vagrant initCreate a new Vagrantfile
vagrant upStart and provision the environment
vagrant sshConnect to a running machine via SSH
vagrant haltStop the machine
vagrant destroyDelete the machine completely
vagrant statusShow current machine state
vagrant reloadRestart and reload configuration
vagrant validateCheck Vagrantfile for errors
vagrant global-statusShow all Vagrant environments

Getting Help on Any Command

vagrant <COMMAND> -h
 
# Examples:
vagrant up -h
vagrant ssh -h
vagrant destroy -h

Installing the Docker Plugin

Vagrant needs a plugin to communicate with Docker:

vagrant plugin install docker

Expected output:

Installing the 'docker' plugin. This can take a few minutes...
Installed the plugin 'docker (0.4.0)'!

Verify installed plugins:

vagrant plugin list

Plugin 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 list

Updating Vagrant

Since we installed from the HashiCorp repository, updates come through apt:

sudo apt update
sudo apt upgrade vagrant

Verifying 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 docker

If 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 vagrant CLI has subcommands for every lifecycle action
  • Use vagrant <command> -h for 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.