CLOVER🍀

That was when it all began.

Ubuntu Linux 18.04 LTSに、VagrantをインストールしてUbuntu Linux 18.04 LTSのboxを起動するまで

最近、ちょっとした環境の用意はDockerで行っているのですが、それでもどうしても仮想マシン自体が欲しくなる時があり、
かといってがっつりと仮想化ソフトウェアを使う感じでもない(あくまでインストール手順の確認とか)ので、
簡単に仮想マシンを起動できて破棄できる、Vagrantを利用することにしました。

Vagrant by HashiCorp

環境は、Ubuntu Linux 18.04 LTS。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:    18.04
Codename:   bionic

Vagrantを使うための仮想化ソフトウェアとしては、VirtualBoxを利用します。

Installing Vagrant - Vagrant by HashiCorp

Oracle VM VirtualBox

VirtualBoxのインストール。

$ wget https://download.virtualbox.org/virtualbox/5.2.22/virtualbox-5.2_5.2.22-126460~Ubuntu~bionic_amd64.deb
$ sudo dpkg -i virtualbox-5.2_5.2.22-126460~Ubuntu~bionic_amd64.deb

自分の環境では、debファイルのインストール時に依存関係でエラーになったので、以下のパッケージを追加で
インストールする必要がありました。

$ sudo apt install libqt5opengl5 libqt5printsupport5 libqt5x11extras5

設定実行。

$ sudo /sbin/vboxconfig

こんなことをしているのは、VirtualBoxを使う時にエラーが出てたから…。

$ VBoxManage --version
WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (4.15.0-39-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.2.22r126460

この時、さらにこのあたりもインストールすることに…。

$ sudo apt install gcc make perl

で、VirtualBoxがセットアップできたので、次はVagrantのインストール。

Vagrantインストーラーは、こちらのダウンロードページから取得。

Download - Vagrant by HashiCorp

インストール。

$ wget https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_x86_64.deb
$ sudo dpkg -i vagrant_2.2.2_x86_64.deb

Ubuntu Linux 18.04 LTSをboxに使って、セットアップ。

$ vagrant init ubuntu/bionic64

Project Setup - Getting Started - Vagrant by HashiCorp

Boxes - Getting Started - Vagrant by HashiCorp

ubuntu - Vagrant Cloud

Vagrant box ubuntu/bionic64 - Vagrant Cloud

とりあえず、起動。

$ vagrant up

数分待つと起動するので、SSHで接続。

$ vagrant ssh
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-39-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 System information disabled due to load higher than 2.0


  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


vagrant@ubuntu-bionic:~$

いったん、SSHを抜けてインストールされているboxの確認。

$ vagrant box list
ubuntu/bionic64 (virtualbox, 20181129.0.0)

停止。

$ vagrant halt

破棄。

$ vagrant destroy

とりあえず、ここまではOKなので少しVagrantfileをカスタマイズしておきましょう。

メモリを増やして、プロビジョニング時に「apt-get update」。

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update -y
    apt-get upgrade -y
    apt-get autoremove -y
  SHELL

Configuration - VirtualBox Provider - Vagrant by HashiCorp

Provisioning - Getting Started - Vagrant by HashiCorp

Shell Scripts - Provisioning - Vagrant by HashiCorp

簡単な確認環境としては、これでOKでしょう。

あと、たまに仮想マシンをブリッジ接続にしたい時もあるので、そちらも合わせて。

Public Networks - Networking - Vagrant by HashiCorp

ネットワーク設定で、「public_network」を使用するように設定します。

  config.vm.network "public_network"

ネットワークインターフェースを複数持っている場合は、次のようにインターフェースを明示します。

  config.vm.network "public_network", bridge: "enp14s0"

でないと、起動時に対話形式で質問されることになります。

==> default: Available bridged network interfaces:
1) enp14s0
2) docker0
...

ブリッジ接続で起動した仮想マシンIPアドレスを知りたい場合は、「ip a」ででも確認しましょう。

$ vagrant ssh -c 'ip a'