LoginSignup
13
14

More than 5 years have passed since last update.

chrubyで快適Ruby環境

Last updated at Posted at 2015-02-18

chrubyで快適Ruby環境

rubyのバージョンを簡単に切り替えられるようにするツールです。
同様のツールとして、rbenvがありますが、
rbenvより起動も速く、シンプルで管理しやすいので、オススメです。

注: proxy環境だとruby-installがうまく動かないことがあるようです。
https://github.com/postmodern/ruby-install/issues/128

Mac OS Xでインストールした例:

brew install chruby
brew install ruby-install
# インストール可能なruby一覧
ruby-install
# 最新のstableを~/.rubies/にインストール
ruby-install ruby

# chruby用のフックを~/.bashrcに登録
cat<<EOF >> ~/.bashrc
# Ruby by chruby
source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh
EOF

# auto.shを使ってプロジェクト毎のRubyのバージョンを指定
# (曖昧でいいみたいです)
echo "2.1" > .ruby-version

# .bashrcを再読み込み
. ~/.bashrc

# Rubyのパスの確認
which ruby

gem update --no-document
gem install bundler

direnvでbundle execを不要に

毎回bundle exec rake ...とか打つの、面倒ですよね。

プロジェクトディレクトリにローカルにgemをインストールし、
vendor/binにbundle execが不要なスタブを作成します。

# Gemfileのあるプロジェクトディレクトリで
bundle install --path vendor/bundle --binstubs=vendor/bin

これで、vendor/binにPATHを通せばbundle execとか打つ必要がなくなります。
(Gemがインストールするrakeやpryの場合。rubyだけはbundle execが必要かも)

direnvは、ディレクトリに入った時にだけ適用される環境変数を、
.envrcという設定ファイルで設定できるツールです。

このdirenvを使って、プロジェクトディレクトリに入った際に、
自動的にvendor/binにPATHが通るように設定します。

brew install direnv
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
# プロジェクトディレクトリで
echo 'export PATH=$PWD/vendor/bin:$PATH' > .envrc

# ~/.bashrcを再読み込み
source ~/.bashrc

.envrcのあるディレクトリに移動した際、初回のみ、
その.envrcを適用していいか確認があるので、問題なければ許可してください。
次回から、自動的に適用されるようになります。

direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
13
14
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
13
14