18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WindowsマシンでVagrantを使用してUbuntuを起動する手順

Last updated at Posted at 2014-03-27

Vagrant(ベイグラント)

テスト用の仮想マシン作成や環境設定を自動化するツール


インストール環境

  • Windows 7 pro 64bit
  • Vagrant 1.5.1
  • VirtualBox 4.3.10

Vagrantインストール

  1. 本家からダウンロードしてインストール
  2. 再起動

VirtualBoxインストール

  1. Download VirtualBox からダウンロードしてインストール

VagrantでVirtualBox用の仮想マシンを作成

  1. Proxy設定

    1. コマンドプロンプト起動しproxy環境変数設定
      set http_proxy={HOST}:{PORT}
      set https_proxy={HOST}:{PORT}

    2. vagrant用proxyプラグインインストール
      vagrant plugin install vagrant-proxyconf

    3. proxy設定記述

    ~/.vagrant.d/Vagrantfile
        Vagrant.configure("2") do |config|
          if Vagrant.has_plugin?("vagrant-proxyconf")
            config.proxy.http     = ENV["HTTP_PROXY"]
            config.proxy.https    = ENV["HTTPS_PROXY"]
            config.proxy.no_proxy = "localhost,127.0.0.1"
          end
        end```
    
    
  2. BOX(仮想マシンひな形)の設定
    例)Ubuntuの場合
    vagrant box add Ubuntu-13.04 http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box

    vagrant box add Ubuntu-13.04 http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box
    ==> box: Adding box 'Ubuntu-13.04' (v0) for provider:
    box: Downloading: http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box
    box: Progress: 100% (Rate: 466k/s, Estimated time remaining: --:--:--)
    ==> box: Successfully added box 'Ubuntu-13.04' (v0) for 'virtualbox'!

    ダウンロードされたBOXファイルは~/.vagrant.d/以下に保存される

  3. Vagrant設定ファイル作成
    適当なフォルダに移動して
    vagrant init Ubuntu-13.04
    ※box addで付けた名前を指定

  4. 仮想マシン起動
    vagrant up

    vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'Ubuntu-13.04'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Setting the name of the VM: xxxxxxx
    ==> 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: Error: Connection timeout. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    default: Error: Remote connection disconnect. Retrying...
    ==> 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.10
    default: VirtualBox Version: 4.3
    ==> default: Configuring proxy for Apt...
    ==> default: Configuring proxy environment variables...
    ==> default: Mounting shared folders...
    default: /vagrant => C:/Users/{適当なフォルダ}

    何かエラーが出ているがとりあえずおいておく


動作確認

  • ログイン
    1. ターミナルで 127.0.0.1:2222 に接続し、 vagrant/vagrant でログイン出来ることを確認

その他コマンド

  • 稼働状況確認
    vagrant status

  • 再起動
    vagrant reload

  • sshでログイン
    vagrant ssh

  • ゲストOSを終了
    vagrant halt

  • 仮想ホストを削除
    vagrant destroy

  • 現在起動中の環境からBOXファイルを作成する
    vagrant package


その他

  • GUIモードで起動するように変更する場合

    1. Vagrantfile の以下部分をアンコメントし、再起動vagrant upする
    ./{適当なフォルダ}/Vagrantfile
    config.vm.provider "virtualbox" do |vb|
      # Don't boot with headless mode
      vb.gui = true
    
      # Use VBoxManage to customize the VM. For example to change memory:
      vb.customize ["modifyvm", :id, "--memory", "1024"]
    end```
    
    

vagrant upしたら以下のエラーメッセージが出た場合

Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the VBoxManage binary that ships with VirtualBox, and requires
this to be available on the PATH. If VirtualBox is installed, please find the
VBoxManage binary and add it to the PATH environmental variable.

  • コンパネから環境変数にpath追加

      C:\Program Files\Oracle\VirtualBox
    

    もしくはコマンドプロンプトで
    SETX /M PATH "%PATH%;C:\Program Files\Oracle\VirtualBox"
    でも
    SETX /M PATH "%PATH%;%VBOX_INSTALL_PATH%"
    でもいけるかも


参考

Vagrant本家
Virtualbox本家
Vagrantbox.es
仮想環境構築ツール「Vagrant」で開発環境を仮想マシン上に自動作成する
Vagrantで簡単仮想マシン構築
WindowsのVagrant上でDockerを使う

18
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?