LoginSignup
0
1

More than 3 years have passed since last update.

rbenv installが成功しない時は、環境変数「RUBY_BUILD_CURL_OPTS」を使ってみよう。

Posted at

Vagrantで作成しているローカルの開発環境で、rubyを入れる際、rbenvを使おうとしました(Vagrantなので、基本的にrootで作業しています)。

rbenvのインストール自体は完了しましたが、rbenvで、いざrubyをインストールしようとすると、以下のエラーが出ました。

[root@localhost ~]# rbenv install 2.6.6
Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
error: failed to download ruby-2.6.6.tar.bz2

BUILD FAILED (CentOS Linux 7 using ruby-build 20200520-2-gf00582b)

curlの失敗

ファイルをダウンロードできないようです。調べてみると、rbenvではファイルのダウンロードにcurlを使ってることが分かりました。

実は、この作業をしていた環境は、proxyの内部であったので、おそらくネットワーク周りが原因だろうと当たりをつけて、curl単体を実行して、その挙動を確認してみました。

[root@localhost ~]# curl https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
curl: (60) Peer's Certificate issuer is not recognized.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

プロキシ設定

curlも失敗しました。どうも原因は「SSL認証が上手く行ってない」もしくは「プロキシが上手く設定できてない」の、いずれかのようです。なので、まず可能性が高いプロキシ関係の設定をしてみました。

vi ~/.curlrcでプロキシを設定。

proxy-user="{ユーザー名}:{パスワード}"
proxy = "http:/{プロキシ}:{ポート番号}/"

設定後、curlを実行してみました。エラーメッセージは少し変化しましたが、上手く行きません(ちなみに、前項と指定バージョンが違ってますが、あれこれとバージョンを切り替えて試していたためです)。

[root@localhost ~]# curl -v https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
* Could not resolve proxy: http; 不明なエラー
* Closing connection 0
curl: (5) Could not resolve proxy: http; 不明なエラー

オプションによるプロキシ指定により、curl単体は成功

curlには、直接、プロキシなどを設定できる-xパラメータと、ついでにファイルダウンロードで使用する-O、SSL接続で証明書エラーをスキップするパラメータ-kを実行すると、curl単体でのダウンロードは上手く行きました。

curl -k -x http://{ユーザー名}:{パスワード}@{プロキシ}:{ポート番号}/ -v -O https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2

...
...
...

  0 11.9M    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{ [data not shown]
100 11.9M  100 11.9M    0     0  1314k      0  0:00:09  0:00:09 --:--:-- 1658k

エイリアスを指定。curlは成功。しかし、rbenvは失敗。

やはりプロキシが原因のようだと分かりました。rbenvでも、このパラメータを利用するために、次にcurlのエイリアスに、同じパラメータを設定してみました。

vi /etc/bashrcで、末尾に以下を設定。

alias curl="curl -k -x http://{ユーザー名}:{パスワード}@{プロキシ}:{ポート番号}/"

source ~/.bashrcで設定を再読み込みして、-x-kを省略してcurlを設定すると、curl単体では成功。

curl -v -O https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2

100 11.9M  100 11.9M    0     0  1853k      0  0:00:06  0:00:06 --:--:-- 1899k

しかし、rbenvの方は、上手く行かない。

[root@localhost ~]# rbenv install 2.6.6
Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
error: failed to download ruby-2.6.6.tar.bz2

BUILD FAILED (CentOS Linux 7 using ruby-build 20200520-2-gf00582b)

ruby-buildの環境変数RUBY_BUILD_CURL_OPTSを利用し、rbenvも成功。

あれこれと考えてみて、rbenvと、ruby-buildのGithubページを眺めていると、curlのオプションを指定できる環境変数RUBY_BUILD_CURL_OPTSがある事に気づく。

以下のように実行すると、成功。

[root@localhost ~]# RUBY_BUILD_CURL_OPTS="-k -x http://{ユーザー名}:{パスワード}@{プロキシ}:{ポート番号}/" rbenv install 2.7.1
Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...

Installed ruby-2.7.1 to /root/.rbenv/versions/2.7.1

[root@localhost ~]# rbenv global 2.7.1
[root@localhost ~]# rbenv rehash
[root@localhost ~]# ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[root@localhost ~]#

成功しました。RUBY_BUILD_CURL_OPTSを使う方法というのが、自分が検索した限りでは見当たらなかったので、メモとして残しておきます。

他に、RUBY_BUILD_HTTP_CLIENTというパラメータを使うと、aria2cwgetを使うように指定できるみたいなので、curlそのものが使えない環境の人は、RUBY_BUILD_HTTP_CLIENTを使うと良いかもしれません。

0
1
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
0
1