hashicorp/vagrant · error · Vagrant::Errors::VMNameExists

A VirtualBox machine with the name '%{name}' already exists.

Error message

A VirtualBox machine with the name '%{name}' already exists.
Please use another name or delete the machine with the existing
name, and try again.

What it means

The SetName action derives a VM name (explicit config.vm.name, or '<folder>_<machine>_<ms>_<rand>' by default) and checks it against read_vms. If the name maps to a different VM UUID than the current machine, it raises VMNameExists — VirtualBox forbids two VMs with the same name in its registry.

Source

Thrown at plugins/providers/virtualbox/action/set_name.rb:37

          sentinel = env[:machine].data_dir.join("action_set_name")
          if !name && sentinel.file?
            @logger.info("Default name was already set before, not doing it again.")
            return @app.call(env)
          end

          # If no name was manually set, then use a default
          if !name
            prefix = "#{env[:root_path].basename.to_s}_#{env[:machine].name}"
            prefix.gsub!(/[^-a-z0-9_]/i, "")

            # milliseconds + random number suffix to allow for simultaneous
            # `vagrant up` of the same box in different dirs
            name = prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
          end

          # Verify the name is not taken
          vms = env[:machine].provider.driver.read_vms
          raise Vagrant::Errors::VMNameExists, name: name if \
            vms.key?(name) && vms[name] != env[:machine].id

          if vms.key?(name)
            @logger.info("Not setting the name because our name is already set.")
          else
            env[:ui].info(I18n.t(
              "vagrant.actions.vm.set_name.setting_name", name: name))
            env[:machine].provider.driver.set_name(name)
          end

          # Create the sentinel
          sentinel.open("w") do |f|
            f.write(Time.now.to_i.to_s)
          end

          @app.call(env)
        end
      end

View on GitHub (pinned to 35f3160f4a)

Solutions

  1. Delete the existing VM: `VBoxManage unregistervm '<name>' --delete` (or remove it in the VirtualBox GUI with 'Delete all files')
  2. Or set a unique name: `config.vm.name = 'dev-2'` / rename your project directory when using default names
  3. If the existing VM is actually this machine (stale .vagrant id), run `vagrant destroy -f` or remove the .vagrant/machines data so Vagrant re-adopts it
  4. Check `VBoxManage list vms` to see the UUID currently holding the name before deleting anything valuable

Example fix

# before: two projects both do
config.vm.name = 'devbox'

# after (option A - unique names):
config.vm.name = "devbox-#{File.basename(Dir.pwd)}"

# after (option B - clear the collision on the host):
# VBoxManage unregistervm "devbox" --delete && vagrant up
Defensive patterns

Strategy: validation

Validate before calling

def vbox_name_free?(name, current_uuid = nil)
  `VBoxManage list vms`.lines.none? do |l|
    l =~ /^"#{Regexp.escape(name)}" \{(.+)\}$/ && Regexp.last_match(1) != current_uuid
  end
end
abort 'VM name already registered' unless vbox_name_free?('devbox')

Try / catch

begin
  env.cli('up')
rescue Vagrant::Errors::VMNameExists => e
  warn "Unregister '#{e.extra_data[:name]}' (VBoxManage unregistervm --delete) or set config.vm.name"
end

Prevention

When it happens

Trigger: `vagrant up` when a VirtualBox VM with that name already exists under another UUID: a leftover VM from a crashed/destroyed environment, a manually created VM with the same config.vm.name, or a copied project directory reusing an explicit name.

Common situations: Explicit `config.vm.name = 'dev'` shared by two checkouts; a previous `vagrant destroy` that failed mid-way leaving the VM registered; VMs created by hand or by VirtualBox GUI with colliding names; restoring a project from backup while the old VM still exists.

Related errors


AI-assisted analysis of hashicorp/vagrant@35f3160f4a (2026-08-21). Data as JSON: /api/errors/a85b6d7e6112895d. Report an issue: GitHub.