RHEL7でRailsアプリのサーバを構築した【Rubyのインストール・設定】

こちらは、

RHEL7でRailsアプリのサーバを構築した - takapiのブログ

Rubyのインストール・設定 に関する記事です。

takapi86.hatenablog.com

インストール済みの Ruby の削除

EC2のRHEL7では、元々インストールはされていませんでしたが、環境によってはデフォルトでRubyが入っている場合があります。 ほとんどの場合は、古いRubyが入っているので、一旦削除してしまいましょう。

$ sudo yum -y remove ruby

gitをインストールする

後ほど、 rbenv,ruby-build といったツールをダウンロードするのに必要です。

$ sudo yum -y install git

rbenvをインストールする

Rubyのバージョンの切り替えなどが簡単にできるツールであるrbenvをインストールします。 以下のサイトを参考にインストールします。

https://github.com/rbenv/rbenv

クローンします。

$ git clone --depth 1 https://github.com/rbenv/rbenv.git

.bash_profile に PATHを通します。

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile で何をしているかは、 こちらを参考にするとよいでしょう。

takatoshiono.hatenablog.com

一度、ログアウトし直すか、以下のコマンドで ~/.bash_profile を読みなおします。

$ source ~/.bash_profile

rbenv コマンドが使えるか確認します。

$ rbenv
rbenv 1.1.1-2-g615f844
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

ruby-buildをインストールする

rbenv の プラグインである ruby-build をインストールします。 実際にRubyコンパイルしてインストールしてくれるものです。

クローンします。

$ git clone --depth 1 https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

もう一度、rbenvコマンドを実行します。

$ rbenv
rbenv 1.1.1-2-g615f844
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
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a 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

ruby-build をクローンする前になかった install,uninstall が表示されていれば成功です。

Ruby2.4.1をインストール

EC2のRHEL7では、Rubyコンパイルに以下のライブラリが必要なのでインストールしておきます。

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

Rubyコンパイル・インストールします。

rbenv install 2.4.1

スペックによりますが、結構時間がかかるので待ちます。

インストールしたRuby2.4.1を使うので、以下のコマンドで使えるようにします。

rbenv global 2.4.1

Rubyがインストールされているか確認する。

ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

以上でRuby のインストールは完了です。