LoginSignup
171

More than 5 years have passed since last update.

rbenv インストールから Bundler を使用した Rails のローカルインストールと起動まで

Last updated at Posted at 2014-11-04

以下の環境は全て Mac OS X

参考にした URL

rbenv を利用した Ruby 環境の構築
http://dev.classmethod.jp/server-side/language/build-ruby-environment-by-rbenv/

Rails開発環境の構築(rbenvでRuby導入からBundler、Rails導入まで)
http://qiita.com/emadurandal/items/a60886152a4c99ce1017

rbenv, ruby-buildのインストール
http://qiita.com/isaoshimizu/items/07ddd1e25996e19d45bf

rbenv環境でBundlerを使ってgemを管理する
http://qiita.com/itayan/items/2948d35a699bbfd98f66

Ruby on Railsでモデル作成からデータ取得まで実装してみる
http://owen11.hateblo.jp/entry/2014/08/14/175551

rbenv のインストール

rbenv は Ruby の切替が主目的のようなので、Ruby のインストールが出来るように ruby-build も併せてインストールする

Git でインストール

Mac OS X の homebrew でもインストールできるけど、最新版が使えるまでに時間がかかることもあるようなので Git 推奨

rbenv

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

$ source ~/.bashrc
$ rbenv --version
rbenv 0.4.0

ruby-build

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

Git でインストールした場合のアップデート方法

$ cd ~/.rbenv
$ git pull
$ cd ~/.rbenv/plugins/ruby-build
$ git pull

Mac OS X の homebrew でインストール

いつも手を抜く brew update は必ず実行すること

$ brew update
$ brew install rbenv ruby-build

$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc

$ source ~/.bashrc
$ rbenv --version
rbenv 0.4.0

新規 Rails プロジェクト作成手順

ローカル Ruby のインストール

インストールされている Ruby のバージョンを確認

$ rbenv versions
* system (set by /Users/eikichi/.rbenv/version)
  2.1.3

Ruby の新しいバージョンが有るか確認

$ rbenv install -l
  2.1.0
  2.1.1
  2.1.2
  2.1.3
  2.1.4
  2.2.0-dev
  2.2.0-preview1

新しいバージョンの Ruby をインストール

$ rbenv install 2.1.4
Downloading ruby-2.1.4.tar.gz...
-> http://...
Installing ruby-2.1.4...
Installed ruby-2.1.4 to /...

プロジェクトディレクトリに移動してローカル Ruby を設定

$ cd /path/to/project
$ rbenv local 2.1.4

プロジェクトディレクトリに .ruby-version ファイルが生成される

ローカル Ruby のバージョン確認

$ rbenv version
2.1.4 (set by /...

Rails のローカルインストールの準備

ローカル Ruby に Bundler のみをインストール

$ rbenv exec gem install bundler
$ rbenv rehash

Gemfile 作成

$ rbenv exec bundle init

Gemfile 編集

$ vi Gemfile

編集内容

source "https://rubygems.org"

gem "rails", "4.1.7" # 出来るだけ開発開始時の最新バージョンを指定

Rails を vender/bundle ディレクトリ以下にインストール

$ rbenv exec bundle install --path vendor/bundle

インストールされた Gem を確認

$ rbenv exec bundle list 
Gems included by the bundle:
  * actionmailer (4.1.7)
  ...

Rails プロジェクト作成

MySQL を利用

$ rbenv exec bundle exec rails new project-name --skip-bundle -d mysql

SQLite を利用(デフォルト)

$ rbenv exec bundle exec rails new project-name --skip-bundle

--skip-bundle を必ず指定すること。指定しないとと bundle install が発動し、ローカル Ruby 自体 に Gem がインストールされてしまう

Rails をローカルインストールするために使用した Bundler 環境を削除

$ rm -f Gemfile
$ rm -f Gemfile.lock
$ rm -rf .bundle
$ rm -rf vendor

Rails プロジェクトセットアップ

Rails プロジェクトディレクトリに移動

$ cd project-name

必要に応じて Gemfile などを編集

Bundler で必要な Gem をインストール

$ rbenv exec bundle install --path vendor/bundle

Gitの管理対象から vendor/bundle ディレクトリを外す

$ echo '/vendor/bundle' >> .gitignore

Rails プロジェクト起動

$ rbenv exec bundle exec rails server

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
171