Help us understand the problem. What is going on with this article?


More than 5 years have passed since last update.

※筆者の環境はMac OS Xです


Vagrant Box をイチから作る!(備忘録)


  • Vagrant初心者


  • VirtualBoxがインストール済み
  • Vagrantがインストール済み
  • Vagrant Boxの用意



Vagrant Boxの用意

  • 自分で作成したもの
  • この辺からダウンロードしてきてもOK


Vagrant Boxの追加

  • Macのターミナルを立ち上げる
  • Vagrant Boxの追加
$ vagrant add [box名] [boxのパス]
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box '[box名]' (v0) for provider: 
    box: Unpacking necessary files from: file:///[boxのパス]
==> box: Successfully added box '[box名]' (v0) for 'virtualbox'!

# $ vagrant add centos6 /vagrant/box/
  • 追加されたかどうか確認
$ vagrant box list


  • Vagrant初期化
$ vagrant init [box名]
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
`` for more information on using Vagrant.

# box名は省略可能ですが、後の設定項目が1つ増えます
  • Vagrantfile(設定)の編集
$ vi Vagrantfile
# 使用するBox名 = "[box名]"
# ポートフォワーディング(http://localhost:[ポート番号]/でアクセスしたい場合等に) "forwarded_port", guest: 80, host: [ポート番号]
# 仮装マシンのIPアドレス "private_network", ip: ""
# GUIの有効化やメモリ数
config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "512"
  • Vagrant起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box '[box名]'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: [box名]_default_xxxxxxxxxxxxx_xxxxx
==> 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:
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /vagrant/[box名]
  • Vagrantにアクセス
$ vagrant ssh


  • 電源をOFFする
$ vagrant halt
==> default: Attempting graceful shutdown of VM...


$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

# -fオプションでyes/noを聞かれずに削除します
# また`vagrant up`すると仮装環境を作れます


コマンド 説明
vagrant init 初期化
vagrant ssh vagrantにアクセス
vagrant up 起動
vagrant halt 停止
vagrant reload 再起動
vagrant destroy 削除
vagrant status ステータス
vagrant global-status 全ステータス
vagrant box add [box名] [boxパス] vagrant box追加
vagrant box remove [box名] vagrant box削除
vagrant box list vagrant boxリスト


  • 下記のようなファイル構造にしておくと分かりやすいです
        - box1
        - box2
        - Vagrantfile
        - Vagrantfile
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away