LoginSignup
29
29

More than 5 years have passed since last update.

Amazon Linuxに rbenv をインストールして、Rubyをバージョンアップ(Bundlerも)

Last updated at Posted at 2017-01-20

はじめに

awspecを使って環境構築後にテストをしようとしたのですが、
Amazon Linuxにawspecをインストールしました。

  • 使ったAmazon Linux:Amazon Linux AMI 2016.09.1 (HVM), SSD Volume Type
  • Amazon LinuxにインストールされていたRubyのバージョン:ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

するとawspecが使えるバージョンが2.0.0は使えないとのこと。

ERROR:  Error installing awspec:
    awspec requires Ruby version >= 2.1.

そのため、rubyもバージョンアップすることにしました。

rbenvとRubyのインストール

Rubyはrbenvを使ってバージョンアップをします。
今回は、2.4.0をインストールしてみます。

$ sudo yum -y install gcc-c++ openssl-devel git readline-devel
$ mkdir ~/.rbenv
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ mkdir ~/.rbenv/plugins ~/.rbenv/plugins/ruby-build
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd ~/.rbenv/plugins/ruby-build
$ sudo ./install.sh
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ rbenv install 2.4.0

これで以下のようなメッセージが出ていればOKです。

Downloading ruby-2.4.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.0.tar.bz2
Installing ruby-2.4.0...
Installed ruby-2.4.0 to /home/ec2-user/.rbenv/versions/2.4.0

あとは、順をおってrubyのバージョンを標準に変えていったりします。

$ rbenv rehash
$ rbenv global 2.4.0

これで標準変更は完了になります。
念のため、Rubyのバージョンを確認します。

$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

これでRubyのバージョンが2.4.0になりました。

Bundlerのインストール

awspecではBundlerも使うので、こちらもこのタイミングでインストールします。

Bundler(2017/01/20時点で1.13.7)もrbenv経由でインストールしていきます。

$ rbenv exec gem install bundler
$ rbenv rehash

こちらも念のため、バージョン確認をします。

$ bundler -v
Bundler version 1.13.7

これで、作業は完了です。

このあと、awspecをインストールして使っていきます。

ちなみに、awspecのインストールは

$ gem install awspec

これで、OKです。

後は、 ~/.aws/credentials にAccessKeyとSecretAccessKeyを書いてください。

[default]
aws_access_key_id = AKIxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

では、最後にawspecコマンドを実行してコマンドがいい感じに実行できるか確認します。

$ awspec generate ec2 vpc-xxxxxxx

※「vpc-xxxxxxx」はEC2があるVPC-IDを指定してください。

するとこんな感じで出力されればOKです。

describe ec2('VPC-VPC') do
  it { should exist }
  it { should be_running }
  its(:instance_id) { should eq 'i-xxxxxxxxxxxxxxxxx' }
  its(:image_id) { should eq 'ami-xxxxxxxx' }
  its(:private_dns_name) { should eq 'ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal' }
  its(:public_dns_name) { should eq 'ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com' }
  its(:instance_type) { should eq 't2.micro' }
  its(:private_ip_address) { should eq 'xxx.xxx.xxx.xxx' }
  its(:public_ip_address) { should eq 'xxx.xxx.xxx.xxx' }
  it { should have_security_group('SecurityGroup-VPC) }
  it { should belong_to_vpc('VPC_VPC') }
  it { should belong_to_subnet('Subnet-VPC') }
  it { should have_eip('xxx.xxx.xxx.xxx') }
  it { should have_ebs('EBS-VPC') }
  it { should have_network_interface('eni-xxxxxxxx') }
end

参考サイト

ちょっとした問題

rbenvを使ってrubyをインストールしたときにエラーが発生しました。
そのため、急遽、「readline-devel」を追加でインストールして、
再度rubyのバージョンアップをしました。
エラーメッセージは以下の通りです。

[ec2-user@ip-xxx-xxx-xxx-xxx ruby-build]$ rbenv install 2.4.0
Downloading ruby-2.4.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.0.tar.bz2
Installing ruby-2.4.0...

BUILD FAILED (Amazon Linux AMI 2016.09 using ruby-build 20170112-2-g3d59394)

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

Last 10 log lines:
installing rdoc:              /home/ec2-user/.rbenv/versions/2.4.0/share/ri/2.4.0/system
installing capi-docs:         /home/ec2-user/.rbenv/versions/2.4.0/share/doc/ruby
The Ruby readline extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y readline-devel` to fetch missing dependencies.

Configure options used:
  --prefix=/home/ec2-user/.rbenv/versions/2.4.0
  LDFLAGS=-L/home/ec2-user/.rbenv/versions/2.4.0/lib 
  CPPFLAGS=-I/home/ec2-user/.rbenv/versions/2.4.0/include 
[ec2-user@ip-xxx-xxx-xxx-xxx ruby-build]$
29
29
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
29
29