Vagrant 起動エラーの解決方法

以前は正常に起動していたVagrant環境が、突然以下のエラーが発生するようになりました。様々な試行錯誤の末、Vagrantfileに特定のコードを追加することで解決できました。

エラーメッセージ

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2221 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2221
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
[default] GuestAdditions 5.2.22 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/myvagrant/Borg-web/borg/ => /vagrant
C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/io.rb:32:in `encode': "\x83\xBC" from Windows-31J to UTF-8 (Encoding::UndefinedConversionError)
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/io.rb:32:in `read_until_block'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:194:in `block in execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:192:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:192:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:22:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/helper.rb:215:in `rsync_single'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:48:in `block in enable'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:47:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:47:in `enable'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:93:in `block in call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:90:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:90:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/before_trigger.rb:23:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/after_trigger.rb:26:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folder_cleanup.rb:28:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/before_trigger.rb:23:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/after_trigger.rb:26:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/nfs/action_cleanup.rb:19:in `call'

解決策

Vagrantfileに以下の行を追加することで、問題が解決しました。

Encoding.default_external = 'UTF-8'

修正後のVagrantfileの例:

Vagrant.configure("2") do |config|
  # 仮想マシンに使用するボックスの指定
  config.vm.box = "centos/7"
  
  # エンコーディングの設定(追加)
  Encoding.default_external = 'UTF-8'
  
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In this example,
  # accessing "localhost:8080" on the host machine will forward web traffic to
  # the guest on port 80. If you'd like to access this VM from the host on a
  # specific port, you can specify that port below.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options
  # such as setting the amount of memory on the VM or activating the USB
  # passthrough. See the documentation for the provider you're using for
  # more information on available options.
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you're using for more
  # information on how to configure specific provider sections.
  # config.vm.provider "hyperv" do |hv|
  #   # Hyper-V specific configuration
  #   vmvmemory = "2048MB"
  # end
end

この設定により、ホストマシンとゲストマシン間のファイル同期時に発生するエンコーディング関連の問題が解決されます。Windows環境で日本語ファイル名を扱う場合に特に有効です。

タグ: vagrant 仮想環境 エンコーディング VirtualBox 開発環境

7月18日 22:03 投稿