#90DaysOfDevOps
Here, we will discuss the basics of Vagrant and how to use it to create a development environment. We will also provide some tips on how to use Vagrant to improve your development workflow.
What is Vagrant?
Vagrant is the command line utility for managing the lifecycle of virtual machines. Isolate dependencies and their configuration within a single disposable and consistent environment.
Vagrant uses a configuration file, called a Vagrantfile, to define the virtual machine. The Vagrantfile can be used to specify the operating system, the amount of memory, the number of CPUs, and other settings for the virtual machine.
How do we use Vagrant?
You must install Vagrant and virtualization software, such as VirtualBox or VMware, in order to use it. You can make a Vagrantfile once you've set up the virtualization program and installed it.
A config file called the Vagrant file describes the virtual machine. A text editor or a vagrant generator can be used to create the vacuous file.
The vagrant up command can be used to create a virtual machine once you've created the vagrant file. The operating system image will be downloaded and installed on the virtual machine using the vagrant up
command.
You can connect to the virtual machine using the vagrant ssh
command once it has been created. The virtual machine's terminal window will be opened by the stray ssh command.
Let's dive into the Vagrant setup
Prerequisites
Before we begin, ensure that you have the following installed on your machine:
If using the Windows version of Linux Distros from Microsoft Store then remember to set the environment variable as below
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export PATH="${PATH}:/mnt/c/Program Files/Oracle/VirtualBox"
Setting up Vagrant
Create a new directory for your Vagrant project:
mkdir my-vagrant cd my-vagrant
Initialize a new Vagrant environment:
vagrant init
This will create a new
Vagrantfile
in your project directory.
Choose a base box to use for your virtual machine. A base box is a preconfigured virtual machine image that Vagrant uses as a starting point. You can find a list of available base boxes at Vagrant Cloud.
For example, to use the Ubuntu 20.04 LTS base box, add the following line to your
Vagrantfile
:config.vm.box = "ubuntu/focal64"
Customize your virtual machine by adding configuration options to your
Vagrantfile
.For example, to forward port 80 from your virtual machine to your host machine, add the following line:
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "192.168.1.10"
You can find a list of available configuration options in the Vagrant Boxes.
Start your virtual machine by running:
vagrant up
This will download the base box if it is not already installed, and create a new virtual machine instance.
SSH into your virtual machine by running:
vagrant ssh
This will open a new terminal window and connect you to your virtual machine.
Now you have successfully set up Vagrant on your local machine! You can now use Vagrant to manage your virtual machine environments in a single workflow. For more information on using Vagrant, check out the Official documentation.