# Vagrant — Reproducible VM Development Environments

> Practical guide to Vagrant — VM-based development environments with Vagrantfile, boxes, providers, provisioning and snapshots on the command line.

Source: https://www.jpkc.com/db/en/cheatsheets/containers/vagrant/

<!-- PROSE:intro -->
Vagrant by HashiCorp builds reproducible development environments as full virtual machines – not containers. The entire configuration lives in a single file, the `Vagrantfile`, which you version alongside your project. Prebuilt boxes serve as the base images, and Vagrant boots them through a provider such as VirtualBox, VMware, Hyper-V or libvirt. A single `vagrant up` gives every team member the exact same environment. This guide walks you through the essential commands – from initializing through provisioning to snapshots.
<!-- PROSE:intro:end -->

## Initialize & Start

`vagrant init <box>` — Create a Vagrantfile for a specific box.

```bash
vagrant init ubuntu/jammy64
```

`vagrant up` — Start and provision the VM.

```bash
vagrant up
```

`vagrant up --provider=<name>` — Start with a specific provider.

```bash
vagrant up --provider=vmware_desktop
```

`vagrant up --no-provision` — Start without running provisioners.

```bash
vagrant up --no-provision
```

`vagrant reload` — Restart the VM (applies Vagrantfile changes).

```bash
vagrant reload
```

`vagrant reload --provision` — Restart and re-run provisioners.

```bash
vagrant reload --provision
```

## Stop & Destroy

`vagrant halt` — Gracefully shut down the VM.

```bash
vagrant halt
```

`vagrant halt -f` — Force shutdown the VM.

```bash
vagrant halt -f
```

`vagrant suspend` — Suspend the VM (save state to disk).

```bash
vagrant suspend
```

`vagrant resume` — Resume a suspended VM.

```bash
vagrant resume
```

`vagrant destroy` — Stop and delete the VM completely.

```bash
vagrant destroy
```

`vagrant destroy -f` — Force destroy without confirmation.

```bash
vagrant destroy -f
```

## SSH & Status

`vagrant ssh` — Connect to the VM via SSH.

```bash
vagrant ssh
```

`vagrant ssh -c '<command>'` — Run a command inside the VM via SSH.

```bash
vagrant ssh -c 'cat /etc/os-release'
```

`vagrant ssh-config` — Show SSH config (for use with ssh command).

```bash
vagrant ssh-config >> ~/.ssh/config
```

`vagrant status` — Show status of the current VM.

```bash
vagrant status
```

`vagrant global-status` — Show status of all Vagrant VMs system-wide.

```bash
vagrant global-status
```

`vagrant port` — Show port mappings for the VM.

```bash
vagrant port
```

## Provisioning

`vagrant provision` — Run provisioners on a running VM.

```bash
vagrant provision
```

`vagrant provision --provision-with <name>` — Run only a specific provisioner.

```bash
vagrant provision --provision-with shell
```

`vagrant upload <src> <dest>` — Upload a file to the VM.

```bash
vagrant upload config.yaml /home/vagrant/config.yaml
```

## Box Management

`vagrant box list` — List all installed boxes.

```bash
vagrant box list
```

`vagrant box add <name>` — Download and add a box.

```bash
vagrant box add ubuntu/jammy64
```

`vagrant box update` — Update the box for the current environment.

```bash
vagrant box update
```

`vagrant box remove <name>` — Remove a box from local storage.

```bash
vagrant box remove ubuntu/focal64
```

`vagrant box outdated` — Check if the box is outdated.

```bash
vagrant box outdated
```

`vagrant package --output <file>` — Package the current VM into a reusable box.

```bash
vagrant package --output mybox.box
```

## Snapshots

`vagrant snapshot save <name>` — Take a named snapshot of the VM.

```bash
vagrant snapshot save before-update
```

`vagrant snapshot restore <name>` — Restore a snapshot.

```bash
vagrant snapshot restore before-update
```

`vagrant snapshot list` — List all snapshots.

```bash
vagrant snapshot list
```

`vagrant snapshot delete <name>` — Delete a snapshot.

```bash
vagrant snapshot delete before-update
```

`vagrant snapshot push` — Take a quick snapshot (stack-based).

```bash
vagrant snapshot push
```

`vagrant snapshot pop` — Restore and delete the last pushed snapshot.

```bash
vagrant snapshot pop
```

<!-- PROSE:outro -->
## Conclusion

Vagrant shines wherever you need a complete, isolated environment – a different operating system, a kernel module or a setup that is awkward to reproduce with containers. Once your `Vagrantfile` is in place, onboarding a new team member is a single command. For lightweight, purely Linux-based services, containers are often the leaner choice; for reproducible VM environments, Vagrant remains the tool to reach for.

## Further Reading

- [Vagrant – official documentation](https://developer.hashicorp.com/vagrant/docs) – reference and handbook by HashiCorp
- [Vagrant Cloud – box catalog](https://portal.cloud.hashicorp.com/vagrant/discover) – ready-made boxes to plug in
- [Vagrant (software) – Wikipedia](https://en.wikipedia.org/wiki/Vagrant_(software)) – background and history
<!-- PROSE:outro:end -->

## Related Commands

- [ddev](https://www.jpkc.com/db/en/cheatsheets/containers/ddev/) – container-based local development environments for PHP projects
- [docker](https://www.jpkc.com/db/en/cheatsheets/containers/docker/) – build and run containers instead of full VMs
- [docker-compose](https://www.jpkc.com/db/en/cheatsheets/containers/docker-compose/) – orchestrate multiple containers declaratively

