LoginSignup
38
29

More than 3 years have passed since last update.

Windows10 WSL から Vagrant を起動する

Last updated at Posted at 2019-04-22

Windows10 WSL から Vagrant を起動する

Virtualbox Install

https://www.virtualbox.org/
ここからWindows10上にインストールします。※5.2系で確認

Ubuntu18.04LTS(WSL)をインストール

Microsoft Store でUbuntu18.04LTS をインストール
初期設定でユーザ名とパスワードが聞かれます。
任意の設定をしてください。
初回にRootになれるかの確認とupdateを行っておきましょう

$ sudo su -
[sudo] password for ubuntu:
apt-get update
apt-get upgrade

Vagrant を WSLからインストールします。

https://www.vagrantup.com/downloads.html
上記のURLから Debian 64bit のリンクをCopyしてWSLから wget で
pkgを取得後インストールします。

wget https://releases.hashicorp.com/vagrant/2.2.4/vagrant_2.2.4_x86_64.deb
dpkg -i vagrant_2.2.4_x86_64.deb

https://www.vagrantup.com/docs/other/wsl.html
公式の指示に従って、環境変数を追加します。

vi ~/.bashrc

...
...末尾に追記
export VAGRANT_WSL_WINDOWS_ACCESS_USER= [ウインドウズのユーザ名]
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
#/mnt/c/[任意のDir名]でもOK
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/vagrant"
export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox"
export PATH="$PATH:/mnt/c/Windows/System32/"
export PATH="$PATH:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/"

Vagrant initとVagrantfile 作成

Vagrant用dir 作成後 vagrant init を実行します。

mkdir /mnt/c/vagrant
cd /mnt/c/vagrant
vagrant init

VagrantFile を以下のようにする。
IP等は任意です。

vi Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.network "private_network", ip: "192.168.33.10"
  #config.vm.synced_folder ".", "/vagrant",disabled: true
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
  end
end

vagrant up を実行しマシンが起動することを確認します。

vagrant up
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The host path of the shared folder is not supported from WSL. Host
path of the shared folder must be located on a file system with
DrvFs type. Host path: .

上記のエラーが出る場合は以下の設定を有効化し再度実行してください。
VagrantFile

config.vm.synced_folder ".", "/vagrant",disabled: true

共有フォルダ(/vagrant)は使えませんが、マシンは起動します。

The private key to connect to this box via SSH has invalid permissions
set on it. The permissions of the private key should be set to 0600, otherwise SSH will
ignore the key. Vagrant tried to do this automatically for you but failed. 

ssh key権限エラーメッセージが発生する場合
keyの置き場所がWindows上で600にできないっぽい。
VagrantFileがあるDirに以下のshellを置いて
new_key_set.sh

#!/bin/bash
SCRIPT_DIR=$(cd $(dirname $0);pwd)
prefix=$(echo ${SCRIPT_DIR}|grep -o -E "[^\/]*$")
mv ${SCRIPT_DIR}/.vagrant/machines/default/virtualbox/private_key /mnt/private_key_${prefix}
cd ${SCRIPT_DIR}/.vagrant/machines/default/virtualbox/
ln -s /mnt/private_key_${prefix} private_key
chmod 600 private_key
bash new_key_set.sh
vagarnt reload 

で問題なくなるはず。
対応としては/mnt/下にprivate_keyを置いて600にしシンボリックリンクで見るようにしただけです。

参考
38
29
16

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
38
29