Edited at

CentOSにRuby環境を構築する

More than 3 years have passed since last update.


CentOS上にRuby環境を構築する


rbenvの導入


  • rbenvをGitHubから取得してくるので、事前にgitをインストールしておく

# yum install -y git

Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
base | 3.7 kB 00:00
base/primary_db | 4.4 MB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 5.4 MB 00:01
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.7.1-3.el6_4.1 will be installed
--> Processing Dependency: perl-Git = 1.7.1-3.el6_4.1 for package: git-1.7.1-3.el6_4.1.x86_64
--> Processing Dependency: perl(Git) for package: git-1.7.1-3.el6_4.1.x86_64
--> Processing Dependency: perl(Error) for package: git-1.7.1-3.el6_4.1.x86_64
--> Running transaction check
---> Package perl-Error.noarch 1:0.17015-4.el6 will be installed
---> Package perl-Git.noarch 0:1.7.1-3.el6_4.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================
Package Arch Version Repository Size
===============================================================================================
Installing:
git x86_64 1.7.1-3.el6_4.1 base 4.6 M
Installing for dependencies:
perl-Error noarch 1:0.17015-4.el6 base 29 k
perl-Git noarch 1.7.1-3.el6_4.1 base 28 k

Transaction Summary
===============================================================================================
Install 3 Package(s)

Total download size: 4.7 M
Installed size: 15 M
Downloading Packages:
(1/3): git-1.7.1-3.el6_4.1.x86_64.rpm | 4.6 MB 00:00
(2/3): perl-Error-0.17015-4.el6.noarch.rpm | 29 kB 00:00
(3/3): perl-Git-1.7.1-3.el6_4.1.noarch.rpm | 28 kB 00:00
-----------------------------------------------------------------------------------------------
Total 4.2 MB/s | 4.7 MB 00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 1:perl-Error-0.17015-4.el6.noarch 1/3
Installing : perl-Git-1.7.1-3.el6_4.1.noarch 2/3
Installing : git-1.7.1-3.el6_4.1.x86_64 3/3
Verifying : git-1.7.1-3.el6_4.1.x86_64 1/3
Verifying : perl-Git-1.7.1-3.el6_4.1.noarch 2/3
Verifying : 1:perl-Error-0.17015-4.el6.noarch 3/3

Installed:
git.x86_64 0:1.7.1-3.el6_4.1

Dependency Installed:
perl-Error.noarch 1:0.17015-4.el6 perl-Git.noarch 0:1.7.1-3.el6_4.1

Complete!
#
# git --version
git version 1.7.1


  • GitHubからrbenvをクローンする

# cd /opt

# git clone git://github.com/sstephenson/rbenv.git
Initialized empty Git repository in /opt/rbenv/.git/
remote: Counting objects: 2002, done.
remote: Total 2002 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (2002/2002), 318.89 KiB | 172 KiB/s, done.
Resolving deltas: 100% (1249/1249), done.


  • 「rbenvへのパス」と「自動補完とshimsの有効化」のために~/.bash_profileに以下を追記する

# diff -u /tmp/.bash_profile.org ~/.bash_profile

--- /tmp/.bash_profile.org 2009-05-20 19:45:02.000000000 +0900
+++ /root/.bash_profile 2014-10-23 19:54:18.456294353 +0900
@@ -9,4 +9,10 @@

PATH=$PATH:$HOME/bin

+# For rbenv
+
+export RBENV_ROOT="/opt/rbenv"
+export PATH="${RBENV_ROOT}/bin:${PATH}"
+eval "$(rbenv init -)"
+
export PATH


~/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

# For rbenv

export RBENV_ROOT="/opt/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"

export PATH




  • ~/.bash_profileを再読み込みして、パスが通っていることを確認する

# . ~/.bash_profile

# type rbenv
rbenv is a function
rbenv ()
{
local command;
command="$1";
if [ "$#" -gt 0 ]; then
shift;
fi;
case "$command" in
rehash | shell)
eval "`rbenv "sh-$command" "$@"`"
;;
*)
command rbenv "$command" "$@"
;;
esac
}


ruby-buildの導入


  • ruby-buildはrbenvのプラグインの1つ

  • GitHubからruby-buildをクローンする

# mkdir /opt/rbenv/plugins

# cd /opt/rbenv/plugins
# git clone git://github.com/sstephenson/ruby-build.git
Initialized empty Git repository in /opt/rbenv/plugins/ruby-build/.git/
remote: Counting objects: 4015, done.
remote: Compressing objects: 100% (143/143), done.
remote: Total 4015 (delta 66), reused 0 (delta 0)
Receiving objects: 100% (4015/4015), 705.38 KiB | 355 KiB/s, done.
Resolving deltas: 100% (1990/1990), done.


Rubyの導入


  • 事前にgcc、make、openssl-develをインストールする

# yum install gcc make openssl-devel

読み込んだプラグイン:fastestmirror, refresh-packagekit, security
インストール処理の設定をしています
Loading mirror speeds from cached hostfile
パッケージ gcc-4.4.7-11.el6.x86_64 はインストール済みか最新バージョンです
パッケージ 1:make-3.81-20.el6.x86_64 はインストール済みか最新バージョンです
パッケージ openssl-devel-1.0.1e-30.el6_6.2.x86_64 はインストール済みか最新バージョンです
何もしません


  • インストール可能なRubyのバージョンを確認する

# rbenv install -l

Available versions:
(省略)
2.1.0
2.1.1
2.1.2
2.1.3
2.1.4
2.2.0-dev
2.2.0-preview1
jruby-1.5.6
(省略)


  • Rubyをインストールする

# rbenv install 2.1.4

Downloading ruby-2.1.4.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/bf9952cdeb3a0c6a5a27745c9b4c0e5e264e92b669b2b08efb363f5156549204
Installing ruby-2.1.4...
Installed ruby-2.1.4 to /opt/rbenv/versions/2.1.4


  • 普段使うデフォルトの Ruby に設定する

# rbenv global 2.1.4

# ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]