Tabla de contenido
Hey everyone! Today we’re going to dive into the world of Vagrant, a fantastic tool that allows us to manage virtualized development environments quickly and easily. If you’re a developer, you know how difficult it can be to configure and maintain consistent and efficient development environments. Well, Vagrant is the solution to those problems. Let’s check it out!
What is Vagrant and why should you use it?
Vagrant is an open-source tool that allows us to create, configure, and manage virtualized development environments. With Vagrant, we can have a uniform and controlled development environment on our machine, regardless of the operating system we use. This way, we avoid compatibility issues and can focus on what really matters: developing!
But what are the advantages of Vagrant? Well, some of them are:
- It facilitates collaboration among developers since everyone can work in the same environment.
- It simplifies the configuration and management of virtual machines.
- It allows for the automation of the creation and provisioning of development environments.
- It encourages the use of good development practices, such as infrastructure as code.
Installing and configuring Vagrant
To install Vagrant, we first need to have a virtualization provider on our machine. One of the most popular ones is VirtualBox, but we can also use VMware, Hyper-V, among others. In this article, we will focus on VirtualBox. To install it, simply follow the instructions on the official VirtualBox website.
Once the virtualization provider is installed, we can download Vagrant from its official website. There we will find versions for Windows, macOS, and Linux. Download and install the appropriate version for your operating system.
Getting started with Vagrant
Now that we have Vagrant installed, let’s create our first virtualized development environment. To do so, we will follow these steps:
Open a terminal and create a new directory for our project:
mkdir my-first-vagrant-environment cd my-first-vagrant-environment
Initialize Vagrant in the directory:
This command will create a file called Vagrantfile in our directory. This file is the key to configure and customize our virtualized development environment.
Edit the Vagrantfile with your favorite text editor and add the following line:
config.vm.box = "hashicorp/bionic64"
This line indicates that we will use the “hashicorp/bionic64” image as the base for our virtual machine. This image is a 64-bit version of Ubuntu 18.04 (Bionic Beaver). There are many other images available in the official Vagrant catalog, which you can explore in Vagrant Cloud.
Start the virtual machine with the command:
Vagrant will download the image (if it hasn’t already) and create a new virtual machine based on it. This process may take a while, depending on the speed of your internet connection and your computer.
Once the virtual machine is up and running, we can connect to it via SSH:
Congratulations! You are now connected to your first virtualized development environment with Vagrant. You can start installing software, developing applications, and experimenting without fear of breaking your local environment.
One of the most interesting features of Vagrant is provisioning, which allows us to automate the configuration and installation of software on our virtual machines. Vagrant is compatible with several provisioning systems, such as Shell, Puppet, Ansible, and Chef, among others.
To illustrate how provisioning works, we will use a simple Shell script. Add the following lines to your Vagrantfile, just below config.vm.box = “hashicorp/bionic64”:
config.vm.provision "shell", inline: <<-SHELL sudo apt-get update sudo apt-get install -y git nginx SHELL
These lines indicate that Vagrant should run a Shell script that updates the Ubuntu package repositories and installs Git and Nginx. To apply these changes, we must reprovision our virtual machine with the command:
vagrant reload --provision
Once the process is complete, our virtual machine will have Git and Nginx installed.
Basic Vagrant commands
Here’s a list of some basic Vagrant commands that will be useful in your day-to-day:
- vagrant init: Initializes a new Vagrant environment in the current directory.
- vagrant up: Starts the virtual machine.
- vagrant ssh: Connects to the virtual machine via SSH.
- vagrant halt: Shuts down the virtual machine.
- vagrant reload: Restarts the virtual machine.
- vagrant destroy: Deletes the virtual machine and all its resources.
- vagrant status: Shows the status of the virtual machine.
- vagrant global-status: Shows the status of all virtual machines on your system.
- vagrant box: Manages virtual machine images (boxes) on your system.
Working with multiple virtual machines
Vagrant allows us to easily manage multiple virtual machines in the same project. To do so, we simply need to add a new virtual machine definition in our Vagrantfile. For example, if we want to add a second virtual machine with CentOS 7, we could do the following:
config.vm.define "centos" do |centos| centos.vm.box = "centos/7" centos.vm.hostname = "centos.local" centos.vm.network "private_network", ip: "192.168.33.20" end
With this configuration, we have created a new virtual machine called “centos” based on the “centos/7” image. Additionally, we have assigned it a hostname and an IP address on a private network.
To start both virtual machines, simply run the vagrant up command. If we want to start only one of them, we can specify its name:
vagrant up centos
We can connect to the CentOS virtual machine via SSH with the following command:
vagrant ssh centos
File synchronization between the host and the virtual machine
Vagrant facilitates file synchronization between our host machine and the virtual machines. By default, the directory where our Vagrantfile is located is automatically synchronized with the /vagrant directory inside the virtual machine. This allows us to easily share files between both environments.
If we want to configure a custom shared folder, we can do so by adding the following line to our Vagrantfile:
config.vm.synced_folder "my-local-folder", "/my-remote-folder"
This line indicates that the “my-local-folder” folder on our host machine will be synchronized with the “/my-remote-folder” folder in the virtual machine. Vagrant will take care of keeping both directories synchronized automatically.
Networking in Vagrant
Vagrant offers us several options to configure the network in our virtual machines. Some of the most common ones are:
Private network: Allows virtual machines to communicate with each other and with the host machine through a private network. To configure a private network, add the following line to your Vagrantfile:
config.vm.network "private_network", ip: "192.168.33.10"
Public network: Connects the virtual machine directly to the public network, allowing other machines on the network to access it. To configure a public network, add the following line to your Vagrantfile:
Port forwarding: Allows access to services in the virtual machine through a specific port on the host machine. To configure port forwarding, add the following line to your Vagrantfile:
config.vm.network "forwarded_port", guest: 80, host: 8080
This line indicates that port 80 in the virtual machine will be forwarded to port 8080 on our host machine.
And that’s it! With these basics, you should be able to start using Vagrant to manage your virtual development environments. Happy developing!