LoginSignup
1
3

More than 1 year has passed since last update.

Railsで"Failed to build gem native extension"のエラーが出る原因と解決法

Last updated at Posted at 2022-06-09

1. 背景

Rails 5.2.6から5.2.8へアップデートするためにGemfileを修正し、bundle updateした時に事態は発生した。

2. エラーログ

(前略)
Fetching unf_ext 0.0.8.2 (was 0.0.8)
Installing unf_ext 0.0.8.2 (was 0.0.8) with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /opt/bundle/ruby/2.7.0/gems/unf_ext-0.0.8.2/ext/unf_ext
/usr/bin/ruby2.7 -I /usr/lib/ruby/2.7.0 -r ./siteconf20220609-1597-ncuewr.rb extconf.rb
checking for -lstdc++... *** 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.
(後略)

3. 原因

依存関係の問題かと思ったが、rubygems.orgを参照しても問題なさそうだった。
注目すべきポイントは以下の二つ。

Installing unf_ext 0.0.8.2 (was 0.0.8) with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

要約:ネイティブ拡張のgemである'unf_ext'をでインストールするのに失敗した。

checking for -lstdc++... *** 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.

要約:おそらくc++関連のライブラリが不足していてMakefileが作れない。

ネイティブ拡張とはCやC++などの言語で書かれたライブラリをRubyから使用できる機能である。
unf_extのgithubを参照したところ、このgemはHackというプログラミング言語で書かれており、インストールにはC++のコンパイラが必要とのこと。
https://github.com/knu/ruby-unf_ext

4. 対処法

以下の記事を参照しつつ、C++のコンパイラとmakeをインストールした。
https://azechi-n.hatenadiary.com/entry/2020/04/11/084307

apt update
apt install g++ make

上記を実行し再度bundle updateしたところ、全て上手く行った。

5. 参考サイト

knu/ruby-unf_ext
Ruby:slimにdiscordrbをインストールできた

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