woshidan's blog

あいとゆうきとITと、とっておきの話。

Linuxにrbenvをインストールし、rbenvでRubyを入れ、mysql2のgemを入れるまでのメモ

ちょっとたくさんのインスタンスからMySQLにクエリを投げたいことってありますよね?

というわけで今日はRubyMySQLに接続するコードを書く前段階として表題のEC2上で環境構築の素振りをします。

TL;DR

  • rbenvおよび、rbenvのプラグイン ruby-buildはGitHubからcloneしてくるのでまずgitを入れる
  • exec $SHELL -l で新しく起動中のシェルを実行し、現在のプロセスと入れ替えることで ~/.bash_profile に書いた設定内容(シェルの起動時に読み込み)をその場で反映させられる
  • RubyのインストールにはCコンパイラが必要で、Cコンパイラとしてgccをインストールする
  • -devel がついたパッケージには開発環境で使うオブジェクトやヘッダが含まれていて、C言語で書かれたライブラリをコンパイルする際に必要になることがある
  • mysql2 のgem のインストールには mysql-devel が必要
    • ネイティブエクステンションの部分でgemのインストールが失敗すると長いメッセージが出てきてオプションなどの羅列に目が行きがちだが、まんなかあたりに具体的に何をやればいいか1行で書いてある

rbenvのインストール

https://qiita.com/inouet/items/478f4228dbbcd442bfe8 を参考に作業していきます。

gitをインストールする

$ sudo yum -y install git

rbenvはGitHubからDLしてきます。そのために、Gitクライアントがあると便利なのでgitのインストールを行います。

rbenvをホームディレクトリにDLし、rbenvの実行ファイルへのパスを通す

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

ついでに、シェルを立ち上げた時に rbenv が起動するようにも設定しておきます。

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

~/.bash_profile に書いた設定は次以降にbashを開く時に読み込まれるので

$ exec $SHELL -l

を実行して先ほどまで ~/.bash_profile に追記していた設定をその場で反映させます。

exec $SHELL -l コマンドは、新しく ~/.bash_profile の設定変更が反映されているbash($SHELL の中身)のプロセスを実行して今動いているbashのプロセスと入れ替えます*1

rbenvのプラグインである ruby-build をインストールする

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

rbenvはシステム全体や特定のディレクトリ下で利用するRubyのバージョンを管理してくれますが、Rubyのインストールは ruby-buildプラグインが必要です。

というわけで、 rbenv を入れたパスの /plugins/ 以下に ruby-build を、これもGitHubからDLしてきて入れます。

Ruby 2.5.1 のインストール

1回目のRubyのインストールをやってみるとCのコンパイラがないといって失敗する

それではrbenvでRubyのインストールをためしてみます。

$ rbenv install 2.5.1
Downloading ruby-2.5.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
Installing ruby-2.5.1...

BUILD FAILED (Amazon Linux AMI 2018.03 using ruby-build 20180822-8-g336584c)

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

Last 10 log lines:
tool/config.sub already exists
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.20180916020903.3029/ruby-2.5.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

すると利用可能なCコンパイラがないといってインストールに失敗します。

Cのコンパイラとしてgccを入れる

sudo yum install gcc

gccC言語コンパイラ、およびコンパイル用実行ファイルの名称です*2

ところで、XCodeを入れたら/入れ直したらmysqlのgemがインストールできるようになった、という話がありますが*3XCodeはもともとC系の言語用のIDEなので、C言語コンパイラが一緒に入った/正常な状態のものに更新された、ということではないかと思います。

2回目のRubyのインストールをやってみるとopenssl, readline, zlibのextensionがないといって失敗する

では、気を取り直してもう一回インストールを試すと、今度は

[ec2-user@ip-172-31-19-189 ~]$ rbenv install 2.5.1
Downloading ruby-2.5.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
Installing ruby-2.5.1...

BUILD FAILED (Amazon Linux AMI 2018.03 using ruby-build 20180822-8-g336584c)

Inspect or clean up the working tree at /tmp/ruby-build.20180916021333.14684
Results logged to /tmp/ruby-build.20180916021333.14684.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/ec2-user/.rbenv/versions/2.5.1
  LDFLAGS=-L/home/ec2-user/.rbenv/versions/2.5.1/lib 
  CPPFLAGS=-I/home/ec2-user/.rbenv/versions/2.5.1/include 

のようなメッセージで失敗します。

openssl-devel readline-devel zlib-devel のインストール

openssl, readline, zlib はそれぞれSSL通信用のライブラリ、コマンド履歴管理用のライブラリ*4、データ圧縮、解凍のためのライブラリですが、これらの拡張が足りず -devel のsuffixがついたパッケージを入れるように言われています。

