未来エンジニア養成所Blog

月単価180万以上のプログラミング講師がプログラミングを皆に楽しんでもらうための情報をお届けします。

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

はじめに

MacOS上にVagrant&VirtualBoxを準備しましたので、そこにRubyをインストールしていきます。

Gitのインストール

まずはGitのインストール。

[vagrant@localhost ~]$ sudo yum -y install git
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror

(途中省略)
  
  perl-threads.x86_64 0:1.87-4.el7                 perl-threads-shared.x86_64 0:1.43-6.el7

Complete!

バージョン確認をします。

[vagrant@localhost ~]$ git --version
git version 1.8.3.1

rbenv

rbenvのインストールをします。

[root@localhost ~]# git clone git://github.com/sstephenson/rbenv.git /usr/local/rbenv
Cloning into '/usr/local/rbenv'...
remote: Enumerating objects: 2759, done.
remote: Total 2759 (delta 0), reused 0 (delta 0), pack-reused 2759
Receiving objects: 100% (2759/2759), 526.02 KiB | 0 bytes/s, done.
Resolving deltas: 100% (1731/1731), done.

ディレクトリを確認します。

[root@localhost ~]# ls -d /usr/local/rbenv
/usr/local/rbenv

大丈夫そうですね。

ruby-build

ruby-buildをインストールします。

[root@localhost ~]# git clone git://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
Cloning into '/usr/local/rbenv/plugins/ruby-build'...
remote: Enumerating objects: 65, done.
remote: Counting objects: 100% (65/65), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 9777 (delta 26), reused 51 (delta 19), pack-reused 9712
Receiving objects: 100% (9777/9777), 2.08 MiB | 1.35 MiB/s, done.
Resolving deltas: 100% (6352/6352), done.

ディレクトリを確認します。

[root@localhost ~]# ls -d /usr/local/rbenv/plugins/ruby-build
/usr/local/rbenv/plugins/ruby-build

.bash_profile

.bash_profileを設定します。

[root@localhost ~]# echo 'export RBENV_ROOT="/usr/local/rbenv"' >> /etc/profile.d/rbenv.sh
[root@localhost ~]# echo 'export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /etc/profile.d/rbenv.sh
[root@localhost ~]# echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh

設定の確認です。

[root@localhost ~]# cat /etc/profile.d/rbenv.sh
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"

設定ファイルを反映させておきます。

[root@localhost ~]# source /etc/profile.d/rbenv.sh

パッケージ

Rubyのインストールに必要なパッケージをインストールします。

[vagrant@localhost ~]$ sudo yum -y install bzip2 gcc openssl-devel readline-devel zlib-devel
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

(途中省略)

Dependency Updated:
  glibc.x86_64 0:2.17-260.el7_6.5             glibc-common.x86_64 0:2.17-260.el7_6.5
  libgcc.x86_64 0:4.8.5-36.el7_6.2            libgomp.x86_64 0:4.8.5-36.el7_6.2
  openssl.x86_64 1:1.0.2k-16.el7_6.1          openssl-libs.x86_64 1:1.0.2k-16.el7_6.1

Complete!

バージョンの確認をします。
- bzip2

[vagrant@localhost ~]$ bzip2 --version
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

   Copyright (C) 1996-2010 by Julian Seward.

   This program is free software; you can redistribute it and/or modify
   it under the terms set out in the LICENSE file, which is included
   in the bzip2-1.0.6 source distribution.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   LICENSE file for more details.

bzip2: I won't write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help'.
  • gcc
[vagrant@localhost ~]$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • openssl
[vagrant@localhost ~]$ yum list installed | grep openssl-devel
openssl-devel.x86_64                1:1.0.2k-16.el7_6.1         @updates
  • readline
[vagrant@localhost ~]$ yum list installed | grep readline-devel
readline-devel.x86_64               6.2-10.el7                  @base
  • zlib
[vagrant@localhost ~]$ yum list installed | grep zlib-devel
zlib-devel.x86_64                   1.2.7-18.el7                @base

Ruby

いよいよRubyのインストールです。 まずはrbenvのバージョンを確認

[vagrant@localhost ~]$ rbenv --version
rbenv 1.1.2-2-g4e92322

インストールできるrubyのバージョンを確認します。

[vagrant@localhost ~]$ rbenv install --list
Available versions:
  1.8.5-p52
  1.8.5-p113
  1.8.5-p114

(途中省略)

  2.6.0-rc1
  2.6.0-rc2
  2.6.0
  2.6.1
  2.6.2
  2.6.3
  2.7.0-dev
  2.7.0-preview1

(途中省略)

  truffleruby-1.0.0-rc8
  truffleruby-1.0.0-rc9
  truffleruby-19.0.0

インストールしたいバージョンを選んで、rubyをインストールします。
今回は 2.6.3 にしました。

[vagrant@localhost ~]$ rbenv install 2.6.3
Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
Installed ruby-2.6.3 to /home/vagrant/.rbenv/versions/2.6.3

rbenvで切り替え可能なバージョンを確認します。
(今回はまだ2.6.3しかインストールしてません)

[vagrant@localhost ~]$ rbenv versions
  2.6.3

Rubyのバージョンを切り替えます。

[vagrant@localhost ~]$ rbenv global 2.6.3

Rubyのバージョンが切り替わったことを確認します。

[vagrant@localhost ~]$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

これで完了です。



オンラインプログラミングスクール
未来エンジニア養成所Logo