fugafuga.write

日々のログ

rbenv を開発環境に導入する

Ruby のバージョン管理ツールを導入します。


f:id:tokoyax:20150227191004j:plain


環境

Mac OSX
Cent OS 6.5 (Virtual Box)

目的

rbenv を開発環境に導入する

Vagrant インストール

ローカル開発環境

導入方法はこちらVagrant で開発環境構築 (Mac OSX) - fugafuga.write()


以降の手順は、root で作業します。

rbenv インストール

Ruby のバージョンを管理するツール

ここから持ってくる。
sstephenson/rbenv · GitHub

Githubページのインストール手順は、各ユーザー毎となっているが、
システムワイド(だれでも使える)にインストールする方が後々都合が良いらしいのでそうします。

rbenvグループを作成しておく。
rbenv ディレクトリのグループが root のままだとローカルインストールした時にパーミッションエラーが発生する。

# groupadd rbenv


rbenv をインストール

# pwd
/usr/local
# git clone https://github.com/sstephenson/rbenv.git
# mkdir /usr/local/rbenv/plugins
# chgrp -R rbenv rbenv/ 
# chmod -R g+rwxXs rbenv/


ruby-build インストール

Rubyのビルドを行うツール。

ここから持ってくる。
sstephenson/ruby-build · GitHub

rbenv のプラグインとしてインストールする。

# pwd
/usr/local/rbenv/plugins
# git clone https://github.com/sstephenson/ruby-build.git


ruby-binstubs インストール

後で Bundler を使用するので入れておきます。
bundle exec ~ を省略するためのプラグイン。

ここから持ってくる
ianheggie/rbenv-binstubs · GitHub

# pwd
/usr/local/rbenv/plugins
# git clone https://github.com/ianheggie/rbenv-binstubs.git


rbenv-gem-rehash

rbenv で gem をインストールした場合、シンボリックリンクの更新のために、
rbenv rehash をする必要がありますが、めんどくさいので勝手にやってもらうツール。

(2015/08/26 更新)
以下のpullrequestがmergeされ、自動的に rehash されるようになったので、プラグインは不要となりました!!github.com

# pwd
/usr/local/rbenv/plugins
# git clone https://github.com/sstephenson/rbenv-gem-rehash.git


rbenv.sh 作成

rbenv をシステムワイドでインストールしたため、
RBENV_ROOT などのパスをシェルに知らせてやる必要があります。
そのための設定をシェルで書いて、/etc/profile.d に置いておきます。

# pwd
/etc/profile.d
# cat rbenv.sh
export RBENV_ROOT="/usr/local/rbenv"
export PATH="/usr/local/rbenv/bin:$PATH"
eval "$(rbenv init -)"

設定をリロードします。

# source rbenv.sh

rbenv のパスが通っているか確認します。

# which rbenv
/usr/local/rbenv/bin/rbenv

パスが表示されればOK!

Ruby インストール

rbenv を使ってインストールします。
インストールしたいバージョンのリストアップ。

# rbenv install -l
Available versions:
  1.8.6-p383
  1.8.6-p420
  1.8.7-p249
  1.8.7-p302
  1.8.7-p334
…

今回は 2.2.0 を入れます。

インストールの前に、必要なライブラリをインストールしておきます。
無いとインストール途中でこけます。

# yum -y install libffi libffi-devel zlib zlib-devel openssl openssl-devel readline-devel

ruby のインストール

# rbenv install 2.2.0
Downloading ruby-2.2.0.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/7671e394abfb5d262fbcd3b27a71bf78737c7e9347fa21c39e58b0bb9c4840fc
Installing ruby-2.2.0...
Installed ruby-2.2.0 to /usr/local/rbenv/versions/2.2.0

インストール済み Ruby のバージョンをリスト表示。

# rbenv versions
  2.2.0

バージョンを選択する。

# rbenv global 2.2.0
# rbenv versions
* 2.2.0 (set by /usr/local/rbenv/version)


