0
0

More than 3 years have passed since last update.

rbenvでバージョンを変えたら、gem install railsできないって言われた。

Last updated at Posted at 2020-04-15

備忘録

概要


CentOS7の環境でrubyをrbenvで管理。
rbenv local 2.6.5でとあるアプリのRubyのバージョンを変更。
すると、、
$ rails s -b 0.0.0.0
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.5.7

怒られちゃったという話。

さらに「gem install rails」したらエラーにハマった話。

状況


まぁ、
そもそもこれは当たり前の話で、実際に.rbenv以下のパッケージの管理のされ方を見ればわかる。
/home/vagrant/.rbenv/versions
$ ls
2.5.7  2.6.5

こんな感じで管理されており、

/home/vagrant/.rbenv/versions/2.5.7/bin
$ ls
bundle               irb       rake          spring
bundler              listen    rdoc          sprockets
byebug               nokogiri  ri            thor
chromedriver-helper  puma      ruby          tilt
chromedriver-update  pumactl   sass
erb                  rackup    sass-convert
gem                  rails     scss

いつもの方々がいるが、
さっきダウンロードした2.6.5の方は

/home/vagrant/.rbenv/versions/2.6.5/bin
$ ls
bundle  bundler  erb  gem  irb  rake  rdoc  ri  ruby

いないんですよね。
だから当然railsコマンドも使えないというわけ。

対応


こちらの記事を参考にさせて頂いた。
手順
$ gem update --system
$ gem install bundler
$ gem install rails

ふぅ、、。ってあれ?

*** 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/vagrant/.rbenv/versions/2.6.5/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --use-system-libraries
    --enable-static
    --disable-static
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --enable-cross-build
    --disable-cross-build
/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mini_portile2-2.4.0/lib/mini_portile2/mini_portile.rb:402:in `block in execute': Failed to complete compile task (RuntimeError)
    from /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mini_portile2-2.4.0/lib/mini_portile2/mini_portile.rb:373:in `chdir'
    from /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mini_portile2-2.4.0/lib/mini_portile2/mini_portile.rb:373:in `execute'
    from /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mini_portile2-2.4.0/lib/mini_portile2/mini_portile.rb:115:in `compile'
    from /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mini_portile2-2.4.0/lib/mini_portile2/mini_portile.rb:154:in `cook'
    from extconf.rb:365:in `block (2 levels) in process_recipe'
    from extconf.rb:257:in `block in chdir_for_build'
    from extconf.rb:256:in `chdir'
    from extconf.rb:256:in `chdir_for_build'
    from extconf.rb:364:in `block in process_recipe'
    from extconf.rb:262:in `tap'
    from extconf.rb:262:in `process_recipe'
    from extconf.rb:557:in `<main>'

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

  /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/nokogiri-1.10.9/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/nokogiri-1.10.9 for inspection.
Results logged to /home/vagrant/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0/nokogiri-1.10.9/gem_make.out

なぜかインストールできない。。。

かなり煮詰まったが、こんなページを発見
https://teratail.com/questions/221735

※CentOSではnative extension系のエラーはdevelパッケージをインストールとだいたい解決します。
どのdevelパッケージが必要なのかはエラーメッセージなどから判断したり、
「CentOS nokogiri devel」とかで検索すると分かる場合があります。
nokogoriの場合は「libxml2-devel」「libxslt-devel」のはずです。

自分のエラーコードもnokogiriのインストールで詰まってたみたいなのでなるほどと思いました。
CentOS環境では、これからはまず「devel」(補助パッケージみたいなものという認識している。。。)の存在を探せばいいのか。

しかし解決しなかった。。。。。

解決方法



この記事に救われた。。。
nokogiriが悪さするから先にインストールしろってことらしい。(雑)
$ brew install libxml2 libxslt
# nokogiriとかいう悪者を先にブチのめす(インストールする)
$ gem install nokogiri -- --use-system-libraries --with-xml2-include=/usr/include/libxml2 --with-xml2- lib=/usr/lib/

$ gem install rails

そしてついに、、、

$ rails -v
Rails 5.2.4.2
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