Vagrant For Beginners: Creating Your First Virtual Environment.
Tags: vagrant, devops, beginners, tutorial
Published: 2023-09-15 10:20:51

_In the previous article, we explored [Vagrant](https://www.vagrantup.com/) a powerful virtualization tool designed for creating and configuring portable, reproducible development environments through declarative configuration. Vagrant proves indispensable for developers seeking to develop and test applications within isolated environments before deploying them to production._

---

### Table of Contents
* [Initializing a Vagrant Project](#initialising-a-vagrant- project)
* [Starting the Virtual Machine] (#starting-the-virtual-machine)
* [Connecting to the Virtual Machine] (#connecting-to-the-virtual-machine)
* [Exiting the Virtual Machine] (#exiting-to-the-virtual-machine)
* [Cleanup](#cleanup)
* [Summary](#summary)

In this article, I'll take you through the essential steps to set up your very first Vagrant project. You'll learn how to create a dedicated project directory, initialize a `Vagrantfile`, setting the foundation for building and managing virtual machines with ease.

So, let's dive in and embark on your journey into the realm of Vagrant!

### Initializing a Vagrant Project

Initializing a Vagrant project creates a `Vagrantfile`, which is a configuration file that tells Vagrant how to create and manage your virtual machine.

This ensures that everyone on your team has the same development environment, automates the creation and management of virtual machines, and makes it easy to share your development environment with others.

Additionally, you can use Vagrant to test your code in a controlled environment.

To initialise a Vagrant project open a Terminal application and create a new directory for your Vagrant project.

You can name it anything you like.

```
breakingpitt@converge~ mkdir ubuntu & cd ubuntu
```

Once you have created the project's directory run the following command to initialise a new Vagrant project within the directory.

```
breakingpitt@converge~ vagrant init ubuntu/focal64
```

This action will create a `Vagrantfile` in your project directory, which is used to define your virtual machine's configuration. This file is filled with a lot of comments with information regarding the configuration of the Vagrant environment.

If you want a easy to read `Vagrantfile` with the lines needed to create and execute your environment, just run the following command:

```
breakingpitt@converge~ vagrant init ubuntu/focal64 --minimal
```

The `Vagrantfile` will have the following content and it will be easy to read and modify:

```
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
end
```

### Starting the virtual machine

Once you have initialised and configured your Vagrant project the `vagrant up` command creates and configures a virtual machine according to the instructions in your `Vagrantfile`.

```
breakingpitt@converge~ vagrant up
```

This is the single most important command in Vagrant, since it is how any Vagrant machine is created.

### Connecting to the virtual machine.

Once the virtual machine is up and running, you can use the `vagrant ssh` command connects you to a running a virtual machine via SSH.

This allows you to interact with the virtual machine operating system and execute commands just as you would on a physical machine.

```
breakingpitt@converge~ vagrant ssh
```

You can now start working in the virtual machine. You can install software, run commands, and edit files.

### Exiting the virtual machine.

When you've completed your work within the virtual machine, you can exit the SSH session using the following command:

```
breakingpitt@converge~ exit
```

This will disconnect you from the the virtual machine and return you to your local terminal. Alternatively, you can exit the SSH session in these ways:

- Press Ctrl+D.
- Type logout and press Enter.
- Type ~. and press Enter. The ~. character sequence is a special way to terminate an SSH session. It is called the "escape character".

### Cleanup.

To halt or destroy the virtual machines when you've finished with the, utilize the following commands.

The `vagrant halt` command will stop the virtual machine, but it will not delete it:

```
breakingpitt@converge~ vagrant halt
```

If you wish to keep the virtual machines files but no longer run it, you can suspend it with the vagrant suspend command:

```
breakingpitt@converge~ vagrant suspend
```

Suspended virtual machines are saved in a paused state and can be resumed later using the vagrant resume command. Remember to stop and destroy your virtual machines when you're finished to free up computer resources and maintain a clean Vagrant directory.

The `vagrant destroy` command both stops and deletes the virtual machine:

```
breakingpitt@converge~ vagrant destroy
```

If you want to delete the downloaded Vagrant box used the first time you ran `vagrant up`, the `vagrant box remove` command will delete the downloaded box:

```
breakingpitt@converge~ vagrant box remove ubuntu/focal64
```

You should also delete any unused `Vagrantfile` to keep your Vagrant directory clean.

A few considerations when stopping and destroying virtual machines:

- If you have any changes that you have not yet committed to your project's repository, you will lose those changes if you destroy the virtual machine.
- If you are using a shared folder, unmount it before destroying the virtual machine.
- If you are using a private network, delete the network before destroying the virtual machine.

### Summary.

This getting started guide offers a foundational introduction to Vagrant. As you become more proficient with Vagrant, you can explore advanced configurations such as provisioning with tools like [Ansible](https://www.ansible.com/) or [Puppet](https://www.puppet.com/), managing multiple the virtual machines in a single project, and using different base boxes to meet your development needs.

[Vagrant's documentation] (https://www.vagrantup.com/docs) is a valuable resource for diving deeper into its capabilities.