CentOS6にRuby on Rails環境を構築する

投稿者: | 2014年6月30日
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

■前提条件

・CentOS release 6.2 (Final)
・Ruby 2.1.2
・Rails 4.1.2
・システムワイドな環境の構築
・root権限付きのアカウントで作業
→ 無用な事故を防ぐために適時sudoに切り替えながらの作業を推奨しますが、CentOS6系のデフォルトだとPATHが引き継がれなかったりしますので要注意です(詳細はsecure_pathでGoogle先生に)。

■事前準備

GitHubからファイルを取得しますので、まずGitが必要です。

[bash gutter=”0″]# yum -y install git[/bash]

依存ライブラリとしてOpenSSLも必要です。

[bash gutter=”0″]# yum -y install openssl-devel[/bash]

このパッケージが足りない場合、rubyのインストール時に

[bash gutter=”0″]
BUILD FAILED
・・・
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?
[/bash]

のように怒られます。

それぞれ、すでにインストールしている場合は飛ばしても構いません。準備が整ったら、インストールを開始します。

■インストール

(1)rbenvをインストールします。

[bash gutter=”0″]
# cd /usr/local
# git clone https://github.com/sstephenson/rbenv.git rbenv
Initialized empty Git repository in /usr/local/rbenv/.git/
remote: Reusing existing pack: 1889, done.
remote: Total 1889 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1889/1889), 297.15 KiB | 183 KiB/s, done.
Resolving deltas: 100% (1182/1182), done.
[/bash]

 

(2)PATHの設定を全ユーザに反映させるため下記のファイルを生成します。

[bash gutter=”0″]
# vi /etc/profile.d/rbenv.sh 
[/bash]

以下の内容を記述します。

[bash]
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
[/bash]

 

(3)保存したら反映させます。

[bash gutter=”0″]
# source /etc/profile.d/rbenv.sh
[/bash]

 

(4)正しくインストールされたかどうか確認してみます。

[bash gutter=”0″]
# rbenv
rbenv 0.4.0-98-g13a474c
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable

See `rbenv help <command>’ for information on a specific command.
For full documentation, see: https://github.com/sstephenson/rbenv#readme
[/bash]

 

(5)ruby-buildをインストールします。

[bash gutter=”0″]
# git clone git://github.com/sstephenson/ruby-build.git rbenv/plugins/ruby-build
Initialized empty Git repository in /usr/local/rbenv/plugins/ruby-build/.git/
remote: Reusing existing pack: 3534, done.
remote: Total 3534 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3534/3534), 595.99 KiB | 244 KiB/s, done.
Resolving deltas: 100% (1703/1703), done.
[/bash]

 

(6)rubyをインストールします。

まず、インストール可能なバージョンを確認してみます。

[bash gutter=”0″]
# rbenv install –list
Available versions:
1.8.6-p383
1.8.6-p420
・・・・
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.1.0
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.1
2.1.2
2.2.0-dev
jruby-1.5.6
・・・・
ree-1.8.7-2012.02
topaz-dev
[/bash]

今回は 2.1.2 を入れてみます。マシンスペックにもよりますが、インストール完了まで少し時間がかかります。

[bash gutter=”0″]
# rbenv install 2.1.2
Downloading ruby-2.1.2.tar.gz…
-> http://dqw8nmjcqpjn7.cloudfront.net/f22a6447811a81f3c808d1c2a5ce3b5f5f0955c68c9a749182feb425589e6635
Installing ruby-2.1.2…
Installed ruby-2.1.2 to /usr/local/rbenv/versions/2.1.2
[/bash]

 

(7)rbenvの管理下にあるrubyバージョンで利用可能なコマンドへのスクリプトを生成します。

[bash gutter=”0″]
# rbenv rehash
[/bash]

rehashは面倒ですので、自動化させるためにrbenv-rehashをインストールしましょう。

[bash gutter=”0″]
# gem install rbenv-rehash
Fetching: rbenv-rehash-0.3.gem (100%)
Successfully installed rbenv-rehash-0.3
Parsing documentation for rbenv-rehash-0.3
Installing ri documentation for rbenv-rehash-0.3
Done installing documentation for rbenv-rehash after 0 seconds
1 gem installed
[/bash]

これで、rubyやgemのインストール毎にrehashを実行する手間を省くことができます。

 

(8)インストールしたバージョンをシステムのデフォルトに設定します。

[bash gutter=”0″]
# rbenv global 2.1.2
[/bash]

 

(9)RDocをインストールします。

[bash gutter=”0″]
# gem install rdoc
Fetching: rdoc-4.1.1.gem (100%)
rdoc’s executable "rdoc" conflicts with /usr/local/rbenv/versions/2.1.2/bin/rdoc
Overwrite the executable? [yN] y
rdoc’s executable "ri" conflicts with /usr/local/rbenv/versions/2.1.2/bin/ri
Overwrite the executable? [yN] y
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data –install
= 1.9.1 : gem install rdoc-data; rdoc-data –install
>= 1.9.2 : nothing to do! Yay!
Successfully installed rdoc-4.1.1
Parsing documentation for rdoc-4.1.1
Installing ri documentation for rdoc-4.1.1
Done installing documentation for rdoc after 18 seconds
1 gem installed
[/bash]

 

(10)Ruby on Railsをインストールします。

マシンスペックにもよりますが、インストール完了まで少し時間がかかります。

[bash gutter=”0″]
# gem install rails
Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
Fetching: minitest-5.3.5.gem (100%)
Successfully installed minitest-5.3.5
Fetching: tzinfo-1.2.1.gem (100%)
Successfully installed tzinfo-1.2.1
Fetching: i18n-0.6.9.gem (100%)
Successfully installed i18n-0.6.9
Fetching: activesupport-4.1.2.gem (100%)
Successfully installed activesupport-4.1.2
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
・・・・
Installing ri documentation for rails-4.1.2
Parsing documentation for railties-4.1.2
Installing ri documentation for railties-4.1.2
Parsing documentation for sprockets-2.12.1
Installing ri documentation for sprockets-2.12.1
Parsing documentation for sprockets-rails-2.1.3
Installing ri documentation for sprockets-rails-2.1.3
Parsing documentation for thor-0.19.1
Installing ri documentation for thor-0.19.1
Parsing documentation for thread_safe-0.3.4
Installing ri documentation for thread_safe-0.3.4
Parsing documentation for tilt-1.4.1
Installing ri documentation for tilt-1.4.1
Parsing documentation for treetop-1.4.15
Installing ri documentation for treetop-1.4.15
Parsing documentation for tzinfo-1.2.1
Installing ri documentation for tzinfo-1.2.1
Done installing documentation for actionmailer, actionpack, actionview, activemodel, activerecord, activesupport, arel, builder, bundler, erubis, hike, i18n, mail, mime-types, minitest, multi_json, polyglot, rack, rack-test, rails, railties, sprockets, sprockets-rails, thor, thread_safe, tilt, treetop, tzinfo after 671 seconds
[/bash]

 

(11)正常にインストールできたようですので、確認してみましょう。

[bash gutter=”0″]
# ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
[/bash]

[bash gutter=”0″]
# rails -v
Rails 4.1.2
[/bash]

これでOKです。


コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください