Ubuntu に Vagrant をインストール

Ubuntu 16.04 上で Vagrant を動かしたメモ.よくわからないエラーが出てハマった.

VirtualBox のインストール

最新版の deb をダウンロードしてきて,インストール.

Oracle VM VirtualBox - Downloads | Oracle Technology Network | Oracle

Vagrant のインストール

最新版の deb をダウンロードしてきて,インストール.

Vagrant by HashiCorp

仮想環境の作成

今回はとりあえず hashicorp/precise64 という公式の box を使ってみる.

% vagrant box add hashicorp/precise64

途中でプロバイダを聞かれるので,ここでは 2 (virtualbox) を選択.box がダウンロードされるまで数分間待つ.

次のコマンドで,ちゃんと box が追加できたか確かめる.

% vagrant box list
hashicorp/precise64 (virtualbox, 1.1.0)

そしたら,ディレクトリを作って,仮想環境を作成する.

% mkdir box1
% cd box1
% vagrant init hashicorp/precise64

そうすると Vagrantfile というファイルができるので,それをいじるといろんな設定ができるらしい.

仮想環境の立ち上げ

仮想環境を立ち上げてみる.

% vagrant up
VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

エラー発生.エラーメッセージにしたがって,VBoxManage --version を叩いてみる.

% VBoxManage --version
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (4.4.0-112-generic) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
5.1.26r117224

今度は /sbin/vboxconfig を叩いてみる.

% sudo /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: failed: modprobe vboxdrv failed. Please use 'dmesg' to find out why.

どうも一筋縄では解決できなさそう.調べたら次のページに解決策が載っていた.

askubuntu.com

このページの内容を全部やって vagrant up したら,今度は別のエラーが出るように.

% vagrant up 
[...]
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "8b358478-0e96-4b23-90bf-ca7a63c05053", "--type", "headless"]

Stderr: VBoxManage: error: VT-x is disabled in the BIOS for all CPU modes (VERR_VMX_MSR_ALL_VMX_DISABLED)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

エラーメッセージの啓示にしたがって BIOS の設定をいじった.これで,無事に立ち上がった.

% vagrant up
[...]

あとは ssh で接続するだけ.

% vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Sat Feb  3 05:42:45 2018 from x.x.x.x