構築した環境
OS - Software | version |
---|---|
macOS High Sierra | 10.13.6 |
Homebrew | 1.7.2 |
Homebrew-Cask | 1.7.2 |
virtualbox | 5.2.18 |
vagtant | 2.1.2 |
Homebrewのインストールの前に
以下をインストールします。
-
Xcode
Homebrewを動かしたりするのに使うのでインストールします。
XcodeはApp Storeから入手できます。 -
Command Line Tools for Xcode
$ xcode-select --install
Homebrewのインストール
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
最新のコマンドは、Homebrewのサイトにあります。
brewに問題がないか確認
$ brew doctor
Your system is ready to brew.
「Your system is ready to brew.」と出れば問題なしです。
brewを最新に更新
$ brew update
update後に再度問題ないか確認
$ brew doctor
Your system is ready to brew.
※警告が出る場合もありますが、基本的には対処法も吐き出してくれます。
VirtualBoxのインストール
$ rew cask install virtualbox
※セキュリティとプライバシーで許可を求められるので、許可します。
自分の場合は、terminalを開いていると許可ボタンが押せなかったので、一旦閉じてから許可をしました。
CentOS7のboxを検索
Vagrant Cloudで追加するboxを見つけましょう。
boxの追加
今回は、CentOS7を入れたいので、 centOS/7
を使います。
$ 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 ←ここで
==> box: Adding box 'centos/7' (v1804.02) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1804.02/providers/virtualbox.box
box: Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1804.02) for 'virtualbox'!
追加したbox確認
$ vagrant box list
centos/7 (virtualbox, 1804.02)
Vagtantfile作成
$ # vagrantfileを作成するディレクトリ作成
$ mkdir VagCentOS7
$ # 作成したディレクトリへ移動
$ cd VagCentOS7
$ # vagrantfileを作成
$ vagrant init centOS/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
ls -l でvagrantfileが作成されていることが確認できます。
仮想マシンの起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centOS/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centOS/7' is up to date...
==> default: Setting the name of the VM: VagCentOS7_default_1535016528037_62346
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/zamakei/VagCentOS7/ => /vagrant
仮想マシンの起動確認
$ vagrant status
Current machine states:
default running (virtualbox)
runningが表示されていればOK
仮想マシンに接続
$ vagrant ssh
仮想マシンの停止
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
仮想マシンの停止確認
$ vagrant status
Current machine states:
default poweroff (virtualbox)
poweroffが表示されていれば、停止しています。
仮想マシンの削除
$ vagrant destroy
※boxは残るので、環境をやり直したいときに使う感じ。