親バカエンジニアのナレッジ帳

webのエンジニアをやっており、日頃の開発で詰まったことや書き残しておきたいことを載せています。

MacにCentOS7のVagrant環境を構築


タイトルにある通り、MacにCentOS7のVagrant環境を構築した時のメモです。
必要なものをインストールしてvagrant sshでLinux環境に入れるまでの手順になります。

バージョン情報
VirtualBox:5.1
CentOS:7.2

まずはVirtualBoxのインストールです。
dmgファイルを以下よりダウンロードして手順通り進めればインストールが完了します。

Oracle VM VirtualBox

私がインストールした時は最新バージョンが5.2でした。
ただ後述しますが、最新バージョンでは途中うまくいかない箇所があったので、
以下より5.1をダウンロードしました。

https://www.virtualbox.org/wiki/Download_Old_Builds_5_1

次にVagrantをインストールします。
これも以下よりMacOSのdmgファイルをダウンロードして手順通り進めればOKです。

www.vagrantup.com

ここからはターミナルを開いて環境構築になります。
まずはCentOS7のvagrantboxを作ります。
boxはネットで探せば色々上がってますが、その中でCentOS7.2のものを今回は使いました。

vagrant box add CentOS7 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box

次にVagrantfileを設置するディレクトリに移動し、
以下のコマンドを入力すればVagrantの初期化がされてVagrantfileも作成されます。

vagrant init CentOS7

あとは以下コマンドでvagrantを立ち上げることができます。

vagrant up


ここで注意点ですが、私がこの対応をした時は、最初にVirtualBoxの5.2のバージョンで行なったのですが、
その時点ではVagrantがVirtualBoxの5.2に対応していなかったために、
以下のエラー表示がされました。

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:

4.0, 4.1, 4.2, 4.3, 5.0, 5.1

A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.

表示の通り「4.0, 4.1, 4.2, 4.3, 5.0, 5.1」のバージョンをインストールすれば問題ないのですが、
一応バージョンには注意してください。