https://www.unknownengineer.net/entry/2017/04/07/162346 の記事によると、 -devel というsuffixはなにかというと開発に必要なライブラリオブジェクトやヘッダファイル(**.h とかそういう)が入っていて、(そのライブラリやコマンドを単体で利用する場合にはおそらくいらない場合もあると思われるんですが、)ライブラリを利用したプログラムをコンパイルする際に必要となることがあるそうで、

つまり、Rubyコンパイルするのに、openssl, readline, zlib の開発用のヘッダ、ライブラリの一式が必要ということみたいです。

というわけでいれます。

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

なお、 -devel のパッケージは -devel が入っていない方のパッケージに依存しているというか、openssl-develはopensslの内容+開発用オブジェクトやヘッダが入っている、という感じなので、 sudo yum install -y openssl-devel したあとは openssl コマンドが動かせます。

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

...


Installed:
  openssl-devel.x86_64 1:1.0.2k-12.110.amzn1                 readline-devel.x86_64 0:6.2-9.14.amzn1                 zlib-devel.x86_64 0:1.2.8-7.18.amzn1                

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.12.amzn1           krb5-devel.x86_64 0:1.15.1-19.43.amzn1                   libcom_err-devel.x86_64 0:1.42.12-4.40.amzn1          
  libkadm5.x86_64 0:1.15.1-19.43.amzn1                    libselinux-devel.x86_64 0:2.1.10-3.22.amzn1              libsepol-devel.x86_64 0:2.1.7-3.12.amzn1              
  libverto-devel.x86_64 0:0.2.5-4.9.amzn1                 ncurses-devel.x86_64 0:5.7-4.20090207.14.amzn1          

Dependency Updated:
  krb5-libs.x86_64 0:1.15.1-19.43.amzn1                                               openssl.x86_64 1:1.0.2k-12.110.amzn1                                              

Complete!
$ openssl
OpenSSL> exit

このAMIにはもともとopenssl入っていたみたいですが!

3回目の正直でRubyのインストールに成功するので、インストールしたバージョンを利用するようにする

$ rbenv install 2.5.1
Downloading ruby-2.5.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
Installing ruby-2.5.1...
Installed ruby-2.5.1 to /home/ec2-user/.rbenv/versions/2.5.1

$ ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
$ rbenv global 2.5.1
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

mysql2のgemをインストールする

なにもせず mysql2 のgemを入れようとするとmysqlがインストールされていないので怒られる

$ gem install mysql2

Fetching: mysql2-0.5.2.gem (100%)
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
/home/ec2-user/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20180916-10601-y92qux.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
checking for -lmysqlclient... no
-----
mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/ec2-user/.rbenv/versions/2.5.1/bin/$(RUBY_BASE_NAME)
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2 for inspection.
Results logged to /home/ec2-user/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0-static/mysql2-0.5.2/gem_make.out

MySQLをインストールしていないので、

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

と怒られています。それはそうですね...。ということで 'yum install mysql-devel' をします。

mysql-develをインストールする

$ sudo yum install mysql-devel
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                                               | 2.1 kB  00:00:00     
amzn-updates                                                                                                                            | 2.5 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package mysql-devel.noarch 0:5.5-1.6.amzn1 will be installed
--> Processing Dependency: mysql55-devel >= 5.5 for package: mysql-devel-5.5-1.6.amzn1.noarch
--> Processing Dependency: /usr/bin/mysql_config for package: mysql-devel-5.5-1.6.amzn1.noarch
--> Running transaction check
---> Package mysql55.x86_64 0:5.5.61-1.22.amzn1 will be installed
--> Processing Dependency: real-mysql55-libs(x86-64) = 5.5.61-1.22.amzn1 for package: mysql55-5.5.61-1.22.amzn1.x86_64
--> Processing Dependency: mysql-config for package: mysql55-5.5.61-1.22.amzn1.x86_64
---> Package mysql55-devel.x86_64 0:5.5.61-1.22.amzn1 will be installed
--> Running transaction check
---> Package mysql-config.x86_64 0:5.5.61-1.22.amzn1 will be installed
---> Package mysql55-libs.x86_64 0:5.5.61-1.22.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================
 Package                                Arch                            Version                                    Repository                             Size
===============================================================================================================================================================
Installing:
 mysql-devel                            noarch                          5.5-1.6.amzn1                              amzn-main                             2.7 k
Installing for dependencies:
 mysql-config                           x86_64                          5.5.61-1.22.amzn1                          amzn-updates                           49 k
 mysql55                                x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          7.5 M
 mysql55-devel                          x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          201 k
 mysql55-libs                           x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          816 k

