MacPCにVirtualboxとVagrantでUbuntuの環境構築します。
OS: OSx El Capitan(Mac)
Version 10.11.16
1.最初にVirtualboxとVagrantをインストール
ダウンロード手順はドットインストールの1~3の動画を見ると分かり易いです。
http://dotinstall.com/lessons/basic_localdev_mac
ローカル開発環境の構築 [MacOS X編]
VirtualBoxのダウンロードページ
https://www.virtualbox.org/wiki/Downloads
Vagrantのダウンロードページ
https://www.vagrantup.com/downloads.html
※vagrantがダウンロードできているか以下のコマンドで確認できます。
~:$ vagrant -v
2.Ubuntuをインストール!!
以下のページからダウンロードしたいOSを選択してダウンロードします
今回の例ではUbuntu 15.04 (based on amd64 server iso file)
をダウンロードします。 ダウンロード自体は下のコマンドで行います。
OS一覧ページ
ターミナルを開き、以下のコマンドを打ちます。
vagrant box add Box名(自分で決める)取得元URL(OS一覧ページから選ぶ)
今回はこのように入力します。
~:$ vagrant box add Ubuntu15.04 https://github.com/kraksoft/vagrantbox -ubuntu/releases/download/15.04/ubuntu-15.04-amd64.box
※以下のコメンドでUbuntuが追加されたか確認することができます。
~:$ vagrant box list
3.Vagrantの設定
Vagrantを動かす作業用ディレクトリを作成する必要があります。
今回は以下のコマンドで作成しました。
~:$ mkdir ~/dev/vagrant/Ubuntu15.04
次にそこのディレクトリに移動します。
~:$ cd ~/dev/vagrant/Ubuntu15.04
移動したらVagrantの設定ファイルを以下のコマンドで作成します。
~/dev/vagrant/Ubuntu15.04:$ vagrant init Ubuntu15.04
作成が完了したらそのディレクトリ内で以下のコマンドで入力して
Vagrantを起動させてみましょう
~/dev/vagrant/Ubuntu15.04:$ vagrant up
起動は成功しますが以下のエラーが発生しました
※後で説明しますのでとりあえずVagrantに接続しましょう
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attemped was:
set -e
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
The error output from the command was:
: No such device
4.Vagrantに接続
上記のコマンドでVagrant upしたのでローカル環境は動いていると思います。 念のため、以下のコマンドでローカル環境が起動しているか確認して見て下さい。 ~/dev/vagrant/Ubuntu15.04:```$ vagrant status ```
動いていたら以下のコマンドで接続してください!
~/dev/vagrant/Ubuntu15.04:$ vagrant ssh
接続すると$の前が変わっていることを確認して下さい
例 vagrant@vagrant-ubuntu-trusty:$
5.エラー解決法
探したところプラグインを入れればすぐに解決出来そうでしたが、
情報とバージョンが古かったので少し難しいですが確実な方法でやります。
エラーの内容は共通フォルダがありませんと言われています。
通常は/etc/init.d/の中にvboxaddというフォルダがないといけないのそうです。
なので以下のサイトからダウンロードします。
http://download.virtualbox.org/virtualbox/
しかし共通フォルダがないのでMac上のブラウザでダウンロードすると
仮想環境(Ubuntu)に入れるのは少し手間がかかります。
そこで以下2つのwgetコマンドでファイルをダウンロードします!
vagrant@vagrant-ubuntu-trusty:$ cd /tmp
vagrant@vagrant-ubuntu-trusty:/tmp$ wget http://download.virtualbox.org/virtualbox/5.0.20/VBoxGuestAdditions_5.0.20.iso
※wgetはURLを入れることでブラウザ上のファイルをダウンロードすることができます。
vagrant@vagrant-ubuntu-trusty:/tmp$
sudo mount -t iso9660 /tmp/VBoxGuestAdditions_5.0.20.iso /mnt
コマンドを入力したらtmpフォルダはもう用がないの出ます
vagrant@vagrant-ubuntu-trusty:/tmp$ cd ..
そうしたら上記で作ったmntフォルダに移動します。
vagrant@vagrant-ubuntu-trusty:$ cd /mnt
移動したら以下のコマンドを入力すると
vagrant@vagrant-ubuntu-trusty:/mnt$ sudo ./VBoxLinuxAdditions.run
You should restart your guest to make sure the new modules are actually used
と言われるのでexitでvagrantからログアウトし、
以下のコマンドでVagrantを再起動します。
$ vagrant halt で停止、 $ vagrant up で起動です。