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 existsView on GitHub (pinned to 35f3160f4a)
Solutions
- Keep the existing file: open and edit it instead of re-initializing.
- Overwrite intentionally: `vagrant init --force <box>` (or `vagrant init -f <box>`).
- 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
- Check for ./Vagrantfile before scripting vagrant init.
- Use --force only in disposable environments (CI containers, temp dirs).
- Keep the Vagrantfile in version control so any overwrite is recoverable.
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
- There are errors in the configuration of this machine. Pleas
- The specified Vagrantfile to clone from was not found. Pleas
- The specified file '%{filename}' to save the package as alre
- There was an error loading a Vagrantfile. The file being loa
- There is a syntax error in the following Vagrantfile. The sy
AI-assisted analysis of hashicorp/vagrant@35f3160f4a (2026-08-21).
Data as JSON: /api/errors/1955fa07c0059edb.
Report an issue: GitHub.