【Rails】CentOSにRailsを入れよう

CentOS 7(AWSインスタンス)にRails入れるのに大いに時間がかかったのでメモっておきます。

こちら参考にさせていただきました。
rbenv を利用した Ruby 環境の構築 | Developers.IO

まず、gitが入ってる前提とのことなのでyumでインストールします。

$ sudo yum -y install git

次にrbenvを入れます。以下のコマンドを実行します。

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
 
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
 
$ source ~/.bash_profile
$ rbenv --version
rbenv 1.0.0-19-g29b4da7

※rbenvのクローン先は~/.rbenv推奨です。デフォルトのrootだそうなので。
ここまではサクサクすすみました。
rbenvが入ったので、Rubyをインストールします。

インストールできるRubyのバージョンを確認して、

$ rbenv install --list

2.2.4が見つかったのでインストールします。

$ rbenv install 2.2.4

Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
/home/centos/.rbenv/plugins/ruby-build/bin/ruby-build: 209 行: pushd: ruby-2.2.4: そのようなファイルやディレクトリはありません

BUILD FAILED (CentOS Linux 7 using ruby-build 20160228-5-g522f3ba)

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

Last 10 log lines:
/tmp/ruby-build.20160315122604.9219 ~
warning: bzip2 not found; consider installing `bzip2` package
tar (child): bzip2: exec 不能: そのようなファイルやディレクトリはありません
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

ログ見ると、
bzip2 not found; consider installing bzip2 package
とおっしゃってますのでインストールします。

$ sudo yum -y install bzip2

再度、インストールを試します。

$ rbenv install 2.2.4

Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
Installing ruby-2.2.4...

BUILD FAILED (CentOS Linux 7 using ruby-build 20160228-5-g522f3ba)

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

Last 10 log lines:
/tmp/ruby-build.20160315122847.9303/ruby-2.2.4 /tmp/ruby-build.20160315122847.9303 ~
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20160315122847.9303/ruby-2.2.4':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

error: no acceptable C compiler found in $PATH
今度はコンパイラにパス通って無いよって言ってます。
そりゃコンパイラ入れてないですからね。
最初から入れといてよと思いましたが、情弱っぽいのでぐっと我慢してgcc入れます。

$ sudo yum -y install gcc

gccが入ったので今度こそ!

$ rbenv install 2.2.4

Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
Installing ruby-2.2.4...

BUILD FAILED (CentOS Linux 7 using ruby-build 20160228-5-g522f3ba)

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

Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.

Configure options used:
  --prefix=/home/centos/.rbenv/versions/2.2.4
  LDFLAGS=-L/home/centos/.rbenv/versions/2.2.4/lib
  CPPFLAGS=-I/home/centos/.rbenv/versions/2.2.4/include

Try running yum install -y openssl-devel readline-devel zlib-devel to fetch missing dependencies.
はいはい。

$ sudo yum install -y openssl-devel readline-devel zlib-devel

再々度インストール!

$ rbenv install 2.2.4

Downloading ruby-2.2.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.bz2
Installing ruby-2.2.4...
Installed ruby-2.2.4 to /home/centos/.rbenv/versions/2.2.4

やっと入りました。
バージョン指定して、ちゃんと入ってるか確認します。

$ rbenv global 2.2.4
$ ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]

完璧ですね。
次にbundlerとRails入れましょう。

$ gem install bundle
$ gem install rails

ここは流石に簡単に入りますね。
ではアプリをcloneしてきて、bundle installしてみましょう。
※アプリのcloneは省略

$ bundle install 

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/centos/.rbenv/versions/2.2.4/bin/ruby -r ./siteconf20160315-3608-feu520.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/centos/.rbenv/versions/2.2.4/bin/$(RUBY_BASE_NAME)
    --with-sqlite3-dir
    --without-sqlite3-dir
    --with-sqlite3-include
    --without-sqlite3-include=${sqlite3-dir}/include
    --with-sqlite3-lib
    --without-sqlite3-lib=${sqlite3-dir}/lib

extconf failed, exit code 1

Try 'port install sqlite3 +universal','yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
とのことなので以下を実行します。

$ sudo yum -y install sqlite-devel

spliteいれたらbundle installが通りました。 いよいよrails sでサーバーを立ち上げます。

$ rails s

/home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError)
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler.rb:99:in `require'
    from /home/centos/apps/talk_on/config/application.rb:7:in `<top (required)>'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:78:in `require'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:78:in `block in server'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/centos/apps/talk_on/bin/rails:9:in `require'
    from /home/centos/apps/talk_on/bin/rails:9:in `<top (required)>'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/client/rails.rb:28:in `load'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/client/rails.rb:28:in `call'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/client/command.rb:7:in `call'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/client.rb:28:in `run'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/bin/spring:49:in `<top (required)>'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/binstub.rb:11:in `load'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/spring-1.6.4/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/centos/.rbenv/versions/2.2.4/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/centos/apps/talk_on/bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

エラー多すぎて笑えてきた。
こちら参考にさせていただき、rubyracerいれます。 qiita.com

$ vi Gemfile

gem 'therubyracer', platforms: :ruby

gemが増えたのでbundle installし直します。

$ bundle install

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/centos/.rbenv/versions/2.2.4/bin/ruby -r ./siteconf20160315-23889-drnx6o.rb extconf.rb
checking for main() in -lpthread... yes
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling accessor.cc
make: g++: コマンドが見つかりませんでした
make: *** [accessor.o] エラー 127

make failed, exit code 2

g++コマンドがないそうです。
c++コンパイラのようなので以下を実行。

$ sudo yum -y install gcc-c++

ここまででbundle installが通ったので、今度こそサーバー立ち上げます。

$ rails s
INFO  WEBrick 1.3.1
INFO  ruby 2.2.4 (2015-12-16) [x86_64-linux]
INFO  WEBrick::HTTPServer#start: pid=24119 port=3000

ついに立ち上がった!
長々とお付き合いありがとうございました。