Vagrant tutorial for beginners
Published: 2024-01-07 09:43:50


Vagrant is a tool used to simplify building and managing Virtual Machines in a very simple yet powerful way. If you, like me, are tired of installing Linux Virtual Machines as I do, you will welcome Vagrant into your life. Let's learn more about Vagrant!

<table><tbody><tr><td><strong><span style="text-decoration: underline;">Table of contents</span></strong></td></tr><tr><td><a href="#intro">Introduction</a></td></tr><tr><td><a href="#installation">Vagrant installation</a></td></tr><tr><td><a href="#basic-commands">Vagrant basic commands</a></td></tr><tr><td><a href="#Vagrantfile">Vagrantfile</a></td></tr><tr><td><a href="#final-thoughts">Final thoughts</a></td></tr><tr><td><a href="#resources">Resources</a></td></tr></tbody></table>

## Introduction

How many times have you created a Virtual Machine?

You know the drill: You download a [Linux](https://letslearnabout.net/hacking/linux-file-system/) image, create a Virtual Machine, select your options, add the .iso file, start the installation process then wait for something between 15 and 30 minutes. If you did this once or twice every year, it would be ok, but sometimes you need to create a lot for Virtual Machines, and time is lost by waiting.

What if we could simplify and speed up the process?

### Enter Vagrant

Vagrant is an open-source tool for building and managing virtualized development environments. Vagrant uses configuration files to define the virtual machine settings and provisions them with the required software and tools. Or you can run a few commands and install a Virtual Machine in seconds. This approach ensures that all team members work in the same development environment, reducing compatibility issues and streamlining the development process.

With Vagrant, instead of doing all the _Download a Linux image, create a Virtual Machine, etc_ processes, we will fetch an existing image from a Vagrant repository and add it to your Virtual Machine manager in 2 minutes. It is not an expression, it takes 2 minutes.

### How does it work?

Vagrant downloads a pre-existing Virtual Machine installation (Vagrant calls them **boxes**), adds it to your Virtual Machine manager and sets the configuration automatically.

How cool is that?

You can run the command _vagrant init generic/ubuntu2204_ to download an Ubuntu 22.04 Jellyfish box, and _vagrant up_ to start the Virtual Machine. Now, you can open it in your VM or just connect to it with the command _vagrant ssh_.

## Vagrant installation

As the installation depends on what Operating System you have, you just need to enter a few commands in your terminal. As this changes every few months/years, I'll link you to the [official Vagrant Installation](https://developer.hashicorp.com/vagrant/install) site.

For example, for Ubuntu/Debian, open your terminal and enter:

`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
sudo apt update && sudo apt install vagrant`

You also need to have installed a virtualization program, such as [VirtualBox](https://www.virtualbox.org/wiki/Downloads).

### Downloading your first Vagrant box

Ok, you have Vagrant and VirtualBox installed, now what?

Why don't we start with a download? Download a Centos VM with

`vagrant init geerlingguy/centos7`

Then start it with:

`vagrant up`

The VM is up and running. Now you can open it on VirtualBox or, if you don't need the GUI, you can connect to it via SSH with:

`vagrant ssh`

As simple as that. As Vagrant manages all the processes, you don't need to enter the username or the IP to connect to it. Vagrant boxes come with a user already created (Username: vagrant, Password: vagrant) so you can log in to the machine.

## Vagrant basic commands

We just saw how we can download and create a new Virtual Machine, but what more we can do with Vagrant?

Here's a list of basic commands:

### Creating a VM

Install a box
`vagrant init <BOX_NAME>`

### Managing a Vagrant VM state

Start a Vagrant VM
`vagrant up`

Stop a Vagrant VM
`vagrant halt`

Resume a Vagrant VM
`vagrant resume`

Reload a Vagrant VM
`vagrant reload`

Suspend a Vagrant VM
`vagrant suspend`

Delete a Vagrant VM
`vagrant destroy`

### Vagrant VM status

Check a Vagrant VM state
`vagrant status`

Check all Vagrant VM states
`vagrant global-status`

### Provisioning

Force provisioning
`vagrant provision`

Use the debug flag to add more verbosity
`vagrant provision --debug`

Restart the virtual machine and force provisioning
`vagrant reload --provision`

### Connecting to a VM

Connect to a Vagrant VM via SSH
`vagrant ssh`

Connect to a specific Vagrant VM via SSH
`vagrant ssh <BOX_NAME>`

### Managing boxes

List all boxes installed
`vagrant box list`

Check of updates
`vagrant box outdated`

Download a box
`vagrant box add <BOX_NAME> <URL>`

Delete a box
`vagrant box remove <BOX_NAME>`

## Vagrantfile

We can also edit your Vagrant boxes with a Vagrantfile.

A Vagrantfile is a configuration file used to define and configure the various aspects of Vagrant-managed virtual machines. It allows us to specify settings such as the virtual machine's networking, and provisioning, among other parameters.

Then, we can use this Vagrantfile on multiple machines at the same time to create Virtual Machines with the same specs. How cool is that?

If you want to edit a Vagrantfile, open it with your IDE (Atom, Notepad++, Sublime text, VIM, VS Code...) and uncomment any text you want to use.

### Examples

Add a bridged port
`config.vm.network "public_network"`

Add a bridged port with a static IP
`config.vm.network "private_network", ip: "192.168.1.222"`

Specify VM's memory and CPUs
`  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = 2
  end
`
Share a folder from your host PC to the Vagrant VM
`config.vm.synced_folder "C:\\Users\\<YOUR_USERNAME>\\Desktop\\shared_files", "/home/vagrant/vagrant_data"`

Provisioning the VM
`  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y apache2
  SHELL`

Of course, there are more options. You can read more about them in the [official Documentation](https://developer.hashicorp.com/vagrant/docs/vagrantfile/machine_settings).

You can also download my [Vagrant Cheat Sheets](https://github.com/david1707/devops-cheatsheets/blob/main/Vagrant/Vagrant-Cheat-Sheet.md).

## Final thoughts

As we already saw, Vagrant offers several benefits for developers and IT professionals, including:

1. **Consistent Development Environments**: Vagrant ensures that every team member works in an identical environment, reducing "it works on my machine" issues.

3. **Efficiency**: It streamlines the setup process for new team members, as well as the onboarding of existing projects.

5. **Isolation**: Vagrant keeps project dependencies and configurations isolated from each other, preventing conflicts between projects.

7. **Cross-Platform Compatibility**: Vagrant provides a consistent environment across different operating systems, promoting seamless collaboration.

9. **Reproducibility**: With Vagrant, it's easy to reproduce environments for testing, debugging, and production, enhancing overall reliability.

11. **Resource Optimization**: It allows for efficient resource usage by consolidating multiple development environments on a single machine.

Overall, Vagrant simplifies the development workflow, enhances collaboration, and improves the reliability of software projects.

And you just saw how easy it is to use. At the bare minimum, you can work with just 2 or 3 commands to download, run and connect to it via SSH.

Vagrant should become a tool in your belt, professionally or not.

## Resources

[Original post](https://letslearnabout.net/devops/vagrant-tutorial-beginners/)

[Vagrant installation](https://developer.hashicorp.com/vagrant/install)

[VirtualBox installation](https://www.virtualbox.org/wiki/Downloads)

[VagrantfileDocs](https://developer.hashicorp.com/vagrant/docs/vagrantfile/machine_settings)

[Linux File System](https://letslearnabout.net/hacking/linux-file-system/)

[Vagrant Cheat Sheet](https://github.com/david1707/devops-cheatsheets/blob/main/Vagrant/Vagrant-Cheat-Sheet.md)