LoginSignup
1
3

More than 1 year has passed since last update.

Vagrant + VirtualBox on WSL2, Windows 11 pro

Last updated at Posted at 2022-11-04

主に自分用 Windows 開発環境構築手順メモ

まとめ

環境

  • Windows 11 Pro
  • WSL_Ubuntu 20.04 LT
    • Windows のユーザと Ubuntu のユーザは同じ名前 (以下、xxxx)
  • Vagrant 2.2.19
  • VirtualBox 6.1.34

注意

  • Vagrant や VirtualBox のバージョンによっては以下の手順は動作しないことがありえる
  • VSCode による接続とかは書いてない

構築

  1. VirtualBox 6.1 系(.30 以上のバージョン) をインストール

  2. [コンパネ] -> [プログラムと機能] -> [Windows の機能の有効化または無効化]から以下を有効化 & Windows 再起動

    • Linux 用 Windows サブシステム
    • Windows ハイパーバイザー プラットフォーム
    • 仮想マシンプラットフォーム
  3. デフォルトの WSL のバージョンを 2 にする。PowerShell を管理者実行

    PS C:\WINDOWS\system32> wsl --set-default-version 2
    
  4. Microsoft Store から Ubuntu 20.04 LTS をインストール

  5. Ubuntu ユーザの home を /mnt/c/Users/xxxx/wsl_home に変更

    ~# vi /etc/passwd
    ()
    xxxx:x:1000:1000:"",,,:/mnt/c/Users/xxxx/wsl_home:/bin/bash
    
  6. c ドライブを認識させるために

    • wsl 上で /etc/wsl.conf を作成

      ~# vi /etc/wsl.conf
      [automount]
      enabled = true
      root = /mnt
      options = "metadata,umask=22,fmask=11"
      
    • wsl 上で /etc/fstab を編集。以下を追記 (いらないかも)

      C: /mnt/c drvfs metadata,notime,gid=1000,uid=1000,defaults 0 0

  7. Windows 再起動

  8. Vagrant のサイトから Debian用 2.2.19 パッケージを入手

    • vagrant_2.2.19_x86_64.deb
  9. C:\Users\xxxx\wsl_home に Vagrant パッケージを配置

  10. wsl 上で Vagrant をインストール

    ~$ dpkg -i vagrant_2.2.19_x86_64.deb
    
  11. WSL コンソール起動時に環境変数を読み込ませるために .bash_profile を作成

    ~$ vi ~/.bash_profile
    if [[ -f ~/.bashrc ]]; then
      . ~/.bashrc
    fi
    
    # Proxy if necessary
    export HTTP_PROXY=http://proxy.example
    export HTTPS_PROXY=http://proxy.example
    export no_proxy=127.0.0.1
    
    # Vagrant
    export VAGRANT_WSL_WINDOWS_ACCESS_USER="xxxx" # wslのユーザ名
    export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
    export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/Users/xxxx/wsl_home"
    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 で仮想マシン作成

WSL 2 用のプラグイン

$ vagrant plugin install virtualbox_WSL2

共有ファイル使う

プラグインのインストール

$ vagrant plugin install vagrant-vbguest

Vagrantfileに記述

  config.vm.synced_folder "./files", "/home/vagrant/share", type: "virtualbox"

boxのバージョンが低いとエラーになるときがある

config.vbguest.installer_options = { allow_kernel_upgrade: true }

VMにPROXY設定

プラグインのインストール

$ vagrant plugin install vagrant-proxyconf
if Vagrant.has_plugin?("vagrant-proxyconf")
  config.proxy.http     = "http://proxy.example"
  config.proxy.https    = "http://proxy.example"
  config.proxy.no_proxy = "localhost,127.0.0.1"
end

Rockylinux 8 の Vagrantfile

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

Vagrant.configure("2") do |config|

  config.vm.box = "rockylinux/8"

  config.vbguest.installer_options = { allow_kernel_upgrade: true }

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

  # おまじない
  config.vm.boot_timeout = 500

  config.vm.define "rocky8-test" do |dev|

    dev.vm.synced_folder "./files", "/root/share", create:true, owner: "root", group: "root", type: "virtualbox"
    dev.vm.network "private_network", ip: "192.168.100.100", virtualbox__intnet: "dev-network"
    # VM 内で作った HTML 確認する時用
    dev.vm.network "forwarded_port", guest:80, host:8080
    dev.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.name = "rocky8-test"
      vb.memory = "2048"
    end
  end
end
1
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
1
3