LoginSignup
151
133

More than 3 years have passed since last update.

CentOSにrbenv, Rubyをインストールする

Last updated at Posted at 2017-07-25

はじめに

久しぶりにCentOSへRailsをインストールする機会があったので、rbenv, Rubyインストールの手順をメモしました。

環境
CentOS 7.3.1611

もくじ
1. Git
2. rbenv
3. ruby-build
4. .bash_profile
5. パッケージ
6. ruby
7. エラーと解決方法
8. yumでRubyをインストールする場合の注意点


1. Git

# インストール
$ sudo yum -y install git

# バージョン確認
$ git --version


2. rbenv

# rbenvインストール
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

# ディレクトリ確認
$ ls -d ~/.rbenv


3. ruby-build

# インストール
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

# ディレクトリ確認
$ ls -d ~/.rbenv/plugins/ruby-build


4. .bash_profile

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

# 確認
$ cat ~/.bash_profile

# 反映
$ exec $SHELL --login


5. パッケージ

# rubyのインストールに必要なパッケージをインストール
$ sudo yum -y install bzip2 gcc openssl-devel readline-devel zlib-devel

# バージョン確認
$ bzip2 --version
$ gcc --version
$ yum list installed | grep openssl-devel
$ yum list installed | grep readline-devel
$ yum list installed | grep zlib-devel


6. ruby

# バージョン確認
$ rbenv --version

# インストールできるrubyのバージョンを確認
$ rbenv install --list

# rubyインストール
$ rbenv install <version>

# 切り替え可能なrubyのバージョンを確認
$ rbenv versions

# rubyのバージョンを切り替え
$ rbenv global <version>

# rubyのバージョンが切り替わったことを確認
$ ruby -v


7. エラーと解決方法

不足してるパッケージがあるとrubyのインストールに失敗するため、エラーログを参考にパッケージをインストールする。

bzip2がインストールされていない場合

エラーログ

BUILD FAILED (CentOS Linux 7 using ruby-build 20170523-29-gdb3e43a)

Inspect or clean up the working tree at /tmp/ruby-build.20170725093106.1536
Results logged to /tmp/ruby-build.20170725093106.1536.log

Last 10 log lines:
/tmp/ruby-build.20170725093106.1536 ~
warning: bzip2 not found; consider installing `bzip2` package
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

解決方法

# インストール
$ sudo yum -y install bzip2

# バージョン確認
$ bzip2 --version


gccがインストールされてない場合

エラーログ

BUILD FAILED (CentOS Linux 7 using ruby-build 20170523-29-gdb3e43a)

Inspect or clean up the working tree at /tmp/ruby-build.20170725093413.1634
Results logged to /tmp/ruby-build.20170725093413.1634.log

Last 10 log lines:
checking for ruby... false
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20170725093413.1634/ruby-2.4.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

解決方法

# インストール
$ sudo yum -y install gcc

# バージョン確認
$ gcc --version


openssl-devel, readline-devel, zlib-devel がインストールされてない場合

エラーログ

BUILD FAILED (CentOS Linux 7 using ruby-build 20170523-29-gdb3e43a)

Inspect or clean up the working tree at /tmp/ruby-build.20170725093816.1958
Results logged to /tmp/ruby-build.20170725093816.1958.log

Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.
Configure options used:
  --prefix=/home/xxx/.rbenv/versions/2.4.1
  LDFLAGS=-L/home/xxx/.rbenv/versions/2.4.1/lib 
  CPPFLAGS=-I/home/xxx/.rbenv/versions/2.4.1/include

解決方法

# インストール
$ sudo yum install -y openssl-devel readline-devel zlib-devel

# バージョン確認
$ yum list installed | grep openssl-devel
$ yum list installed | grep readline-devel
$ yum list installed | grep zlib-devel


make がインストールされてない場合

解決方法

# インストール
$ sudo yum install make

※ 本項は @Inp さんにコメント欄で補足いただき追記させていただきました


8. yumでRubyをインストールする場合の注意点

yumでもrubyをインストールできるが、少し古いバージョンがインストールされるため注意が必要

執筆時点ではインストールされるのがruby 2.0.0p648となっており、Rails 5が要求するバージョン(2.2.2以上)を満たせない

# インストールされるバージョンを確認
$ yum list installed | grep ruby.x86_64

# インストール
$ sudo yum install -y ruby

# バージョン確認
$ ruby -v
151
133
6

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
151
133