はじめに
macでrailsの開発環境を構築したときのメモです。
rails周りは毎回忘れるのでメモしないときついです。
macを汚したくないのでvagrantで快適な環境を構築をめざしました。
ホスト側(mac)でコード書いて、ゲスト側(CentOS)で実行を簡単にできるイメージです。
ほとんどこちらの記事のパクリです
が、自分用にメモしました。ありがとうございます。
環境
ホスト: macos sierra
ホスト: centos6.7
Shell: Bashです
Vagrant: 1.9.1
Vagrantのホスト側ディレクトリ:~/vagrant/bento/centos-6.7
いつも忘れること
http://qiita.com/windhorn/items/0f58866291f8273f18fb
http://shokai.org/blog/archives/7262
やったこと(ホスト側)
VirtualBoxのインストール
http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html?ssSourceSiteId=otnjp
mac用のを落としてインストール
Vagrantのインストール
公式ダウンロードサイトを参照
macはdmg落としてインストール
CentOSのイメージ落とす
ホーム下にvagrantフォルダを作りその下で管理するようにしてみます。
box addしたほうがいいのですが下記でもつくれちゃったのでそのまま。
mkdir ~/vagrant/bento/centos-6.7
cd ~/vagrant/bento/centos-6.7
vagrant init bento/centos-6.7
.ssh/configに追記
vagrantはデフォルトで192.168.33.10を仮想Networkで持つので.ssh/configに反映しておくと楽
ssh 192.168.33.10
でパスワードなしでログインできます。
vagrant ssh-config --host 192.168.33.10 >> ~/.ssh/config
ShardFolderの設定
rsyncでゲスト、ホスト間のファイルをやり取りできるように。
nfsはパーミッション周りが面倒なのでこちらを試してみます。
ホスト側は~/vagrant/bento-centos6.7/mount
、ゲスト側は/vagrant
フォルダにマウントしてます。
config.vm.synced_folder "./mount", "/vagrant",
type: "rsync",
owner: "vagrant",
group: "vagrant",
rsync__exclude: [".git", "tmp", "log", "cache"],
rsync__chown: true
Vagrant上げてみる
cd ~/vagrant/bento/centos-6.7
vagrant up
vagrant rsync-auto #rsync自動設定
やったこと (ゲスト側)
以下CentOS側の作業です。vagrantユーザでの作業
いろいろインストール
めんどくさいので先にいろいろインストール
sudo yum groupinstall "Development Tools"
sudo yum install libxml2-devel libxslt-devel # nokogiri用
sudo yum install epel-release
sudo yum install nodejs npm --enablerepo=epel # for Javascript Runtime
あとでJavaScriptランタイムがインストールされていないというエラーがでるので
node.jsをインストールしておく
https://github.com/sstephenson/execjs
http://qiita.com/azusanakano/items/771dc9919f347de061d7
rbenv&ruby buildインストール
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
# PATH に追加
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
# .bash_profile に追加
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
# 上記設定の再読み込み
exec $SHELL -l
# ruby build
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
#確認
rbenv -version
#-> rbenv 1.1.0
ruby入れる
#rbenv install --list | grep -v ruby #インストールできるリスト確認
rbenv install 2.3.3
rbenv global 2.3.3
rbenv rehash
bundler入れる
gem install bundler
bundle -v
サンプルアプリ用のディレクトリ作る
mkdir /vagrant/sample
cd /vagrant/sample
bundle init
Gemfileをいじる
bundle initでできたGemfileを修正
rails4系にしたかったので下記追加
gem 'rails', '4.2.3'
アプリ初期化
質問はYを入力すると雛形ができます。
bundle install --path=vendor/bundle
bundle exec rails new .
rails起動
--bind指定することでlocalhost以外でも通信可能になるので
portforward設定しないでもmacから見れます。
bundle exec rails s --bind=0.0.0.0
ブラウザで確認
で見れる様になるとおもいます。これでホスト側をSublimeTextなどで編集し
ゲスト側で動作確認がすぐにできますね。
bundle execが毎回辛い
bundle exec ~ばかりうつと辛くなってくるなとおもってたら下記記事がありました。
この記事参考にしました.
cd ~/.rbenv/plugins
$ git clone git://github.com/ianheggie/rbenv-binstubs.git
プロジェクトのbundle install時に指定すればいいみたいです。
cd path/to/project
$ bundle install --binstubs=bundle_bin
$ rbenv rehash
$ rails s
幸せ。
参考リンク
http://www.1x1.jp/blog/2014/03/vagrant-rsync-synced-folder.html
https://github.com/sstephenson/execjs
http://blog.hotolab.net/entry/vagrant_rsync
http://isseium.hateblo.jp/entry/20101031/1288510938
https://atlas.hashicorp.com/bento/boxes/centos-6.7
http://pyoonn.hatenablog.com/entry/2014/10/17/165416
http://qiita.com/azusanakano/items/771dc9919f347de061d7
http://qiita.com/narikei/items/cd029911597cdc71c516
http://d.hatena.ne.jp/yk5656/20141001/1413183535
http://sunday150.hatenablog.com/entry/2016/05/06/022113
http://qiita.com/naoty_k/items/9000280b3c3a0e74a618