Vagrant で仮想環境構築

Vagrant のインストール

homebrew-cask のインストール


$ brew tap caskroom/homebrew-cask
$ brew install brew-cask

VirtualBox, Vagrant のインストール


$ brew cask install virtualbox
$ brew cask install vagrant

Box の取得

Vagrantbox.es から vagrant box add で取得. 今回は Ubuntu 14.04 32bit のBoxを取得してみる

$ vagrant box add ubuntu1404_32 http://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box

仮想マシンを初期化


$ mkdir vm_ubuntu1404_32     # 作業ディレクトリ作成
$ cd vm_ubuntu1404_32
$ vagrant init ubuntu1404_32   # 仮想マシンの初期化. vagrant init の引数には vagrant box add 時のBox名を指定

仮想マシンの起動


$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu1404_32'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vm_ubuntu1404_32_default_1399722609465_83657
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (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: Warning: Connection timeout. Retrying...
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => /Users/user/vm_ubuntu1404_32

ssh で接続


$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic i686)

* Documentation: https://help.ubuntu.com/

System information disabled due to load higher than 1.0

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

vagrant@vagrant-ubuntu-trusty-32:~$

その他コマンド


# 状態の確認
$ vagrant status

# 一時的な停止
$ vagrant suspend

# suspendからの復帰
$ vagrant resume

# シャットダウン
$ vagrant halt

# Vagrantfile の再読み込み
$ vagrant reload

# 仮想マシンの削除
$ vagrant destroy

参考