DEV Community

Pedro Garcia Rodriguez
Pedro Garcia Rodriguez

Posted on • Updated on

Vagrant For Beginners: Creating Your First Virtual Environment.

In the previous article, we explored Vagrant 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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 or Puppet, managing multiple the virtual machines in a single project, and using different base boxes to meet your development needs.

Vagrant's documentation is a valuable resource for diving deeper into its capabilities.

Top comments (0)