概要
vagrantはVirtualBoxまたはVMwareを使って、仮想マシンを構築するツールです。
公式HP
http://www.vagrantup.com/
今回やること
- VirtualBoxインストール
- Vagrantインストール
- Vagrantfile作成
- 仮想マシン起動
- 仮想マシンにSSHログイン
※今回作成する仮想マシンは、
Ubuntu 12.04.5 LTS (Precise Pangolin)
VirtualBoxをインストール
以下のサイトからダウンロードしてインストール。
https://www.virtualbox.org/wiki/Downloads
Vagrantをインストール
以下のサイトからダウンロードしてインストール。
https://www.vagrantup.com/downloads.html
Vagrantfileを作成
仮想マシン用にディレクトリ作成しておきます。
$ mkdir vagrant_test
cd vagrant_test
以下の内容をVagrantfile
というファイル名でカレントディレクトリに保存。
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end
デフォルトのVagrantfileはコメントがずらずら書いてありますが、
今回は不要な箇所を省きました。
ちなみに、上記のファイル内容は、以下を実行したのと同じです。
$ vagrant box add precise64 http://files.vagrantup.com/precise64.box
$ vagrant init precise64
今回はubuntuの仮想マシンを作成しますが、様々なOSのインストールが可能です。
以下サイトでBoxのイメージリストが閲覧できます。
http://www.vagrantbox.es/
仮想マシンを起動
以下を実行。
$ vagrant up
以下ログ
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_test_default_1411977416152_90752
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
default: /vagrant => /Users/[User]/vagrant_test
仮想マシンにsshログイン
vagrant_test$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$
完了
以上で仮想マシンの作成ができました。
今回は手っ取り早く仮想マシンを作成する手順でした。
別の機会にVagrantfileの事を詳しく解説します。