Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

Ruby on Rails | Rails4環境でbundle execを省略するための設定

概要

Rails4環境でbundle execを省略するための設定

詳細

Rails4環境でbundle execを省略するための設定について

手順

・rbenv-binstubsをGitHubよりCloneします
・「~/.bundle/config」に下記を追記

BUNDLE_BIN: bin

※上記手順の詳細はVagrantfile中で確認

Rails環境を構築

# ローカル環境からvagrantを操作
$ vagrant up # Vagrantfileの詳細は後述
$ vagrant ssh

# ここからサーバー操作
vagrant@vbbinstubs:~$ mkdir work
vagrant@vbbinstubs:~$ cd work
vagrant@vbbinstubs:~/work$ cat << EOS > Gemfile
> source "http://rubygems.org"
> gem "rails", "~> 4.0.0"
> EOS
vagrant@vbbinstubs:~/work$ cat Gemfile
source "http://rubygems.org"
gem "rails", "~> 4.0.0"
vagrant@vbbinstubs:~/work$ bundle install --path vendor/bundle
vagrant@vbbinstubs:~/work$ bundle exec rails new  ../hoge --skip-bundle
vagrant@vbbinstubs:~$ cd ../
vagrant@vbbinstubs:~/hoge$ cd  hoge
vagrant@vbbinstubs:~/hoge$ cat << EOS >> Gemfile
> gem 'execjs'
> gem 'therubyracer'
> EOS

vagrant@vbbinstubs:~/hoge$ bundle install --path=vendor/bundle --binstubs=.bundle/bin
vagrant@vbbinstubs:~/hoge$ rbenv rehash

# bundle execを省略してrakeを呼び出し
vagrant@vbbinstubs:~/hoge$ rake -T | head -3
rake about                  # List versions of all Rails frameworks and the environment
rake assets:clean[keep]     # Remove old compiled assets
rake assets:clobber         # Remove compiled assets

# bundle execを省略してrailsを起動
vagrant@vbbinstubs:~/hoge$ rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-10-30 22:00:34] INFO  WEBrick 1.3.1
[2013-10-30 22:00:34] INFO  ruby 2.0.0 (2013-02-24) [x86_64-linux]
[2013-10-30 22:00:34] INFO  WEBrick::HTTPServer#start: pid=18439 port=3000
起動画面

下記にアクセスして確認
http://192.168.33.15:3000/

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

$script = <<SCRIPT
echo 'start shell'
su - vagrant -c '
  PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

  echo "start install basic"
  sudo apt-get update
  sudo apt-get install build-essential bison git-core libreadline6-dev curl zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt1-dev autoconf libncurses5-dev -y
  sudo apt-get install libsqlite3-dev sqlite3 -y
  echo "end   install basic"

  echo "start install rbenv"
  git clone https://github.com/sstephenson/rbenv.git /home/vagrant/.rbenv

  echo "export PATH=/home/vagrant/.rbenv/bin:$PATH" >> /home/vagrant/.bash_profile
'
echo \'eval "$(rbenv init -)"\' >> /home/vagrant/.bash_profile
source /home/vagrant/.bash_profile
echo "end   install rbenv"

su - vagrant -c '
  echo "start install ruby 2.0.0-p0"
  git clone https://github.com/sstephenson/ruby-build.git /home/vagrant/ruby-build
  cd /home/vagrant/ruby-build
  sudo ./install.sh
  cd /home/vagrant
  rbenv install 2.0.0-p0
  rbenv global 2.0.0-p0
  rbenv rehash
  echo "end   install ruby 2.0.0-p0"

  echo "start install bundler"
  rbenv exec gem i bundler

  mkdir ~/.rbenv/plugins
  cd ~/.rbenv/plugins
  git clone https://github.com/ianheggie/rbenv-binstubs.git

  mkdir ~/.bundle
  echo 'BUNDLE_BIN: bin' >> ~/.bundle/config
  echo "end   install bundler"
'
SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.host_name = "vbbinstubs"
  config.vm.box = "ubuntu-12.04-x64"
  config.vm.network :private_network, ip: "192.168.33.15"

  config.vm.provision "shell", inline: $script
end