Back to Deployment Guides

    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.

    1

    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
    2

    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:

    Check virtualization extensions
    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:

    Check KVM modules
    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.

    3

    Install KVM and Dependencies

    Install KVM on Ubuntu/Debian
    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:

    Add user to groups
    sudo usermod -aG libvirt $USER
    sudo usermod -aG kvm $USER

    Enable and start libvirtd:

    Start libvirt service
    sudo systemctl enable libvirtd
    sudo systemctl start libvirtd

    Log out and back in for group membership changes to take effect.

    4

    Install Vagrant

    Install Vagrant on Ubuntu/Debian
    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:

    Verify Vagrant version
    vagrant --version
    5

    Install Vagrant Libvirt Plugin

    Install libvirt plugin
    vagrant plugin install vagrant-libvirt

    If you encounter dependency issues, install the development packages:

    Install development dependencies
    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.

    3

    Install VirtualBox (Alternative)

    Install VirtualBox on Ubuntu/Debian
    sudo apt update
    sudo apt install -y virtualbox virtualbox-ext-pack

    Add your user to the vboxusers group:

    Add user to 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

    6

    Initialize a Vagrant Project

    Create project directory
    mkdir ~/vagrant-test
    cd ~/vagrant-test
    vagrant init generic/ubuntu2204

    This creates a Vagrantfile in the current directory with default settings.

    7

    Configure the Provider

    Edit the Vagrantfile to configure the libvirt provider:

    Vagrantfile
    Vagrant.configure("2") do |config|
      config.vm.box = "generic/ubuntu2204"
    
      config.vm.provider :libvirt do |libvirt|
        libvirt.memory = 2048
        libvirt.cpus = 2
      end
    end

    For VirtualBox, use config.vm.provider :virtualbox instead.

    8

    Launch the Virtual Machine

    Start the VM
    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:

    SSH into the VM
    vagrant ssh

    Advanced Vagrantfile Configuration

    Multi-Machine Environment

    Create multiple VMs in a single Vagrantfile for testing distributed systems

    Multi-machine Vagrantfile
    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
    end

    Provisioning with Shell Scripts

    Automatically configure VMs during creation

    Provisioned Vagrantfile
    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
    end

    Synced Folders

    Share directories between your VPS and the Vagrant VM

    Synced folder configuration
    config.vm.synced_folder "./data", "/vagrant_data"

    Essential Vagrant Commands

    CommandDescription
    vagrant upStart and provision the VM
    vagrant haltGracefully shut down the VM
    vagrant destroyDelete the VM and all its resources
    vagrant reloadRestart the VM and reload Vagrantfile configuration
    vagrant statusCheck the current state of VMs
    vagrant sshConnect to the VM via SSH
    vagrant suspendSave VM state and stop (faster than halt)
    vagrant resumeResume a suspended VM
    vagrant snapshot pushCreate a snapshot of the current state

    Performance Optimization

    Resource Allocation

    Be conservative with memory and CPU allocation (60-70% of VPS resources max)

    Optimized resource configuration
    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

    Virtio disk configuration
    config.vm.provider :libvirt do |libvirt|
      libvirt.disk_bus = 'virtio'
    end

    Box Management

    Regularly clean up unused boxes to free disk space

    Clean up boxes
    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

    Restart libvirtd
    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

    Check libvirt network
    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.

    Monitor memory usage
    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

    LAMP Vagrantfile
    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

    Additional Resources