0
0

More than 1 year has passed since last update.

conoha wingでruby-buildして新しいrubyを入れてみる

Last updated at Posted at 2023-04-05

conoha wingのrubyのバージョン低すぎぃぃ!

conoha wingをに入っているrubyはバージョン2.5と低い。
脆弱性のあるバージョンではなく最新版が使いたいところ。

それとgem installも出来ないっぽい?

ORマッパー無いとmysql使う気すら起きない自分にとって、ActiveRecordが使えないなんて発狂してしまう!

他にもrmagickは?faradayは?nokogiriは?

これらの便利なライブラリにおんぶに抱っこしてもらってる自分にとってgem使えないのは死活問題。
というわけで最新のrubyをぶっこんで好き放題させてもらう。

試しにruby 3.2.2を入れてみる

とりあえずrbenvで入れようと思ったんだけども、既に試した先駆者様がいらっしゃって、メモリーの制限でbuildが止まるらしい。

試しにやってみる。

基本的なやり方は @cycloawaodorin様 の さくらレンタルサーバーで ruby 3.2.0 をビルド を参考にいたしました。

参考にと言ってますが、ほぼほぼ同じ手順で出来ました。

で、意気揚々とrbenv installしてみると・・・?

$ CC=gcc RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local --with-libyaml-dir=$HOME/local" TMPDIR="$HOME/tmp" rbenv install 3.2.2

結果。

make: vfork: Cannot allocate memory
make: vfork: Cannot allocate memory
make: vfork: Cannot allocate memory
make: vfork: Cannot allocate memory

案の定ダメ・・・。

「じゃあjオプションで並列で走るスレッドをへらしてみればいいのでは?」
ってことでMAKE_OPTSで2スレッドに制限してみる。

$ CC=gcc MAKE_OPTS="-j 2" RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local --with-libyaml-dir=$HOME/local" TMPDIR="$HOME/tmp" rbenv install 3.2.2

allocateのエラーはなくなったけど、なんかエラーで止まってる。

thread_pthread.h:109:39: error:(略

ググってみると・・・↓
https://stackoverflow.com/questions/75094431/why-do-i-get-error-compiling-ruby-3-2-on-centos-7

さすが!困ったときのstackoverflow!
どうやら3系rubyはgcc6以降じゃないとbuild出来ないらしい。

conoha wingのgccを確認してみると・・・?

$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.

全然ダメじゃねーか!!!

rubyをbuild出来るgccを入れる

しょうがないのでgcc4.8.5でgcc8.5.0を入れる

※注意
 prefixのconoha_idになってるところは適宜書き換えること。
 /usr/localはroot権限なのでダメです。
 /home/自分のid/好きなディレクトリにしておく。
 makeはめっちゃ時間かかるので注意。
 本当はバックグラウンド実行推奨です(自分はPCがスリープしてsshが切れた。

ダウンロードしてmake installする。

curl -LO http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-8.5.0/gcc-8.5.0.tar.gz
tar xvzf gcc-8.5.0.tar.gz

cd gcc-8.5.0

./contrib/download_prerequisites

mkdir build
cd build

../configure --enable-languages=c,c++ --prefix=/home/conoha_id/gcc85 --disable-multilib

make
make install

ポイントは、buildというmake用フォルダを作るところ。
こうしないとうまくmake出来ない。

ちゃんと入ったか確認する。

$ /home/conoha_id/gcc85/bin/gcc --version
gcc (GCC) 8.5.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

はいったー!

.bash_profileにパスを入れる。

cd
vi .bash_profile

下の内容を追記してパスを通してあげる

export PATH=$HOME/gcc85/bin:$PATH
export LD_LIBRARY_PATH=$HOME/gcc85/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$HOME/gcc85/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$HOME/gcc85/lib64:$LIBRARY_PATH
export LIBRARY_PATH=$HOME/gcc85/lib:$LIBRARY_PATH

このままだと上の設定が有効にならないので↓で更新。

$ source .bash_profile

後は、このgccでruby-build出来ればOKなはず。

$ cd
$ cd .rbevn
$ CC=gcc MAKE_OPTS="-j 2" RUBY_CONFIGURE_OPTS="--with-openssl-dir=/usr/local --with-libyaml-dir=$HOME/local" TMPDIR="$HOME/tmp" rbenv install 3.2.2

To follow progress, use 'tail -f /tmp/ruby-build.20230405090742.86394.log' or pass --verbose
Downloading ruby-3.2.2.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.2.tar.gz
Installing ruby-3.2.2...
Installed ruby-3.2.2 to /home/conoha_id/.rbenv/versions/3.2.2


NOTE: to activate this Ruby version as the new default, run: rbenv global 3.2.2

通った!

デフォルトを3.2.2に変更。

$ rbenv global 3.2.2
$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

キマシタワー!

gemでなんかを入れてみる。

試しにfaradayをインストール。

$ rbenv rehash
$ gem install faraday
Fetching faraday-2.7.4.gem
Fetching faraday-net_http-3.0.2.gem
Successfully installed faraday-net_http-3.0.2
Successfully installed faraday-2.7.4
Parsing documentation for faraday-net_http-3.0.2
Installing ri documentation for faraday-net_http-3.0.2
Parsing documentation for faraday-2.7.4
Installing ri documentation for faraday-2.7.4
Done installing documentation for faraday-net_http, faraday after 0 seconds
2 gems installed

ちゃんと入ってるー。

irbでチェックしてみる。

$ irb
irb(main):001:0> require "faraday"
=> true
irb(main):002:0> response = Faraday.get('http://httpbingo.org/uuid')
=>
#<Faraday::Response:0x00007f825403a420
...
irb(main):003:0> response.body
=> "{\n  \"uuid\": \"dd3cb902-66b3-485b-93bd-0ca9ed288838\"\n}\n"
irb(main):004:0>

バッチリ!

cgiとして動かしてみる。

以下のようなスクリプトを用意。

faraday.rb
#!/home/conoha_id/.rbenv/shims/ruby
require "faraday"
require "json"

response = Faraday.get('http://httpbingo.org/uuid')

print "Content-Type: text/plain\n\n"
puts JSON.parse(response.body)["uuid"]

アクセスしてみる。

$ curl https://example.fun/faraday.rb
c0ad7f30-4c33-48fd-9184-4df3d66d4f62

やったぜ

感想

キャンペーンで3年間まとめ買いしちゃったのに、実際に使ってみたらrubyのバージョン低くて凹んでたけど、何とかなってよかったです。gccも新しくなったし次はrmagickなどを入れて遊びたいと思います。

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