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

`Vagrantfile` already exists in this directory. Remove it be

Error message

`Vagrantfile` already exists in this directory. Remove it before running `vagrant init`.

What it means

`vagrant init` refuses to clobber an existing Vagrantfile. It computes the output path (default ./Vagrantfile, overridable with --output), deletes it only when --force was given, then raises Vagrant::Errors::VagrantfileExistsError if the path still exists. It is a safety guard, not a corruption problem.

Source

Thrown at plugins/commands/init/command.rb:59

          o.on("--output FILE", String,
               "Output path for the box. '-' for stdout") do |output|
            options[:output] = output
          end

          o.on("--template FILE", String, "Path to custom Vagrantfile template") do |template|
            options[:template] = template
          end
        end

        # Parse the options
        argv = parse_options(opts)
        return if !argv

        save_path = nil
        if options[:output] != "-"
          save_path = Pathname.new(options[:output]).expand_path(@env.cwd)
          save_path.delete if save_path.exist? && options[:force]
          raise Vagrant::Errors::VagrantfileExistsError if save_path.exist?
        end

        # Determine the template and template root to use
        template_root = ""
        if options[:template].nil?
          options[:template] = "Vagrantfile"

          if options[:minimal]
            options[:template] = "Vagrantfile.min"
          end

          template_root = ::Vagrant.source_root.join("templates/commands/init")
        end

        # Strip the .erb extension off the template if the user passes it in
        options[:template] = options[:template].chomp(".erb")

        # Make sure the template actually exists

View on GitHub (pinned to 35f3160f4a)

Solutions

  1. Keep the existing file: open and edit it instead of re-initializing.
  2. Overwrite intentionally: `vagrant init --force <box>` (or `vagrant init -f <box>`).
  3. Write elsewhere: `vagrant init --output Vagrantfile.new <box>` and merge manually.

Example fix

# before
vagrant init ubuntu/jammy64   # Vagrantfile already present
# after
vagrant init --force ubuntu/jammy64
Defensive patterns

Strategy: validation

Validate before calling

path = 'Vagrantfile'
if File.exist?(path)
  warn "#{path} exists; pass --force to overwrite or edit it instead"
  exit 1
end
system('vagrant', 'init', 'ubuntu/jammy64')

Try / catch

begin
  Vagrant::Environment.new.cli(['init', 'ubuntu/jammy64'])
rescue Vagrant::Errors::VagrantfileExistsError => e
  warn e.message # decide: --force, --output, or edit the existing file
  exit 1
end

Prevention

When it happens

Trigger: Running `vagrant init` a second time in a directory that already contains a Vagrantfile; passing `--output somefile` that already exists without `--force`; leftover Vagrantfile from a previous or aborted init run.

Common situations: Re-initializing a project to switch the base box; scaffolding tools or repo templates that pre-create a Vagrantfile; CI jobs that re-run init in a dirty workspace without cleaning.

Related errors


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