Vagrant Provisioning Made Easy: Your Path to Seamless Development - Day 13/90
Why use Vagrant for Provisioning?
Before we jump into provisioning, let's understand what makes Vagrant so fantastic.
๐น Reproducibility: Vagrant lets you define your environment as code. Replicate it on different machines for consistency.
๐น Portability: Package your Vagrant environment into a single file, easily shareable with team members.
๐น Isolation: Vagrant keeps your development environment separate from your host machine, reducing conflicts.
Before we begin, I consider you have installed Vagrant if not then check out the below article:
Now, let's get practical!
NOTE - Vagrant Provisioning by default works only one time when the virtual machine is booted for the first time. If you want to provision a virtual machine again use
vagrant reload --provision
to forcefully provision the machine.
Step 1: Create a Project Directory ๐
Set up a new directory for your Vagrant project. This is where all the magic will happen. Here, I have created a directory named Ubuntu in which we are going to initialize the ubuntu vm.
Step 2: Initialize Vagrant ๐งโโ๏ธ
In your terminal or git bash, navigate to your project directory in my case its ubuntu and run:
vagrant init generic/ubuntu2204
This creates a Vagrantfile
in your project directory, which is your VM's configuration file.
Here, generic/ubuntu2204 is the box name that is uploaded on the vagrant cloud.
Step 3: Configuration Time โ๏ธ
Edit the Vagrantfile
using your favorite text editor. Here's an example of configuring your Ubuntu VM:
Uncomment private and public networks if you want to connect the virtual machine to the internet also change the IP address according to your router.
Step 5: Time to Provision ๐ ๏ธ
Now, let's talk provisioning. Vagrant supports various provisioners like Shell, Ansible, and Puppet. They help automate software installation on your VM.
As an example, if you want to install Apache on your Ubuntu VM, add these lines to your Vagrantfile
:
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
And when you start the machine using vagrant up
you will see:
Let us check the IP address of the machine to ensure whether apache2 is installed or not.
It works apache2 is installed successfully in the system.
๐ In a Nutshell
In this article, I've demystified Vagrant provisioning and showed you how to set up an Ubuntu VM. Vagrant is a developer's best friend, simplifying the creation and management of development environments.
With Vagrant, you can script your environment, ensure reproducibility, and keep your projects isolated. It's a must-have tool in your stack. So, dive into the world of Vagrant for seamless provisioning and development. Happy coding! ๐๐ฉโ๐ป๐จโ๐ป