※ディレクトリ毎にバージョンを選択する場合は、rbenv local 2.2.0 とする。

ruby のバージョン確認

# ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]

OK!

rbenv install で発生したエラー

libffi が無い

# rbenv install 2.2.0
Downloading ruby-2.2.0.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/7671e394abfb5d262fbcd3b27a71bf78737c7e9347fa21c39e58b0bb9c4840fc
Installing ruby-2.2.0...

BUILD FAILED (CentOS release 6.5 (Final) using ruby-build 20150130-2-g8106d8f)

Inspect or clean up the working tree at /tmp/ruby-build.20150222101106.7896
Results logged to /tmp/ruby-build.20150222101106.7896.log

Last 10 log lines:
make[3]: Leaving directory `/tmp/ruby-build.20150222101106.7896/ruby-2.2.0/ext/fiddle/libffi-3.2.1'
linking shared-object fiddle.so
/usr/bin/ld: ./libffi-3.2.1/.libs/libffi.a(raw_api.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
./libffi-3.2.1/.libs/libffi.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[2]: *** [../../.ext/x86_64-linux/fiddle.so] Error 1
make[2]: Leaving directory `/tmp/ruby-build.20150222101106.7896/ruby-2.2.0/ext/fiddle'
make[1]: *** [ext/fiddle/all] Error 2
make[1]: Leaving directory `/tmp/ruby-build.20150222101106.7896/ruby-2.2.0'
make: *** [build-ext] Error 2

こけました。
調べてみると、Ruby 2.2.0 をインストールする際に、libffi-dev が必要らしい。
なので、入れる

# yum -y install libffi libffi-devel

zlib openssl が無い

# rbenv install 2.2.0
Downloading ruby-2.2.0.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/7671e394abfb5d262fbcd3b27a71bf78737c7e9347fa21c39e58b0bb9c4840fc
Installing ruby-2.2.0...

BUILD FAILED (CentOS release 6.5 (Final) using ruby-build 20150130-2-g8106d8f)

Inspect or clean up the working tree at /tmp/ruby-build.20150222103145.20022
Results logged to /tmp/ruby-build.20150222103145.20022.log

Last 10 log lines:
	from /tmp/ruby-build.20150222103145.20022/ruby-2.2.0/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /tmp/ruby-build.20150222103145.20022/ruby-2.2.0/lib/rubygems.rb:556:in `install'
	from ./tool/rbinstall.rb:717:in `block (2 levels) in <main>'
	from ./tool/rbinstall.rb:716:in `each'
	from ./tool/rbinstall.rb:716:in `block in <main>'
	from ./tool/rbinstall.rb:749:in `call'
	from ./tool/rbinstall.rb:749:in `block in <main>'
	from ./tool/rbinstall.rb:746:in `each'
	from ./tool/rbinstall.rb:746:in `<main>'
make: *** [do-install-all] Error 1

エラー。
今度は、`require': cannot load such file -- zlib (LoadError) となっております。
zlib が必要みたいです。
他にも必要なライブラリがあるのか調べたらどうやら、openssl も必要のよう。
yum で入れます。

# yum -y install zlib zlib-devel openssl openssl-devel

お世話になった人

f:id:tokoyax:20150222210336j:plain

rbenv の作者
かっこいい


パーフェクトRuby on Rails

パーフェクトRuby on Rails

Ruby on Rails 4アプリケーションプログラミング

Ruby on Rails 4アプリケーションプログラミング

実践Ruby on Rails 4 現場のプロから学ぶ本格Webプログラミング

実践Ruby on Rails 4 現場のプロから学ぶ本格Webプログラミング



[参考]
rbenvは何をしているのか? - ザリガニが見ていた...。
http://www.atmarkit.co.jp/ait/articles/1402/28/news047.htmlrbenv を利用した Ruby 環境の構築 | Developers.IO
centos7にruby2.2.0-preview2とRails4.2beta4をインストールする - Qiita