1. 問題となった現象
WSL (Windows Subsystem for Linux) のターミナルから、Windows用にインストールした vagrant.exe を実行すると以下のようなエラーが発生するようになりました。
$ vagrant.exe up
Bringing machine 'GuestUbuntu' up with 'virtualbox' provider...
==> GuestUbuntu: Checking if box 'laravel/homestead' is up to date...
C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.1/plugins/providers/virtualbox/driver/version_5_0.rb:388:in `split': invalid byte sequence in UTF-8 (ArgumentError)
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.1/plugins/providers/virtualbox/driver/version_5_0.rb:388:in `read_forwarded_ports'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.1/plugins/providers/virtualbox/driver/version_5_0.rb:601:in `block in read_used_ports'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.1/plugins/providers/virtualbox/driver/version_5_0.rb:593:in `each'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-2.0.1/plugins/providers/virtualbox/driver/version_5_0.rb:593:in `read_used_ports'
:
:
その時の環境は以下です。
- OS: Windows 10 Pro バージョン 1709
- WSL: Ubuntu 16.04.3 LTS
- VirtualBox 5.1.30 r118389 (Qt5.6.2) (Windows用)
- Vagrant 2.0.1 (Windows用)
「vagrant.exe status
」や「vagrant.exe halt
」では問題なく、「vagrant.exe up
」のみでこの問題は発生しており、PowerShell からの実行であれば何も問題は起きません。
エラーメッセージを読むと文字コードが原因のようです。
少し実験してみると、WSL側から Windowsのコマンドを実行する際、文字コードは UTF-8 で実行されているようでした。一方、PowerShell は CP932(Shift_JIS) で実行されます(デフォルト)。Windows用のVagrantは、このあたりの対応ができていないようです。
2. 対処法
エラーメッセージの中にあった version_5_0.rb
という Rubyファイルを修正してもよいのかもしれませんが、ちょっと面倒です。ですので今回は WSL側から実行される Windowsのコマンド実行環境側の文字コードを、素のPowerShell と同じ CP932(Shift_JIS) に変更するという対応を行います。
と言っても、以下のコマンドを実行するだけです。
$ powershell.exe -Command "chcp 932"
これにより、
$ vagrant.exe up
でエラーが発生しないようになります。
この2つのコマンドを同時に実行することもできます。
$ powershell.exe -Command "chcp 932;vagrant.exe up"
あまり問題になるようなことはなさそうですが、Windowsのコマンドが実行される環境側の文字コードを元に戻しておきたい場合は、以下を実行します。
$ powershell.exe -Command "chcp 65001"
これで UTF-8 に戻ります。
3. 補足
Vagrant and Windows Subsystem for Linux – Vagrant by HashiCorp という情報もありましたが、上手くいきませんでした。