てくてくテック☆

千里の道も一歩から。テック(Tech)の道をてくてく歩く。

Vagrant と VirtualBox で自由に汚せるローカル環境を構築する

これまで自宅用 Mac に直接あれこれインストールしていて、どんどん環境が汚れていく?のが気になったので、Vagarnt と VirtualBox でローカル環境を作ってみることにしました。
 

VirtualBox のインストール

以下から VirtualBox をダウンロードして、インストールします。

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

2017/10/28 現在の VirtualBox 最新バージョンは 5.2.0
 

Vagrant のインストール

以下から VirtualBox をダウンロードして、インストールします。

https://www.vagrantup.com/downloads.html

2017/10/28 現在の Vagrant 最新バージョンは 2.0.0
 
 

box ファイルの追加

以下のコマンドを実行して、Vagrant の box ファイルをダウンロードします。
(ダウンロードする box ファイルの OS やバージョンなどはお好みで)

$ vagrant box add CentOS_7.1 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.1/vagrant-centos-7.1.box

ちなみに box ファイルのダウンロードは結構時間がかかります。
(今回、CentOS 7.1 をダウンロードしたと見せかけて、後述の手順では過去にダウンロード済みだった CentOS 6.5 の box ファイルを使ってたり。。)
 

Vagrant の初期化

以下のコマンドを実行して、Vagrant を初期化します。 (vagrant init した場所に Vagrantfile が作られるので、コマンドを実行する前に cd などでディレクトリを移動しておく必要あり)

$ vagrant init CentOS_7.1

 

仮想マシンの起動

以下のコマンドで仮想マシンを起動します。
(Vagrantfile のある場所でコマンドを実行)

$ vagrant up

 

仮想マシンの起動時にエラーが出たら。。

vagrant up したときに以下のようなエラーが出た場合は、Vagrant が対応していないバージョンの VirtualBox をインストールしている可能性があります。

$ vagrant up
No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

 
以下のコマンドで対応している VirtualBox のバージョンを確認してみると、今回インストールした Vagrant 2.0 に VirtualBox 5.2.0 は対応していないことが判明。。orz

$ vagrant up --provider=virtualbox
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.

しょうがないので VirtualBox 5.1.30 を再インストールして、vagrant up したら無事に起動できました。
 

仮想マシンSSH ログイン

以下のコマンドで vagrant up した仮想マシンssh でログインできます。

$ vagrant ssh
[vagrant@localhost ~]$ 

 
仮想マシンssh を終了するには exit で!

[vagrant@localhost ~]$ exit

 

仮想マシンの終了

以下のコマンドで仮想マシンが終了できます。

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

 

これで自由に汚してしまえる?ローカル環境が手に入ったので、いろいろ試してみたいと思いますー
 

関連記事

本記事に関連する記事です。よろしければどうぞー!

teku2tech.hateblo.jp

teku2tech.hateblo.jp