LinuxにVagrantとVirtualBoxをインストールして、実際に仮想マシンを立ち上げます

VirtualBoxというとWindowsにインストールしてマウス操作で仮想マシンを構築するのが一般的だと思います。本日はLinuxのcuiな環境にVagrantとVirtualBoxをインストールします。インストール後はWindowsで比較的容易に仮想マシンを立ち上げることができますが、Linuxではコマンドによる操作となります。

VirtualBoxだけでもコマンドによる仮想マシンの制御は可能ですが、いかんせんコマンドが複雑で戸惑うこともあります。Vagrantはこの困難なコマンドを楽にしてくれる便利なツールですのでセットでインストールします。

https://www.vagrantup.com/

まずはVagrantからインストールします。上記の公式ページにインストール方法が記載してあります。ubuntu22.04にインストールします。

curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
apt-get update && apt-get install vagrant

次はVirtualBoxをインストールします。

https://www.virtualbox.org/

Linux distributionsのリンクを探してクリックします。インストールするOSの上で右クリックしてリンクを取得します。

SSH接続した黒い画面でコマンド入力をします。

wget コピーして取得したURL
dpkg -i ダウンロードしたファイル名

パッケージのインストールでエラーが発生しました。

root@ubuntu2204:/tmp# dpkg -i virtualbox-6.1_6.1.34-150636.1~Ubuntu~jammy_amd64.deb
以前に未選択のパッケージ virtualbox-6.1 を選択しています。
(データベースを読み込んでいます … 現在 138722 個のファイルとディレクトリがインストールされています。)
virtualbox-6.1_6.1.34-150636.1~Ubuntu~jammy_amd64.deb を展開する準備をしています …
virtualbox-6.1 (6.1.34-150636.1~Ubuntu~jammy) を展開しています…
dpkg: 依存関係の問題により virtualbox-6.1 の設定ができません:
virtualbox-6.1 は以下に依存 (depends) します: libqt5core5a (>= 5.15.1) …しか し:
パッケージ libqt5core5a はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libqt5gui5 (>= 5.14.1) | libqt5gui5-gles (>= 5.14.1) …しかし:
パッケージ libqt5gui5 はまだインストールされていません。
パッケージ libqt5gui5-gles はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libqt5opengl5 (>= 5.0.2) …しか し:
パッケージ libqt5opengl5 はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libqt5printsupport5 (>= 5.0.2) …しかし:
パッケージ libqt5printsupport5 はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libqt5widgets5 (>= 5.15.1) …し かし:
パッケージ libqt5widgets5 はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libqt5x11extras5 (>= 5.6.0) …しかし:
パッケージ libqt5x11extras5 はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libsdl1.2debian (>= 1.2.11) …しかし:
パッケージ libsdl1.2debian はまだインストールされていません。
virtualbox-6.1 は以下に依存 (depends) します: libxt6 …しかし:
パッケージ libxt6 はまだインストールされていません。

dpkg: パッケージ virtualbox-6.1 の処理中にエラーが発生しました (–install):
依存関係の問題 – 設定を見送ります
hicolor-icon-theme (0.17-2) のトリガを処理しています …
shared-mime-info (2.1-2) のトリガを処理しています …
処理中にエラーが発生しました:
virtualbox-6.1

インストールされていないと表示されたツールをインストールします。

apt install libqt5core5a libqt5gui5-gles libqt5opengl5 libqt5printsupport5 libqt5widgets5 libqt5x11extras5 libsdl1.2debian libxt6

エラーが出てインストールできません。指示通りのコマンド入力をします。

E: 未解決の依存関係です。’apt –fix-broken install’ を実行してみてください (または解法を明示してください)。

apt --fix-broken install

次のような問題が発生しましたが、続けてVirtualBoxのインストールをします。

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

dpkg -i virtualbox-6.1_6.1.34-150636.1~Ubuntu~jammy_amd64.deb

同じ問題が発生しましたがバージョンの確認をします。

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

VBoxManage -v

WARNING: The vboxdrv kernel module is not loaded. Either there is no module
available for the current kernel (5.15.0-33-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.
6.1.34r150636

警告が出たので指示通りのコマンドを入力します。

/sbin/vboxconfig

root@ubuntu2204:/tmp# /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.

There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root. If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system’s documentation for more information.

エラーが発生しました。ここでも表示されたことに従い、不足しているモジュールをインストールします。

apt install gcc make perl

インストールが完了した後で、先ほどエラーが出たコマンドを再度入力します。今度はうまくいきました。

/sbin/vboxconfig
VBoxManage -v

VirtualBox拡張パックのインストールもします。同様にして拡張パックのリンクを取得してファイルをダウンロードします。VirtualBox専用のコマンドを使用して拡張パックをインストールします。

wget コピーしたURL
VBoxManage extpack install ダウンロードしたファイル名

せっかくなのでなにか試してみましょう。

https://app.vagrantup.com/boxes/search

一から構築するのは面倒なので、予め用意されたものを検索して使用することができます。Vagrant Boxesから好きなものを探すことができます。ここではLaravelの環境をVirtualBoxに作成します。Laravelの環境がすでに構築してあるので楽です。

わかりやすいようにWindowsにインストールしたVagrantとVirtualBoxで確認します。まずは適当なフォルダを作成します。簡単のためCドライブ直下にtestというディレクトリを作成しました。次にコマンドプロンプトを起動して作成したフォルダに切り替えます。

cd
cd test

このフォルダにいる状態でVagrant Boxesにあったコマンドを入力します。

vagrant init laravel/homestead

するとコマンドを実行したフォルダに「Vagrantfile」が作成されているはずです。少しこのファイルを編集することにします。メモリサイズを4GBにして保存します。他にも色々設定できますが、メモリサイズだけ変更することにしました。早速仮想マシンを立ち上げてみましょう。

vagrant up

VirtualBoxとコマンドプロンプトを同時に確認すると、コマンド入力したことがVirtualBoxに反映されていることが確認できます。SSH接続を他のソフトを使用して行うこともできますが、Vagrantのコマンドでも仮想マシンに入ることができます。

vagrant ssh

他にも便利なVagrantのコマンドが用意されていますし、設定ファイルがあるので比較的容易に仮想マシンを作成できると思います。

教育

Posted by admin