rbenvを使ったrubyのインストール

事前準備

sudo yum -y install bzip2 git gcc
sudo yum -y install openssl-devel readline-devel zlib-devel

rbenvおよびpluginインストール

sudo git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv
sudo git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
sudo git clone https://github.com/sstephenson/rbenv-default-gems.git /usr/local/rbenv/plugins/rbenv-default-gems
sudo git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update
chgrp -R root /usr/local/rbenv
chmod -R u+Xs /usr/local/rbenv
chmod -R +X /usr/local/rbenv

/etc/sudoersを編集する。

sudo cat <<EOF | sudo tee /etc/sudoers.d/rbenv
Defaults    env_keep += "RBENV_ROOT"
EOF
sudo chmod 644 /etc/sudoers.d/rbenv
sudo sed --in-place 's|.*Default.*secure_path.*|Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rbenv/bin:/usr/local/rbenv/shims|g' /etc/sudoers

rbenv の使用準備

sudo rbenv init -

全ユーザの環境設定

sudo cat <<EOF | sudo tee /etc/profile.d/rbenv.sh
export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init --no-rehash -)"
EOF
sudo chmod 644 /etc/profile.d/rbenv.sh
source /etc/profile

gemのinstall

sudo yum -y install rubygems
sudo apt-get -y install rubygems
sudo cat <<EOF | sudo tee /usr/local/rbenv/default-gems
json_pure
bundler
pry
rbenv-rehash
EOF
sudo chmod 644 /usr/local/rbenv/default-gems

rubyのインストール

rbenv install --list
sudo rbenv install 2.4.0

システムのRubyを設定

sudo rbenv global 2.4.0

gemのupdate

sudo gem update --system

gemの環境確認

gem env

シェルスクリプトで上記全てを実行する場合

echo "### preparation before installation"
sudo yum -y install bzip2 git gcc
sudo yum -y install openssl-devel readline-devel zlib-devel

echo "### install rbenv from git"
sudo git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv
sudo git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
sudo git clone https://github.com/sstephenson/rbenv-default-gems.git /usr/local/rbenv/plugins/rbenv-default-gems
sudo git clone https://github.com/rkh/rbenv-update.git ~/.rbenv/plugins/rbenv-update

echo "### create rbenv configuraiton"
sudo chgrp -R root /usr/local/rbenv
sudo chmod -R u+Xs /usr/local/rbenv
sudo chmod -R +X /usr/local/rbenv
sudo cat <<EOF | sudo tee /etc/sudoers.d/rbenv
Defaults    env_keep += "RBENV_ROOT"
EOF
sudo chmod 644 /etc/sudoers.d/rbenv
sudo sed --in-place 's|.*Default.*secure_path.*|Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rbenv/bin:/usr/local/rbenv/shims|g' /etc/sudoers
sudo rbenv init -
sudo cat <<EOF | sudo tee /etc/profile.d/rbenv.sh
export RBENV_ROOT=/usr/local/rbenv
export PATH="\$RBENV_ROOT/bin:\$PATH"
eval "\$(rbenv init --no-rehash -)"
EOF
sudo chmod 644 /etc/profile.d/rbenv.sh
source /etc/profile

echo "### install ruby gem environment"
sudo yum -y install rubygems
sudo cat <<EOF | sudo tee /usr/local/rbenv/default-gems
json_pure
bundler
pry
rbenv-rehash
EOF
sudo chmod 644 /usr/local/rbenv/default-gems

echo "### ruby"
rbenv install --list
sudo rbenv install 2.4.0
sudo rbenv global 2.4.0
sudo gem update --system
gem env