LoginSignup
6
5

More than 5 years have passed since last update.

CentOS7でrbenvを使ってRubyをインストールできるようにする

Last updated at Posted at 2016-08-25

発端と概要

/etc/profile を直接編集するとか、そういうやばそうな手順が含まれるもが散見されたので、ちゃんとCentOS7の流儀に従ったやり方でインストールたいです。

etc/profile と /etc/bashrcに書いてあること

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

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

rbenvのインストール~rbenv installで必要になるもののインストールをします。

yum -y install git gcc make openssl-devel libffi-devel readline-devel

それぞれ、以下のタイミングで必要になります。

ライブラリ 使用用途
git rbenvとruby-buildのインストール
他のもの ruby-buildで使う

rbenvとかruby-buildのインストール

/opt/rbenv にインストールします。他の場所(例えば/usr/localとか)にしたい場合は、適宜読み替えてください。

# rbenv
cd /opt
git clone https://github.com/rbenv/rbenv.git
# ruby-build
mkdir /opt/rbenv/plugins
cd /opt/rbenv/plugins
git clone https://github.com/rbenv/ruby-build.git
# ログイン時にrbenvを使えるようにする初期化スクリプトを作る
cat << 'EOS' > /etc/profile.d/rbenv.sh
export RBENV_ROOT="/opt/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
EOS
# 環境変数とかを変更したので、ログインシェルをリロードする
exec $SHELL -l

Rubyのインストールする

2.3.1をインストールして、システム全体で使えるようにします。
install で時間が掛かります。

rbenv install 2.3.1
rbenv global 2.3.1

確認

ruby --version

参考文献

「CentOS/RHEL に Ruby 2.x をインストールする」は、「最後に /etc/profile に以下の3行を追加」しやがりました。
で、それを真に受けてしまった記事が
ruby2.x.xのインストール方法

「CentOS に Ruby をインストールする(kzy52's blog)」 は、丁寧で良い記事だと思います。

設定の反映で、source で直で読み込んでるのが、動作確認の意味も含めたい私としては、危なそうに感じました。

6
5
0

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
6
5