LoginSignup
5
5

More than 5 years have passed since last update.

rbenv-around-installでglobal gemsetにbundle installしてrehashまで自動化する

Last updated at Posted at 2015-03-16

前提

プラグイン

下記のrbenvプラグインをインストールしておくこと。

Gemfile

rbenvでRubyをインストールしたあと自動的にインストールしたいgemを書いておく。
ホームディレクトリにGemfileが置いてある。

rbenv-around-installとは

Automatically run ~/.rbenv/before-install before and ~/.rbenv/after-install after rbenv install. Add extension .eval to eval instead of running those binaries. Don't forget to run chmod u+x ~/.rbenv/before-install ~/.rbenv/after-install. Note that without .eval extension it must be a bash script, otherwise it can be any proper executable.

という感じで、Rubyのインストール前後にスクリプトを実行できるプラグイン。
Rubyのインストール前に ~/.rbenv/before-install が実行され、インストール後に ~/.rbenv/after-install が実行される。

after-installスクリプトの作成

~/.rbenv/after-install を作る。実行権限をつけておくこと。
$RBENV_VERSION には直前にインストールしたRubyのバージョンがセットされる。
また、スクリプト実行中のRubyバージョンは直前にインストールしたRubyになっているので、gemのインストール先は自動的にインストールしたRubyが対象になる。

~/.rbenv/after-install
#!/bin/bash

rbenv gemset create $RBENV_VERSION global

RBENV_GEMSETS=global gem install bundler
rbenv rehash

bundle install --gemfile ~/Gemfile
rbenv rehash

Rubyをインストール

$ rbenv install 2.2.1
Downloading openssl-1.0.1l.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/b2cf4d48fe5d49f240c61c9e624193a6f232b5ed0baf010681e725963c40d1d4
Installing openssl-1.0.1l...
Installed openssl-1.0.1l to /Users/ryo0301/.rbenv/versions/2.2.1

Downloading ruby-2.2.1.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/5a4de38068eca8919cb087d338c0c2e3d72c9382c804fb27ab746e6c7819ab28
Installing ruby-2.2.1...
Installed ruby-2.2.1 to /Users/ryo0301/.rbenv/versions/2.2.1

Running for 2.2.1: /Users/ryo0301/.rbenv/after-install
created global for 2.2.1
Fetching: bundler-1.8.5.gem (100%)
Successfully installed bundler-1.8.5
Parsing documentation for bundler-1.8.5
Installing ri documentation for bundler-1.8.5
Done installing documentation for bundler after 2 seconds
1 gem installed
Fetching gem metadata from https://rubygems.org/........
Fetching version metadata from https://rubygems.org/..
Installing awesome_print 1.6.1
    :
Installing pry 0.10.1
Using bundler 1.8.5
Bundle complete! 6 Gemfile dependencies, 15 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
5
5
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
5
5