Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Vagrantfile and Boxes

VM Settings: Memory, CPUs and Provider Options

Configure Vagrant virtual machines: memory, CPUs, names, hostname, GUI, VirtualBox customizations, Hyper-V and libvirt blocks, and sizing advice.

The default Vagrant VM is small: usually 1 to 2 GB of RAM and 1 or 2 CPU cores, depending on the box. That is enough for a shell, but not for a Java build, a database plus a web server, or a Kubernetes lab. In this lesson you will resize machines, give them proper names, and use provider blocks for settings that are specific to one hypervisor.

Generic settings: config.vm

A few settings work with every provider and belong directly under config.vm:

Ruby
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-24.04"
  config.vm.hostname = "dev-box"      # the guest's hostname
  config.vm.boot_timeout = 600        # seconds to wait for SSH (default 300)
  config.vm.graceful_halt_timeout = 60
  config.vm.post_up_message = "Dev box ready: http://localhost:8080"
end
  • hostname sets the guest's hostname and adds it to /etc/hosts. It is not reachable from the host by itself; it names the machine inside.
  • boot_timeout raises the patience of vagrant up. A slow disk or a first boot can exceed 5 minutes.
  • post_up_message prints a note after a successful up: good for team onboarding.
  • box_check_update can be set to false to skip online checks.

Provider blocks: memory and CPUs

Memory and CPU count are hardware, so each provider names them differently. You configure them in a block named after the provider:

Ruby
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-24.04"

  config.vm.provider "virtualbox" do |vb|
    vb.name   = "shop-dev"      # the name shown in the VirtualBox manager
    vb.memory = 4096            # MB
    vb.cpus   = 2
    vb.gui    = false           # true shows the VM console window
  end

  config.vm.provider "hyperv" do |hv|
    hv.memory = 4096
    hv.cpus   = 2
    hv.vmname = "shop-dev"
  end

  config.vm.provider "libvirt" do |lv|
    lv.memory = 4096
    lv.cpus   = 2
  end
end

Vagrant only applies the block for the provider in use, so one file can serve a team where some use VirtualBox, others Hyper-V or libvirt. The block parameter name (vb, hv, lv) is arbitrary.

Changing memory or CPUs on an existing machine requires vagrant reload. The new values are applied at the next boot, not while running.

VirtualBox customizations

The VirtualBox provider can run any VBoxManage modifyvm option through customize:

Ruby
config.vm.provider "virtualbox" do |vb|
  vb.memory = 2048
  vb.cpus = 2
  vb.customize ["modifyvm", :id, "--cpuexecutioncap", "70"]   # limit CPU use to 70%
  vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] # use host DNS settings
  vb.customize ["modifyvm", :id, "--ioapic", "on"]            # required for more than one CPU
  vb.customize ["modifyvm", :id, "--vram", "32"]
end

:id is replaced by the VM's ID. You can also pass "storagectl", "createhd" and similar to add disks, although for disk size changes Vagrant has its own helper.

Vagrant also supports growing the primary disk:

Ruby
config.vm.disk :disk, size: "40GB", primary: true

This changes the virtual disk size, but the guest's partition and filesystem still need to be expanded, usually with growpart and resize2fs in a provisioner. Newer box versions do this on their own on first boot, but check.

Sizing: how much is enough?

A good rule is to give the VM what the workload needs and no more, because the VM's memory is taken from your laptop for as long as it runs.

Workload Memory CPUs
Shell experiments, a static site, small scripts 1 GB 1
A web app with one database 2 to 4 GB 2
Java or .NET build, Docker inside the VM 4 to 8 GB 2 to 4
Kubernetes control plane (single node) 4 GB or more 2 or more
Windows guest 4 GB or more 2 or more

Never give a VM more CPUs than your host has physical cores; it slows everything down because the host must schedule all virtual CPUs. And always leave at least 4 GB to your host operating system.

Settings that depend on the host

Since the Vagrantfile is Ruby, you can size the VM from the host's hardware or an environment variable:

Ruby
require "etc"

host_cpus = Etc.nprocessors
vm_cpus   = [host_cpus / 2, 1].max

config.vm.provider "virtualbox" do |vb|
  vb.cpus   = ENV.fetch("VM_CPUS", vm_cpus).to_i
  vb.memory = ENV.fetch("VM_MEMORY", 2048).to_i
end

Here the machine gets half of the host's cores by default, but a teammate with a small laptop can run VM_CPUS=1 vagrant up. The next runnable example reproduces this arithmetic: it computes a safe VM size from a host description and refuses to over-allocate. It is a stand-in for the logic (not a real VM).

Ruby A safe VM size for any host (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Notice how the overridden request is clamped to what the host can really afford. Real Vagrant would not do this for you, so adding such a guard in the Vagrantfile prevents hard-to-diagnose freezes.

Showing the console and naming the VM

If a VM hangs at boot, set vb.gui = true and run vagrant up again; a window shows the console and any error message. For long-running labs, always set vb.name: otherwise VirtualBox shows an auto-generated name such as project_default_1731401234567_12345, and it is hard to tell your five machines apart in the VirtualBox manager.

Linked clones and speed

Ruby
config.vm.provider "virtualbox" do |vb|
  vb.linked_clone = true
end

A linked clone creates the VM from a snapshot of the box's imported disk instead of copying the whole disk. Creation is much faster and uses less disk space, which is great when you create many machines from the same box. The trade-off: the machines depend on the original imported disk, so you must not remove the base box while they exist.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do you set the amount of RAM for a VirtualBox VM?

    Memory is provider specific and goes in the provider block.
  2. 2. What must you run after changing memory in the Vagrantfile of a running machine?

    Hardware changes apply at the next boot; vagrant reload restarts the machine with the new settings.
  3. 3. What does vb.customize ["modifyvm", :id, ...] do?

    customize runs VBoxManage commands, with :id replaced by the VM's identifier.
  4. 4. Why is a linked clone faster?

    Linked clones share the base disk, which saves time and space, but depend on that base being kept.

Summary

  • Generic settings (hostname, timeouts, messages) live under config.vm; hardware settings live in provider blocks.
  • Use vb.memory, vb.cpus, vb.name and vb.gui for VirtualBox; Hyper-V and libvirt have their own blocks.
  • Size to the workload, never above the host's physical cores, and leave RAM for the host.
  • Apply changes with vagrant reload; use environment variables and Ruby to adapt to each host.
  • Linked clones make multi-machine setups cheaper.

Next: Building Custom Boxes