LoginSignup
15
16

More than 5 years have passed since last update.

[初心者向け] Vagrantで構築した仮想環境にrbenvを利用してRubyをインストール

Posted at

1. 概要

Vagrantで構築した仮想環境(CentOS 6.4)に、
rbenvを利用してRubyをインストールします。

※仮想環境の構築方法はこちら
[初心者向け] VirtualBoxとVagrantを利用して、MacOS X上にCentOSの仮想環境を構築

2. 仮想環境にログイン

Vagrantで構築した仮想環境に、SSH接続を行いログインします。

仮想環境ログインコマンド
$ vagrant ssh

3. git のインストール

rbenvのインストールに必要な git をインストールします。
git の説明は省きます。

git インストールコマンド
$ sudo yum -y install git

4. ファイアウォールの停止

仮想環境において作業を行う際は不要なファイアウォールを停止させます。

ファイアウォール停止コマンド (CentOS 6以下)
$ sudo service iptables stop
$ sudo chkconfig iptables off

5. rbenvのインストール

rbenv は、Rubyのバージョンを簡単に切り替えて使う環境を提供してくれるツールです。

rbenvインストールコマンド
1. $ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
2. $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
3. $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
4. $ source ~/.bash_profile

rbenvバージョン確認コマンド (正常にインストールされたか確認)
$ rbenv --version

実行結果
rbenv 0.4.0-138-g4d0c289

6. ruby-buildのインストール

ruby-build は、rbenvRuby をインストールする際に必要な rbenv のプラグインです。

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

7. rbenvを利用してRubyのインストール

ruby-build で提供されているrbenv installコマンドを利用して Ruby をインストールします。

7.1 インストール可能なRubyバージョンの確認

rbenv で用意している Ruby のバージョンを確認します。

インストール可能なRubyバージョン表示コマンド
$ rbenv install -l

実行結果
Available versions:
1.8.6-p383
1.8.6-p420
1.8.7-p249
1.8.7-p302
1.8.7-p334
1.8.7-p352
1.8.7-p357
1.8.7-p358
1.8.7-p370
1.8.7-p371
1.8.7-p374
1.8.7-p375
1.9.1-p378
1.9.1-p430
1.9.2-p0
1.9.2-p180
1.9.2-p290
1.9.2-p318
1.9.2-p320
1.9.2-p326
1.9.2-p330
1.9.3-dev
1.9.3-preview1
1.9.3-rc1
1.9.3-p0
1.9.3-p125
1.9.3-p194
1.9.3-p286
1.9.3-p327
1.9.3-p362
1.9.3-p374
1.9.3-p385
1.9.3-p392
1.9.3-p429
1.9.3-p448
1.9.3-p484
1.9.3-p545
1.9.3-p547
1.9.3-p550
1.9.3-p551
2.0.0-dev
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-p451
2.0.0-p481
2.0.0-p576
2.0.0-p594
2.0.0-p598
2.0.0-p643
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.0
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.2.0-dev
2.2.0-preview1
2.2.0-preview2
2.2.0-rc1
2.2.0
2.3.0-dev
jruby-1.5.6
jruby-1.6.3
jruby-1.6.4
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby-1.6.7.2
jruby-1.6.8
jruby-1.7.0-preview1
jruby-1.7.0-preview2
jruby-1.7.0-rc1
jruby-1.7.0-rc2
jruby-1.7.0
jruby-1.7.1
jruby-1.7.2
jruby-1.7.3
jruby-1.7.4
jruby-1.7.5
jruby-1.7.6
jruby-1.7.7
jruby-1.7.8
jruby-1.7.9
jruby-1.7.10
jruby-1.7.11
jruby-1.7.12
jruby-1.7.13
jruby-1.7.14
jruby-1.7.15
jruby-1.7.16
jruby-1.7.16.1
jruby-1.7.16.2
jruby-1.7.17
jruby-1.7.18
jruby-1.7.19
jruby-9.0.0.0-dev
jruby-9.0.0.0+graal-dev
jruby-9.0.0.0.pre1
maglev-1.0.0
maglev-1.1.0-dev
maglev-2.0.0-dev
mruby-dev
mruby-1.0.0
mruby-1.1.0
rbx-1.2.4
rbx-2.0.0-dev
rbx-2.0.0-rc1
rbx-2.0.0
rbx-2.1.0
rbx-2.1.1
rbx-2.2.0
rbx-2.2.1
rbx-2.2.2
rbx-2.2.3
rbx-2.2.4
rbx-2.2.5
rbx-2.2.6
rbx-2.2.7
rbx-2.2.9
rbx-2.2.10
rbx-2.3.0
rbx-2.4.0
rbx-2.4.1
rbx-2.5.0
rbx-2.5.1
rbx-2.5.2
ree-1.8.6-2009.06
ree-1.8.7-2009.09
ree-1.8.7-2009.10
ree-1.8.7-2010.01
ree-1.8.7-2010.02
ree-1.8.7-2011.03
ree-1.8.7-2011.12
ree-1.8.7-2012.01
ree-1.8.7-2012.02
topaz-dev

7.2 バージョンを選択してRubyのインストール

インストール可能な Ruby のバージョンから、お好みのバージョンを選択して Ruby をインストール

今回は、バージョン 2.1.3 をインストールします。

Ruby インストールコマンド
$ rbenv install 2.1.3

実行結果(エラー)
```Downloading ruby-2.1.3.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/0818beb7b10ce9a058cd21d85cfe1dcd233e98b7342d32e9a5d4bebe98347f01
Installing ruby-2.1.3...

BUILD FAILED (CentOS release 6.4 (Final) using ruby-build 20150226)

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

Last 10 log lines:
rake 10.1.0
rdoc 4.1.0
test-unit 2.1.3.0
installing rdoc: /home/vagrant/.rbenv/versions/2.1.3/share/ri/2.1.0/system
installing capi-docs: /home/vagrant/.rbenv/versions/2.1.3/share/doc/ruby
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?
Configure options used:
--prefix=/home/vagrant/.rbenv/versions/2.1.3
LDFLAGS=-L/home/vagrant/.rbenv/versions/2.1.3/lib
CPPFLAGS=-I/home/vagrant/.rbenv/versions/2.1.3/include
```

上記のようなエラーが出た場合は、OpenSSLライブラリをインストールする。

OpenSSLライブラリのインストールコマンド
sudo yum -y install openssl-devel

インストール済みで利用可能なRubyバージョン確認コマンド (正常にインストールされたか確認)
$ rbenv versions

実行結果
* 2.1.3 (set by /home/vagrant/.rbenv/version)

8. 使用するRubyのバージョンを設定

利用可能なRubyバージョンから、デフォルトで設定するRubyバージョンを選択して設定する。

Ruby バージョン設定コマンド
rbenv global 2.1.3

設定Rubyバージョン確認コマンド (正常に設定されたか確認)
$ ruby -v

実行結果
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

以上で、仮想環境にて Ruby 2.1.3 を扱うことができるようになります。

※ 本稿作成時の最新安定版 2.2.0 をインストールしようとすると、なぜかエラーになる。。。

15
16
3

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
15
16