LoginSignup
72
71

More than 5 years have passed since last update.

CentOS 6.6 に rbenvを利用した Ruby 2.2.0 + Ruby on Rails 4.2.0 環境構築

Last updated at Posted at 2015-01-20

Ruby, Ruby on Rails の環境構築

ちょいと Ruby 関係のアレをしないといけないので久しぶりに環境を構築してみました。また検証も兼ねてなので rbenv を使用し異なるrubyを使い分けられるような環境を整えます。

ソフトウェア バージョン
rbenv 0.4.0-129-g7e0e85b
Ruby 2.2.0p0
Rails 4.2.0

必要なパッケージのインストール

git やもろもろインスコする必要がありますので初めにインスコします。
bash でインストール作業してますが zsh でも同様にいけるはずです。

$ sudo yum install zlib zlib-devel openssl-devel sqlite-devel gcc-c++ glibc-headers libyaml-devel readline readline-devel zlib-devel libffi-devel

git から rbenv, ruby-build を clone

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

.bash_profile に以下を追加し source .bash_profile を実行。

.bash_profile
export PATH=$PATH:$HOME/.rbenv/bin:$HOME/.rbenv/shims

rbenv が正しく実行できるか確認

$ . ~/.bash_profile
$ rbenv -v
rbenv 0.4.0-129-g7e0e85b

Ruby のインストール

rbenv でインストールできる ruby のバージョンを確認する。2.x系の安定版は、2015/1/20時点で 2.2.0 なのでインストールする(結構、時間がかかる)。

$ rbenv install --list
$ rbenv install 2.2.0

エラーでインストールできない場合、Results logged で示されているlogの内容を確認する。わたしは、libffi.a が無いよというエラーでしたので素直に sudo yum install libffi-devel をインストールし問題解決しました。

Last 10 log lines:
make[3]: Leaving directory `/tmp/ruby-build.20150119155252.15788/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

インストールした ruby に環境を切り替える

上記 rbenv でインストールした ruby に環境を切り替えます。
下記の様にならない場合は、環境変数の定義を見直すこと。

$ rbenv rehash
$ ruby -v
rbenv: version `system' is not installed
$ rbenv global 2.2.0
$ rbenv version
2.2.0 (set by /home/vagrant/.rbenv/version)
$ ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]

Ruby on Rails のインストール

あと少しw

$ gem update --system
$ gem install --no-ri --no-rdoc rails
$ gem install bundler
$ rbenv rehash

バージョンを確認する

$ rails -v
Rails 4.2.0

WEBrick を起動する

Ruby on Rails サンプルアプリを作成し WEBrick の起動ができるか確認する。

$ rails new sample --skip-bundle
$ cd sample
$ bundle install

WEBrick の起動でエラーがでるよ
※JavaScriptのランタイムが必要だというエラー表示されるので、node.js をインストール

$ rails s 
/home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/execjs-2.2.2/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

node.js をインストールし、いつもの画面が表示出来ていれば問題なし。

$ sudo yum install nodejs npm –enablerepo=epel
$ rails s

Vagrant環境の WEBrick にアクセスできない

明示的に port, ip を指定する。

$ rails s -p 3000 -b 192.168.11.206

参考にさせて頂いたサイト

rails環境構築(CentOS + ruby on rails)
CentOSにRuby on Railsの環境構築とか
rbenvで複数のバージョンのrubyを使用する環境を整える

72
71
3

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
72
71