トラブル:さくらのレンタルサーバでRuby 2.6.1がビルドできない

前提

現象

rbenv install 2.6.1するとエラーになる:

$ rbenv install 2.6.1
Installing ruby-2.6.1...

BUILD FAILED (FreeBSD 9.1-RELEASE-p24 using ruby-build 20190130-6-g2ab166e)

Inspect or clean up the working tree at /home/chorus-kujira/tmp/ruby-build.20190310215723.33461
Results logged to /home/chorus-kujira/tmp/ruby-build.20190310215723.33461.log

Last 10 log lines:
                                    xmlrpc 0.3.0
installing rdoc:                    /home/chorus-kujira/.rbenv/versions/2.6.1/share/ri/2.6.0/system
installing html-docs:               /home/chorus-kujira/.rbenv/versions/2.6.1/share/doc/ruby
installing capi-docs:               /home/chorus-kujira/.rbenv/versions/2.6.1/share/doc/ruby
The Ruby openssl extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
  --prefix=/home/chorus-kujira/.rbenv/versions/2.6.1
  LDFLAGS=-L/home/chorus-kujira/.rbenv/versions/2.6.1/lib 
  CPPFLAGS=-I/home/chorus-kujira/.rbenv/versions/2.6.1/include 

ログ tmp/ruby-build.20190310215723.33461.log:

*** Following extensions are not compiled:
openssl:
        Could not be configured. It will not be installed.
        /home/chorus-kujira/tmp/ruby-build.20190310215723.33461/ruby-2.6.1/ext/openssl/extconf.rb:105: OpenSSL >= 1.0.1 or LibreSSL is required
        Check ext/openssl/mkmf.log for more details.
gdbm:
        Could not be configured. It will not be installed.
        Check ext/gdbm/mkmf.log for more details.
*** Fix the problems, then remove these directories and try again if you want.

ログ tmp/ruby-build.20190310215723.33461/ruby-2.6.1/ext/openssl/mkmf.log:

checking for OpenSSL version is 1.0.1 or later... -------------------- no

"gcc -I../../.ext/include/x86_64-freebsd9.1 -I../.././include -I../.././ext/openssl -I/home/chorus-kujira/.rbenv/versions/2.6.1/include    -O3 -ggdb3 -Wall -Wextra -Wdeclaration-after-statement -Wdeprecated-declarations -Wimplicit-function-declaration -Wim\
plicit-int -Wpointer-arith -Wwrite-strings -Wmissing-noreturn -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-unused-parameter -Wno-unused-value -Wunused-variable   -c conftest.c"
conftest.c:6: error: size of array 'conftest_const' is negative
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <openssl/opensslv.h>
4:
5: /*top*/
6: int conftest_const[(OPENSSL_VERSION_NUMBER >= 0x10001000L) ? 1 : -1];
/* end */

--------------------

/home/chorus-kujira/tmp/ruby-build.20190310215723.33461/ruby-2.6.1/ext/openssl/extconf.rb:105: OpenSSL >= 1.0.1 or LibreSSL is required
        /home/chorus-kujira/tmp/ruby-build.20190310215723.33461/ruby-2.6.1/ext/openssl/extconf.rb:105:in `<top (required)>'
        ./ext/extmk.rb:214:in `load'
        ./ext/extmk.rb:214:in `block in extmake'
        /home/chorus-kujira/tmp/ruby-build.20190310215723.33461/ruby-2.6.1/lib/mkmf.rb:331:in `open'
        ./ext/extmk.rb:210:in `extmake'
        ./ext/extmk.rb:568:in `block in <main>'
        ./ext/extmk.rb:564:in `each'
        ./ext/extmk.rb:564:in `<main>'

原因

リンクしているOpenSSLのバージョンが1.0.1以降でない:

$ cat /usr/include/openssl/opensslv.h  | grep OPENSSL_VERSION_NUMBER
# define OPENSSL_VERSION_NUMBER  0x009081ffL

対策

/usr/local/ssl/にOpenSSL 1.0.2がインストールされている:

$ cat /usr/local/ssl/include/openssl/opensslv.h | grep OPENSSL_VERSION_NUMBER
# define OPENSSL_VERSION_NUMBER  0x100020ffL

ので,/usr/local/ssl/を指定してビルドする:

$ RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local/ssl" rbenv install 2.6.1
Installing ruby-2.6.1...
Installed ruby-2.6.1 to /home/chorus-kujira/.rbenv/versions/2.6.1

see https://github.com/rbenv/ruby-build/wiki#missing-openssl.