LoginSignup
2
1

More than 3 years have passed since last update.

Mac(apple silicon)でruby-filemagicのgemインストール時に ld: library not found for -lgnurx というエラーが出たときの対応

Posted at

この記事の対象者

Macでapple silicon版homebrewを利用している人向け

現象

Rubyのfile-magicのインストールで以下のようなエラーが出ることがあります

gem install -v 0.7.2 ruby-filemagic

gemをインストールしようとすると以下のようなエラーが出る

Building native extensions. This could take a while...
ERROR:  Error installing ruby-filemagic:
    ERROR: Failed to build gem native extension.

    current directory: /Users/sogasawara/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/ruby-filemagic-0.7.2/ext/filemagic
/Users/sogasawara/.rbenv/versions/2.5.5/bin/ruby -I /Users/sogasawara/.rbenv/versions/2.5.5/lib/ruby/site_ruby/2.5.0 -r ./siteconf20210125-7199-4elsmm.rb extconf.rb
checking for -lgnurx... no
checking for magic_open() in -lmagic... no
*** ERROR: missing required library to compile this module
*** 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=/Users/sogasawara/.rbenv/versions/2.5.5/bin/$(RUBY_BASE_NAME)
    --with-magic-dir
    --without-magic-dir
    --with-magic-include
    --without-magic-include=${magic-dir}/include
    --with-magic-lib
    --without-magic-lib=${magic-dir}/lib
    --with-gnurx-dir
    --without-gnurx-dir
    --with-gnurx-include
    --without-gnurx-include=${gnurx-dir}/include
    --with-gnurx-lib
    --without-gnurx-lib=${gnurx-dir}/lib
    --with-gnurxlib
    --without-gnurxlib
    --with-magiclib
    --without-magiclib

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

  /Users/sogasawara/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/extensions/-darwin-20/2.5.0/ruby-filemagic-0.7.2/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/sogasawara/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/ruby-filemagic-0.7.2 for inspection.
Results logged to /Users/sogasawara/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/extensions/-darwin-20/2.5.0/ruby-filemagic-0.7.2/gem_make.out

mkmf.logを見ると以下のように書いてある。

have_library: checking for -lgnurx... -------------------- no

"clang -o conftest -I/Users/sogasawara/.rbenv/versions/2.5.5/include/ruby-2.5.0/-darwin20 -I/Users/sogasawara/.rbenv/versions/2.5.5/inc
ld: warning: directory not found for option '-L/opt/local/lib'
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

"clang -o conftest -I/Users/sogasawara/.rbenv/versions/2.5.5/include/ruby-2.5.0/-darwin20 -I/Users/sogasawara/.rbenv/versions/2.5.5/inc
ld: warning: directory not found for option '-L/opt/local/lib'
ld: library not found for -lgnurx
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10:
11:   return 0;
12: }
13:
14: int t(void) { ; return 0; }
/* end */

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

have_library: checking for magic_open() in -lmagic... -------------------- no

"clang -o conftest -I/Users/sogasawara/.rbenv/versions/2.5.5/include/ruby-2.5.0/-darwin20 -I/Users/sogasawara/.rbenv/versions/2.5.5/inc
conftest.c:13:57: error: use of undeclared identifier 'magic_open'
int t(void) { void ((*volatile p)()); p = (void ((*)()))magic_open; return !p; }
                                                        ^
1 error generated.
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: /*top*/
 4: extern int t(void);
 5: int main(int argc, char **argv)
 6: {
 7:   if (argc > 1000000) {
 8:     printf("%p", &t);
 9:   }
10:
11:   return 0;
12: }
13: int t(void) { void ((*volatile p)()); p = (void ((*)()))magic_open; return !p; }
/* end */

...

環境: Mac Mini(apple silicon, BigSur 11.1), rbenv (v2.5.5)

原因

homebrewはこれまで /usr/local 以下にアプリケーションをインストールしていたがM1版のhomebrewはデフォルトで /opt/homebrew 以下にインストールする仕様となった。
ref: https://github.com/Homebrew/brew/pull/9117

file-magicではネイティブエクステンションでhomebrewでインストールされたlibmagicを利用するが、libmagicのファイル探索先に新しくデフォルトとなった /opt/homebrew が含まれていないため上記のようなエラーが出ることがあるっぽい

具体的にはこちらのファイルでlibmagic関連のファイルを探索する際にエラーが出る。

該当箇所は以下。

dir_config('magic', HEADER_DIRS, LIB_DIRS)

dir_configメソッドを使ってヘッダファイルとlibファイルの探索先を決めるようだが探索先には/opt/homebrew がないためエラーが出おーrたと思われる

対策

dir_configは特定のコマンドライン引数を使って探索先を変更できるので、ビルドオプションをいじればインストールできるはず。
ref: https://ruby-doc.org/stdlib-2.5.0/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config

例えば以下でインストールできた
sh
gem install -v 0.7.2 ruby-filemagic -- \
--with-magic-include=/opt/homebrew/include \
--with-magic-lib=/opt/homebrew/lib

こちらでもインストールできた

gem install -v 0.7.2 ruby-filemagic -- --with-magic-prefix=/opt/homebrew

こちらでもできた。(あまりこのあたり詳しくないので間違ってたらすみません)

gem install -v 0.7.2 ruby-filemagic -- --with-magic-dir=$(brew --prefix libmagic)

/opt/homebrew/include/opt/homebrew/lib の中身はsymbolic linkで実体は別の場所にあるので実体のpathにしてもよいのかもしれません

bundle installする場合

gem install同様ビルドオプションをつければよいはず。例えば以下のようなコマンドを打てばok

  bundle config --local build.ruby-filemagic "--with-magic-dir=$(brew --prefix libmagic)"

あとはインストールするだけ

bundle install --path=vendor/bundle
2
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
2
1