LoginSignup
3
3

More than 5 years have passed since last update.

Homesteadで2つ目の開発環境を作ろうとしたら「仮想マシンが既に存在している」というエラーに直面した件

Posted at

副業でやってるプロダクト以外に、個人で開発するためにもう1つLaravelの開発環境を作ろうとした時の話

【Laravel超入門】開発環境の構築(VirtualBox + Vagrant + Homestead + Composer)

前回もこの記事を参考に環境構築したので、今回も同じ流れで実施。

発生したエラー

Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...
==> homestead-7: Matching MAC address for NAT networking...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
A VirtualBox machine with the name 'homestead-7' already exists.
Please use another name or delete the machine with the existing
name, and try again.

vagrant up をしようとした時に発生。
「homestead-7っていう仮想マシンは既に存在してる」という内容。
homestead-7 というのはHomestead落としてきた時のデフォルトなのかな。
boxを指定しているところを探して修正すれば良いと見込んで調査開始。

Vagrantfileを見てみる

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path(File.dirname(__FILE__))

homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

Vagrant.require_version '>= 1.9.0'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    if File.exist? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
        config.vm.provision "shell" do |s|
            s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases"
        end
    end

    if File.exist? homesteadYamlPath then
        settings = YAML::load(File.read(homesteadYamlPath))
    elsif File.exist? homesteadJsonPath then
        settings = JSON::parse(File.read(homesteadJsonPath))
    else
        abort "Homestead settings file not found in #{confDir}"
    end

    Homestead.configure(config, settings)

    if File.exist? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true
    end

    if Vagrant.has_plugin?('vagrant-hostsupdater')
        config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
    elsif Vagrant.has_plugin?('vagrant-hostmanager')
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
    end
end

とりあえずVagrantfileを見てみる。
ここにはboxをしているしている箇所はないものの、下記のように色んなファイルを読み込んでいるようなので、それぞれ調査する。

homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

/scripts/homestead.rb の中に原因を発見

/scripts/homestead.rb
# Configure The Box
config.vm.define settings["name"] ||= "homestead-7"
config.vm.box = settings["box"] ||= "laravel/homestead"
config.vm.box_version = settings["version"] ||= ">= 5.2.0"
config.vm.hostname = settings["hostname"] ||= "homestead"

/scripts/homestead.rb の中にboxの設定を発見!
config.vm.define settings["name"] ||= "homestead-7" のところをサービス名に書き換えることで、ようやく vagrant up が通るようになった。
ただし、ポートなどのネットワーク関係が前に構築した環境と被っているので同じタイミングで使おうと思ったらもうちょっと修正が必要そう。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3