Transaction Summary
===============================================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 8.6 M
Installed size: 32 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): mysql-config-5.5.61-1.22.amzn1.x86_64.rpm                                                                                        |  49 kB  00:00:00     
(2/5): mysql-devel-5.5-1.6.amzn1.noarch.rpm                                                                                             | 2.7 kB  00:00:00     
(3/5): mysql55-5.5.61-1.22.amzn1.x86_64.rpm                                                                                             | 7.5 MB  00:00:00     
(4/5): mysql55-libs-5.5.61-1.22.amzn1.x86_64.rpm                                                                                        | 816 kB  00:00:00     
(5/5): mysql55-devel-5.5.61-1.22.amzn1.x86_64.rpm                                                                                       | 201 kB  00:00:00     
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                           15 MB/s | 8.6 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql55-libs-5.5.61-1.22.amzn1.x86_64                                                                                                       1/5 
  Installing : mysql-config-5.5.61-1.22.amzn1.x86_64                                                                                                       2/5 
  Installing : mysql55-5.5.61-1.22.amzn1.x86_64                                                                                                            3/5 
  Installing : mysql55-devel-5.5.61-1.22.amzn1.x86_64                                                                                                      4/5 
  Installing : mysql-devel-5.5-1.6.amzn1.noarch                                                                                                            5/5 
  Verifying  : mysql-config-5.5.61-1.22.amzn1.x86_64                                                                                                       1/5 
  Verifying  : mysql55-libs-5.5.61-1.22.amzn1.x86_64                                                                                                       2/5 
  Verifying  : mysql55-5.5.61-1.22.amzn1.x86_64                                                                                                            3/5 
  Verifying  : mysql55-devel-5.5.61-1.22.amzn1.x86_64                                                                                                      4/5 
  Verifying  : mysql-devel-5.5-1.6.amzn1.noarch                                                                                                            5/5 

Installed:
  mysql-devel.noarch 0:5.5-1.6.amzn1                                                                                                                           

Dependency Installed:
  mysql-config.x86_64 0:5.5.61-1.22.amzn1 mysql55.x86_64 0:5.5.61-1.22.amzn1 mysql55-devel.x86_64 0:5.5.61-1.22.amzn1 mysql55-libs.x86_64 0:5.5.61-1.22.amzn1

Complete!

yum installに指定したパッケージのは mysql-devel ですが、

Dependencies Resolved

===============================================================================================================================================================
 Package                                Arch                            Version                                    Repository                             Size
===============================================================================================================================================================
Installing:
 mysql-devel                            noarch                          5.5-1.6.amzn1                              amzn-main                             2.7 k
Installing for dependencies:
 mysql-config                           x86_64                          5.5.61-1.22.amzn1                          amzn-updates                           49 k
 mysql55                                x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          7.5 M
 mysql55-devel                          x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          201 k
 mysql55-libs                           x86_64                          5.5.61-1.22.amzn1                          amzn-updates                          816 k

Transaction Summary
===============================================================================================================================================================
Install  1 Package (+4 Dependent packages)

のように、mysql-develは mysql55 に依存しているのでMySQLのクライアント、およびサーバのプログラムも入ります*5

これだけ書くと yum install mysql でよさそうな気もしますが、別途試したところ、mysql2のgemのCの部分をコンパイルするために mysql-devel パッケージ内のヘッダが必要みたいです。

あらためてmysql2のgemをインストール

$ gem install mysql2
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Installing ri documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

成功しました。

もうちょっとよくわからないトラブルが起きてそのトラブルシューティングで知見をためようとおもっていたらすんなり進んで若干がっかりしていますが、まあいいですね。

現場からは以上です。

*1:https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins

*2:http://e-words.jp/w/gcc.html

*3:https://qiita.com/unsoluble_sugar/items/1403ddf0ac9709b1aae6#xcode%E3%82%92%E6%9C%80%E6%96%B0%E3%81%AB%E3%82%A2%E3%83%83%E3%83%97%E3%83%87%E3%83%BC%E3%83%88 など

*4:https://ja.wikipedia.org/wiki/GNU_Readline irbとかで上キー押したら前のコマンド見れる、とかそういうところなんでしょうか...?

*5:mysql55だとサーバとクライアントの両方が入るんですが、一つのホストに必要なのは片方だけであることも多いので、サーバプログラム、クライアントプログラムをわけてインストールすることも可能 https://dev.mysql.com/doc/refman/5.6/ja/linux-installation-rpm.html