
What is Vagrant?
Vagrantis an open-source tool for creating and managing reproducible virtual development environments.
It sits on top of a virtualization provider such as VirtualBox, VMware, or libvirt. Instead of manually creating a virtual machine, installing an operating system, configuring networking, and setting up software, you describe the environment in a Vagrantfile and let Vagrant automate it.
Typical workflow:
Vagrantfile
↓
vagrant up
↓
Virtual machine created
↓
OS + configuration provisioned
↓
vagrant ssh
↓
Development environment ready
A useful distinction is:
-
Vagrant → manages and configures virtual machines.
-
VirtualBox/libvirt/VMware → actually provides the virtualization.
-
Vagrant box → a reusable base VM image.
-
Vagrantfile → configuration describing your VM.
1. Install a virtualization provider
For a simple Linux setup, VirtualBox is a common choice.
On Ubuntu/Debian-based systems:
sudo apt update
sudo apt install virtualbox
Verify it:
VBoxManage --version
If you prefer libvirt/KVM, you can use that instead, but the Vagrant provider configuration is different.
2. Download and install Vagrant
The safest approach is to download Vagrant from the official HashiCorp distribution.
Vagrant downloads — HashiCorp
For Debian/Ubuntu, the downloaded package will generally be a .deb. Install it with:
sudo dpkg -i vagrant_*.deb
If dpkg reports missing dependencies:
sudo apt-get install -f
Then verify the installation:
vagrant --version
You should see something similar to:
Vagrant 2.x.x
The exact current version can change, so use HashiCorp's download page rather than hard-coding a version into an installation script.
3. Create your first Vagrant environment
Create a directory for your project:
mkdir ~/vagrant-test
cd ~/vagrant-test
Initialize Vagrant:
vagrant init ubuntu/jammy64
This creates a file called:
Vagrantfile
The Vagrantfile contains the VM configuration.
You can inspect it:
cat Vagrantfile
4. Start the virtual machine
Run:
vagrant up
Vagrant will:
-
Check whether the requested box is available.
-
Download it if necessary.
-
Create a VM using your configured provider.
-
Configure networking and other settings.
-
Boot the VM.
The first vagrant up can take several minutes because the box has to be downloaded.
Check its status with:
vagrant status
5. Connect to the VM
Once the VM is running:
vagrant ssh
You'll get a shell inside the virtual machine.
For example:
vagrant@ubuntu-jammy:~$
You can verify the operating system:
cat /etc/os-release
When finished:
exit
6. Configure the VM with the Vagrantfile
A basic Vagrantfile might look like this:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
end
This specifies:
-
ubuntu/jammy64as the base box. -
A private IP address of
192.168.56.10. -
2 GB of RAM.
-
2 virtual CPUs.
-
VirtualBox as the provider.
After changing the configuration, apply it with:
vagrant reload
7. Install software automatically
One of Vagrant's biggest advantages is provisioning.
For example:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y nginx git curl
SHELL
end
Then:
vagrant up
Vagrant will run those commands inside the VM.
You can also explicitly run provisioning later:
vagrant provision
This is useful for creating development environments that other developers can reproduce consistently.
8. Useful Vagrant commands
Command
Purpose
vagrant init
Create a new Vagrantfile
vagrant up
Create/start the VM
vagrant ssh
Connect to the VM
vagrant halt
Shut down the VM
vagrant reload
Restart and apply configuration changes
vagrant suspend
Suspend the VM
vagrant resume
Resume a suspended VM
vagrant status
Show VM status
vagrant provision
Run provisioning scripts
vagrant destroy
Delete the VM
vagrant box list
List installed boxes
vagrant box add
Add a box manually
For example, when you're completely finished with a test environment:
vagrant destroy
This removes the VM, while leaving your Vagrantfile intact. You can recreate the environment later with:
vagrant up
9. Shared folders
By default, Vagrant typically makes the project directory available inside the VM.
For example, if your project is:
~/vagrant-test/
the directory is commonly mounted as:
/vagrant
Inside the VM:
cd /vagrant
ls
This is convenient because you can edit files on your Linux host while programs inside the VM use those files.
10. A complete small example
You could create a development VM with:
mkdir ~/my-vm
cd ~/my-vm
vagrant init ubuntu/jammy64
Then replace the generated Vagrantfile with:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.network "private_network", ip: "192.168.56.20"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y git curl nginx
SHELL
end
Start it:
vagrant up
Connect:
vagrant ssh
Check Nginx:
systemctl status nginx
When you're done:
exit
vagrant halt
And when you want to completely remove the VM:
vagrant destroy
In short
The core Vagrant workflow is:
# Create configuration
vagrant init ubuntu/jammy64
# Build/start VM
vagrant up
# Enter VM
vagrant ssh
# Leave VM
exit
# Stop VM
vagrant halt
# Remove VM
vagrant destroy
For current installation instructions and supported providers, HashiCorp's official Vagrant documentation is the authoritative reference.

0 commenti:
Post a Comment