MacでVagrant導入

今までは、自分のローカル環境で開発環境を構築していたが、
Vagrantを使えば簡単なコマンドで開発環境がVM上に構築できるということを知ったので、今回はVagrantを使用して開発環境を構築しようと思う。
自分のメモ用に、Vagrant導入までの流れを記録する。

1. VirtualBoxインストール
https://www.virtualbox.org/wiki/Downloads
からMac用のものをダウンロードする。
ダウンロードしたものをダブルクリックで、あとは通常のインストールをする。

2. Vagrantダウンロード
http://downloads.vagrantup.com/
から最新版をダウンロードする。 現在の最新バージョンv1.1.5。
vagrant.dmgがダウンロードされるので、ダブルクリックする。

f:id:takahitonara:20130405004334p:plain


vagrant.pkgをダブルクリックして、インストール画面に移行する。

f:id:takahitonara:20130405004502p:plain


そのまま、他のソフト同様インストーラーの選択をすすめていく。
インストール完了。

f:id:takahitonara:20130405004545p:plain


ターミナルでvagrantコマンドが使えることを確認した。

nara-no-MacBook-Air:vagrant NaraTakahito$ vagrant --version
Vagrant version 1.1.5
nara-no-MacBook-Air:vagrant NaraTakahito$ 

3. box選択
http://www.vagrantbox.es/
からvagrantで構築したい環境を選択する。
今回は、以下を選択。
Name: Debian 6 Squeeze x64 configured according to documentation
URL: http://www.emken.biz/vagrant-boxes/debsqueeze64.box

以下のコマンドを実行する。

nara-no-MacBook-Air:vagrant NaraTakahito$ sudo vagrant box add debsqueeze64 http://www.emken.biz/vagrant-boxes/debsqueeze64.box
Password:
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: http://www.emken.biz/vagrant-boxes/debsqueeze64.box
[vagrant] Downloading box: http://www.emken.co/vagrant-boxes/debsqueeze64.box
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...
nara-no-MacBook-Air:vagrant NaraTakahito$ 

4. リスト表示
登録されたかを確認するため、リスト表示をする。

nara-no-MacBook-Air:vagrant NaraTakahito$ vagrant box list
debsqueeze64

5. vagrant用のディレクトリを作っておく
box毎の設定ファイルを置くためのディレクトリを作成する。

nara-no-MacBook-Air:work NaraTakahito$ ls -ld vagrant
drwxr-xr-x  2 NaraTakahito  staff  68  4  5 01:21 vagrant

6. vagrantの初期化
まずは、初期化をする。

nara-no-MacBook-Air:vagrant NaraTakahito$ vagrant init
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.
nara-no-MacBook-Air:vagrant NaraTakahito$ ls
Vagrantfile

7. Vagrantfileを編集
初期化をすると、Vagrantfileがカレントディレクトリに作成されているので、
以下のとおりに編集する。

- config.vm.box = "base"
+ config.vm.box = "debsqueeze64"

※ もしくは初期化の際に、以下で実行する。

vagrant init debsqueeze64

8. 起動
いよいよ起動する。

nara-no-MacBook-Air:vagrant NaraTakahito$ sudo vagrant up
[default] Importing base box 'debsqueeze64'...
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.

Guest Additions Version: 4.1.24
VirtualBox Version: 4.2.10
[default] Mounting shared folders...
[default] -- v-root: /vagrant
nara-no-MacBook-Air:vagrant NaraTakahito$

9. 接続
起動した、VM上のlinuxssh接続する。

nara-no-MacBook-Air:vagrant NaraTakahito$ sudo vagrant ssh
Linux vagrant-debian-squeeze 2.6.32-5-amd64 #1 SMP Sun Sep 23 10:07:46 UTC 2012 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
No mail.
Last login: Sat Feb 16 09:20:45 2013 from 10.0.2.2
vagrant@vagrant-debian-squeeze:~$ 

※ 参考URL
http://www.1x1.jp/blog/2013/03/vagrant.html