LoginSignup
1
1

More than 5 years have passed since last update.

rails-dev-box に rbenv 導入

Last updated at Posted at 2015-05-15

準備

  • メモリ割り当てを増やしておく。 ※デフォルトのまま(512MB?)だと、json等の各種gemビルド時に、メモリ不足エラーが発生した。
  • ruby-debug-ideを使って、Host->Guestのリモートでバッグをする場合、かつその際に1234番ポートをforwardするなら、その設定も追加しておく
Vagrantfile
Vagrant.configure('2') do |config|
  config.vm.box      = 'ubuntu/trusty64'
  config.vm.hostname = 'rails-dev-box'

  config.vm.network :forwarded_port, guest: 3000, host: 3000

  # for Remote Debug (Host RubyMine -> Guest ruby-debug-ide)
  config.vm.network :forwarded_port, guest: 1234, host: 1234

  config.vm.provision :shell, path: 'bootstrap.sh', keep_color: true

  # increase memory: https://www.vagrantup.com/docs/virtualbox/configuration.html
  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
  end
end

環境構築

$ sudo timedatectl set-timezone Asia/Tokyo ※表示を日本時間に

$ cd

$ vi .gemrc   以下2行追加
install: --no-document
update: --no-document

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ git clone https://github.com/rbenv/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
$ git clone git://github.com/ianheggie/rbenv-binstubs.git ~/.rbenv/plugins/rbenv-binstubs

$ vi .bashrc   以下2行追加
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

$ source .bashrc    ※反映

$ rbenv --version   ※rbenv自体のバージョン表示

$ rbenv versions    ※インストールされているruby一覧

※インストール出来るruby一覧
$ rbenv install --list  

※リストを更新
$ cd ~/.rbenv/plugins/ruby-build
$ git pull

※rubyをインストール。rdocをインストールせずに。
$ CONFIGURE_OPTS="--disable-install-rdoc" rbenv install 2.2.5

rbenv-binstubsの代わりにdirenvを使う手もあるとのこと。

新規アプリの作成

$ mkdir gomi_app
$ cd gomi_app
$ rbenv local 1.9.3-p551
$ rbenv exec gem install bundler
$ rbenv which bundle
/home/vagrant/.rbenv/versions/1.9.3-p551/bin/bundle
$ rbenv which bundler
/vagrant/gomi_app/bundle_bin/bundler

$ rbenv exec bundle init

$ vi Gemfile  ※以下の行のコメントを外す(必要があればバージョン指定も書く)
gem "rails"

$ bundle install --binstubs=bundle_bin --without production

$ bundle exec rails new . --skip-bundle
★skip-bundleは『bundle installをしない』オプション。gemをlocalに入れてない場合は、--skip-bundle 不要=bundle installしてもらっていいのかな。

※binstubsやwithoutオプションは .bundle/configファイルに記憶されているので、再度指定する必要なし
$ bundle install

※gemがどこにinstallされているか確認する
$ bundle show --paths

既存アプリの移行

$ rbenv local 1.9.3-p551
$ rbenv exec gem install bundler
$ bundle install --binstubs=bundle_bin --without production
$ rails new . --pretend --skip --skip-test-unit --skip-bundle
$ rails new . --skip --skip-test-unit --skip-bundle
$ rake db:migrate RAILS_ENV=development

参考リンク

rbenv を利用した Ruby 環境の構築

bundle exec コマンドを省略する
Bundler4つの便利な使い方(初期化、インストール、省略する設定など)

Debian 7.0, Ubuntu 13.04 に rbenv をインストールする

Vagrant環境でのbundle installで失敗する
★これで死ぬほどはまった。ありがとう(^^)

bundle execを使わずに済む方法(rbenv編)

rbenv + bundler 環境で Ruby on Rails4 プロジェクトを新規作成する

.gitignoreのベストプラクティス

CentOS7 on Vagrantでの時刻設定

1
1
0

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
1
1