takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Vagrant で CentOS7 環境を構築

VagrantVirtualBox に CentOS7 の VM を構築したときの手順メモ

検証環境

Windows10 Home Edition
VirtualBox 6.0.10
Vagrant 2.2.5

VirtualBox のインストール

こちらのサイトからパッケージををダウンロード、インストールします。 www.virtualbox.org

Vagrant のインストール

こちらのサイトからパッケージををダウンロード、インストールします。 www.vagrantup.com

動作確認

C:\>vagrant -v
Vagrant 2.2.5

環境変数 VAGRANT_HOME の設定

Vagrant はデフォルトで Box ファイルを C:\Users 以下に保存しますが、Cドライブの容量が少ない場合は移動させる必要があります。
Box ファイルの保存先は環境変数 VAGRANT_HOME で指定します。

f:id:takaya030:20190915183758p:plain

VirtualBox仮想マシンフォルダ変更

Box ファイル同様、Cドライブの容量が少ない場合は移動させる必要があります。
フォルダのパス変更は VirtualBox マネージャーの ファイル > 環境設定 をクリックして デフォルトの仮想マシンフォルダー の項目で設定します。

f:id:takaya030:20190916122322p:plain

Vagrant Box ファイル追加

vagrant box add コマンドで CentOS7 の Box ファイルを追加します。
provider は 3) virtualbox を選択します。

C:\>vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3

vagrant box list コマンドで Box ファイルが追加されているか確認可能です。

C:\>vagrant box list
centos/7 (virtualbox, 1905.1)

仮想マシン作成

作業用ディレクトリを作成して vagant init を実行します。

D:\>mkdir vagrant\centos7

D:\>cd vagrant\centos7

D:\vagrant\centos7>vagrant init centos/7

作成された Vagrantfile を下記の内容に変更します。
仮想マシンのメモリを 2014MB にしています。

--- a/Vagrantfile     Mon Sep 16 18:18:43 2019
+++ b/Vagrantfile     Mon Sep 16 18:35:57 2019
@@ -49,13 +49,13 @@
   # backing providers for Vagrant. These expose provider-specific options.
   # Example for VirtualBox:
   #
-  # config.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.memory = "1024"
-  # end
+  config.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.memory = "2048"
+  end
   #
   # View the documentation for the provider you are using for more
   # information on available options.

vagrant up コマンドで仮想マシンを作成します。

D:\vagrant\centos7>vagrant up

仮想マシンへログイン

仮想マシン起動後、vagrant ssh コマンドでログインできます。

D:\vagrant\centos7>vagrant ssh

exit でログアウトします。

[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

仮想マシンの停止

vagrant halt仮想マシンを停止します。

D:\vagrant\centos7>vagrant halt

参考サイト

memories.zal.jp qiita.com qiita.com