Vagrant
Portable Development Environments
Deploy Vagrant on your RamNode VPS to create reproducible, isolated virtual machine environments. Perfect for team collaboration, CI/CD pipelines, and standardized development workflows.
What is Vagrant?
Vagrant is an open-source tool developed by HashiCorp that provides a consistent workflow for building and managing virtual machine environments. It acts as a wrapper around virtualization platforms like VirtualBox, VMware, and KVM, allowing you to define your entire development environment as code through a simple configuration file called a Vagrantfile.
While traditionally used on local machines, running Vagrant on a VPS opens up new possibilities for team collaboration, CI/CD pipelines, and centralized development environments.
Key Use Cases
Development Standardization
Ensure all team members work with identical configurations, eliminating "works on my machine" problems.
Testing and QA
Create isolated environments for testing different software versions or configurations.
Multi-tenant Development
Run multiple isolated development environments on a single VPS for different projects.
CI/CD Integration
Automate testing pipelines with reproducible build environments.
Prerequisites
Before proceeding with the installation, ensure your RamNode VPS meets the following requirements:
- A RamNode KVM VPS with at least 4GB RAM (8GB recommended for running multiple VMs)
- Ubuntu 22.04 or 24.04 LTS, Debian 11/12, or Rocky Linux 8/9
- Root or sudo access to the server
- At least 20GB of available disk space (50GB+ recommended)
- Nested virtualization enabled
Verify Nested Virtualization
Nested virtualization allows you to run a virtual machine inside another virtual machine. Since your RamNode VPS is already a VM, you need nested virtualization support. RamNode KVM VPS instances support nested virtualization.
Check CPU virtualization support:
egrep -c '(vmx|svm)' /proc/cpuinfo
If the output is greater than 0, your CPU supports virtualization. For Intel CPUs, look for vmx; for AMD, look for svm.
Verify KVM modules are loaded:
lsmod | grep kvm
You should see kvm_intel or kvm_amd listed, along with the base kvm module.
Method 1: Vagrant with KVM/Libvirt (Recommended)
KVM (Kernel-based Virtual Machine) is a native Linux virtualization solution that offers excellent performance on VPS environments. This is the recommended approach for production use.
Install KVM and Dependencies
sudo apt update sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
Add your user to the libvirt group:
sudo usermod -aG libvirt $USER sudo usermod -aG kvm $USER
Enable and start libvirtd:
sudo systemctl enable libvirtd sudo systemctl start libvirtd
Log out and back in for group membership changes to take effect.
Install Vagrant
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install -y vagrant
Verify the installation:
vagrant --version
Install Vagrant Libvirt Plugin
vagrant plugin install vagrant-libvirt
If you encounter dependency issues, install the development packages:
sudo apt install -y libvirt-dev ruby-dev gcc make
Method 2: Vagrant with VirtualBox
VirtualBox is a more traditional option and may be familiar to developers who use Vagrant locally. However, it typically has higher overhead than KVM on server environments.
Install VirtualBox (Alternative)
sudo apt update sudo apt install -y virtualbox virtualbox-ext-pack
Add your user to the vboxusers group:
sudo usermod -aG vboxusers $USER
Log out and back in for the changes to take effect. Then follow the same Vagrant installation steps as in Method 1.
Creating Your First Virtual Machine
Initialize a Vagrant Project
mkdir ~/vagrant-test cd ~/vagrant-test vagrant init generic/ubuntu2204
This creates a Vagrantfile in the current directory with default settings.
Configure the Provider
Edit the Vagrantfile to configure the libvirt provider:
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.provider :libvirt do |libvirt|
libvirt.memory = 2048
libvirt.cpus = 2
end
endFor VirtualBox, use config.vm.provider :virtualbox instead.
Launch the Virtual Machine
vagrant up --provider=libvirt
The first time you run this command, Vagrant will download the box image, which may take several minutes.
Connect to the VM:
vagrant ssh
Advanced Vagrantfile Configuration
Multi-Machine Environment
Create multiple VMs in a single Vagrantfile for testing distributed systems
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.define "web" do |web|
web.vm.hostname = "web-server"
web.vm.network "private_network", ip: "192.168.56.10"
end
config.vm.define "db" do |db|
db.vm.hostname = "database"
db.vm.network "private_network", ip: "192.168.56.11"
end
endProvisioning with Shell Scripts
Automatically configure VMs during creation
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
SHELL
endSynced Folders
Share directories between your VPS and the Vagrant VM
config.vm.synced_folder "./data", "/vagrant_data"
Essential Vagrant Commands
| Command | Description |
|---|---|
| vagrant up | Start and provision the VM |
| vagrant halt | Gracefully shut down the VM |
| vagrant destroy | Delete the VM and all its resources |
| vagrant reload | Restart the VM and reload Vagrantfile configuration |
| vagrant status | Check the current state of VMs |
| vagrant ssh | Connect to the VM via SSH |
| vagrant suspend | Save VM state and stop (faster than halt) |
| vagrant resume | Resume a suspended VM |
| vagrant snapshot push | Create a snapshot of the current state |
Performance Optimization
Resource Allocation
Be conservative with memory and CPU allocation (60-70% of VPS resources max)
config.vm.provider :libvirt do |libvirt| libvirt.memory = 2048 # 2GB for the VM libvirt.cpus = 2 # 2 CPU cores libvirt.cpu_mode = 'host-passthrough' # Better performance end
Disk I/O Optimization
Use virtio drivers for better disk performance
config.vm.provider :libvirt do |libvirt| libvirt.disk_bus = 'virtio' end
Box Management
Regularly clean up unused boxes to free disk space
vagrant box list vagrant box remove <box-name> vagrant box prune
Troubleshooting
Nested Virtualization Not Available
- Verify that your RamNode VPS is a KVM instance (not OpenVZ)
- Check if KVM modules are loaded:
lsmod | grep kvm - Contact RamNode support to ensure nested virtualization is enabled for your VPS
Libvirt Connection Issues
sudo systemctl status libvirtd sudo systemctl restart libvirtd
Ensure you're in the libvirt and kvm groups and have logged out and back in.
Network Connectivity Problems
sudo virsh net-list --all sudo virsh net-start default sudo virsh net-autostart default
Out of Memory Errors
If the VPS runs out of memory, reduce VM allocations or upgrade your RamNode plan.
free -h htop
Security Considerations
- Firewall Configuration: Ensure your VPS firewall blocks access to Vagrant VM ports from the internet unless explicitly needed
- Default Credentials: Always change default Vagrant credentials (username: vagrant, password: vagrant) in production environments
- SSH Keys: Use SSH keys for authentication instead of passwords
- Updates: Keep Vagrant, the hypervisor, and box images updated regularly
- Network Isolation: Use private networks for VM-to-VM communication
Best Practices for Production Use
Version Control
Store Vagrantfiles in Git to track environment changes
Backup Strategy
Regularly snapshot VMs using vagrant snapshot push
Clean Up
Destroy unused VMs to free resources with vagrant destroy
Automation
Use provisioning scripts to automate VM setup
Real-World Use Case: LAMP Stack
Development Team Environment
Create a standardized LAMP stack for your team
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.hostname = "dev-lamp"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.provider :libvirt do |libvirt|
libvirt.memory = 4096
libvirt.cpus = 2
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2 mysql-server php libapache2-mod-php php-mysql
systemctl enable apache2
systemctl start apache2
SHELL
config.vm.synced_folder "./www", "/var/www/html"
end