実現したいこと
Rails アプリケーションで使用する "ruby-filemagic" をインストールしたいです。
発生している問題・分からないこと
"ruby-filemagic" がインストールできません。
エラーメッセージ
error
1Fetching ruby-filemagic 0.7.2 2Installing ruby-filemagic 0.7.2 with native extensions 3Gem::Ext::BuildError: ERROR: Failed to build gem native extension. 4 5 current directory: /Users/kosuke/Workspace/diary-system/vendor/bundle/ruby/2.5.0/gems/ruby-filemagic-0.7.2/ext/filemagic 6/Users/kosuke/.asdf/installs/ruby/2.5.1/bin/ruby -r ./siteconf20241012-57499-jpkdgf.rb extconf.rb --with-magic-dir=/opt/homebrew/opt/libmagic 7--with-magic-include=/opt/homebrew/include --with-magic-lib=/opt/homebrew/lib 8checking for -lgnurx... no 9checking for magic_open() in -lmagic... no 10*** ERROR: missing required library to compile this module 11*** extconf.rb failed *** 12Could not create Makefile due to some reason, probably lack of necessary 13libraries and/or headers. Check the mkmf.log file for more details. You may 14need configuration options. 15 16Provided configuration options: 17 --with-opt-dir 18 --without-opt-dir 19 --with-opt-include 20 --without-opt-include=${opt-dir}/include 21 --with-opt-lib 22 --without-opt-lib=${opt-dir}/lib 23 --with-make-prog 24 --without-make-prog 25 --srcdir=. 26 --curdir 27 --ruby=/Users/kosuke/.asdf/installs/ruby/2.5.1/bin/$(RUBY_BASE_NAME) 28 --with-magic-dir 29 --with-magic-include=${magic-dir}/include 30 --with-magic-lib=${magic-dir}/lib 31 --with-gnurx-dir 32 --without-gnurx-dir 33 --with-gnurx-include 34 --without-gnurx-include=${gnurx-dir}/include 35 --with-gnurx-lib 36 --without-gnurx-lib=${gnurx-dir}/lib 37 --with-gnurxlib 38 --without-gnurxlib 39 --with-magiclib 40 --without-magiclib 41 42To see why this extension failed to compile, please check the mkmf.log which can be found here: 43 44 /Users/kosuke/Workspace/diary-system/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-22/2.5.0/ruby-filemagic-0.7.2/mkmf.log 45 46extconf failed, exit code 1 47 48Gem files will remain installed in /Users/kosuke/Workspace/diary-system/vendor/bundle/ruby/2.5.0/gems/ruby-filemagic-0.7.2 for inspection. 49Results logged to /Users/kosuke/Workspace/diary-system/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-22/2.5.0/ruby-filemagic-0.7.2/gem_make.out 50 51An error occurred while installing ruby-filemagic (0.7.2), and Bundler cannot continue. 52Make sure that `gem install ruby-filemagic -v '0.7.2' --source 'https://rubygems.org/'` succeeds before bundling. 53 54In Gemfile: 55 ruby-filemagic
該当のソースコード
特になし
特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ネットで調べると "libmagic" が必要とかパスを指定する必要があるという情報しか出てこないのですが、libmagic はインストールされていますしパスも指定していますがインストールできません。libmagic の再インストールも試しましたが駄目です。
brew reinstall libmagic gem install -v 0.7.2 ruby-filemagic -- \ --with-magic-include=/opt/homebrew/include \ --with-magic-lib=/opt/homebrew/lib gem install ruby-filemagic -v '0.7.2' --source 'https://rubygems.org/' -- --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) bundle config --local build.ruby-filemagic "--with-magic-dir=$(brew --prefix libmagic)" gem install ruby-filemagic -v '0.7.2' --source 'https://rubygems.org/' -- --with-magic-include=/opt/homebrew/include --with-magic-lib=/opt/homebrew/lib/ gem install ruby-filemagic -v 0.7.2 -- \ --with-magic-lib=$(brew --prefix)/lib \ --with-magic-include=$(brew --prefix)/include brew link libmagic gem install ruby-filemagic -v '0.7.2' --source 'https://rubygems.org/' -- --with-magic-include=/opt/homebrew/include --with-magic-lib=/opt/homebrew/lib/
試した情報は以下のとおりです。
- Mac(apple silicon)でruby-filemagicのgemインストール時に ld: library not found for -lgnurx というエラーが出たときの対応 #homebrew - Qiita
- 【Tips】FileMagic用gemインストール時に発生しうるエラーへの対処 #Ruby - Qiita
- fail to install ruby-filemagic 0.7.2 on mac m1 big sur - Stack Overflow
- homebrew - Trying to install ruby-filemagic on Snow Leopard using brew rather than ports - Stack Overflow
- Install libmagic on Mac OS X via homebrew
補足
特になし
"/Users/kosuke/Workspace/diary-system/vendor/bundle/ruby/2.5.0/extensions/x86_64-darwin-22/2.5.0/ruby-filemagic-0.7.2/mkmf.log" の中身は以下のとおりです。
```
have_library: checking for -lgnurx... -------------------- no
"clang -o conftest -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/x86_64-darwin22 -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/ruby/backward -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0 -I. -I/opt/local/include -I/usr/local/include -I/usr/include -I/opt/homebrew/opt/libmagic/include -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include -I/opt/homebrew/opt/imagemagick@6/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -Wno-error=implicit-function-declaration -fno-common -pipe conftest.c -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/local/lib -L/usr/local/lib -L/usr/lib -L/opt/homebrew/opt/libmagic/lib -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/homebrew/opt/imagemagick@6/lib -fstack-protector -lruby.2.5.1 -lpthread -ldl -lobjc "
ld: warning: search path '/opt/local/lib' not found
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/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/x86_64-darwin22 -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/ruby/backward -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0 -I. -I/opt/local/include -I/usr/local/include -I/usr/include -I/opt/homebrew/opt/libmagic/include -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include -I/opt/homebrew/opt/imagemagick@6/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -Wno-error=implicit-function-declaration -fno-common -pipe conftest.c -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/local/lib -L/usr/local/lib -L/usr/lib -L/opt/homebrew/opt/libmagic/lib -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/homebrew/opt/imagemagick@6/lib -fstack-protector -lruby.2.5.1 -lgnurx -lpthread -ldl -lobjc "
ld: warning: search path '/opt/local/lib' not found
ld: library 'gnurx' not found
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/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/x86_64-darwin22 -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/ruby/backward -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0 -I. -I/opt/local/include -I/usr/local/include -I/usr/include -I/opt/homebrew/opt/libmagic/include -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include -I/opt/homebrew/opt/imagemagick@6/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -Wno-error=implicit-function-declaration -fno-common -pipe conftest.c -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/local/lib -L/usr/local/lib -L/usr/lib -L/opt/homebrew/opt/libmagic/lib -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/homebrew/opt/imagemagick@6/lib -fstack-protector -lruby.2.5.1 -lmagic -lpthread -ldl -lobjc "
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 */
"clang -o conftest -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/x86_64-darwin22 -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0/ruby/backward -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include/ruby-2.5.0 -I. -I/opt/local/include -I/usr/local/include -I/usr/include -I/opt/homebrew/opt/libmagic/include -I/Users/kosuke/.asdf/installs/ruby/2.5.1/include -I/opt/homebrew/opt/imagemagick@6/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -Wno-error=implicit-function-declaration -fno-common -pipe conftest.c -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/local/lib -L/usr/local/lib -L/usr/lib -L/opt/homebrew/opt/libmagic/lib -L. -L/Users/kosuke/.asdf/installs/ruby/2.5.1/lib -L/opt/homebrew/opt/imagemagick@6/lib -fstack-protector -lruby.2.5.1 -lmagic -lpthread -ldl -lobjc "
ld: warning: search path '/opt/local/lib' not found
ld: warning: ignoring file '/opt/homebrew/Cellar/libmagic/5.45/lib/libmagic.1.dylib': found architecture 'arm64', required architecture 'x86_64'
ld: Undefined symbols:
_magic_open, referenced from:
_t in conftest-ad4b24.o
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: extern void magic_open();
14: int t(void) { magic_open(); return 0; }
/* end */
--------------------
```
インストールした libmagic.1.dylib はどこに有りますか?
「パスも指定していますが」とありますが、どの様に指定したのか書いてください。翻訳しないで 入力した通りに
libmagic.1.dylib は /opt/homebrew/Cellar/libmagic/5.45/lib 以下にありますが /opt/homebrew/lib にもシンボリックリンクが貼られています。
パスの指定は本文にも記載していますし翻訳もしていないのですが、--with-magic-include=/opt/homebrew/include --with-magic-lib=/opt/homebrew/lib や --with-magic-dir=$(brew --prefix libmagic) のように指定しました。
ちなみにシンボリックリンクではなく実体を参照しても結果は同じでした。
「lib 以下にあります」とのことですが 「lib にあります」ではない? 「以下」にある?
/opt/homebrew/lib/libmagic.1.dylib があるのですね?
はい、/opt/homebrew/lib/libmagic.1.dylib があります。ただしこれはシンボリックリンクで、実体は /opt/homebrew/Cellar/libmagic/5.45/lib/libmagic.1.dylib です。ls -l で確認すると /opt/homebrew/lib/libmagic.1.dylib -> ../Cellar/libmagic/5.45/lib/libmagic.1.dylib のようにリンクが貼られています。
> ld: warning: ignoring file '/opt/homebrew/Cellar/libmagic/5.45/lib/libmagic.1.dylib': found architecture 'arm64', required architecture 'x86_64'
と表示されているので、libmagic.1.dylib は ARM64 版で、実際に必要なのは x86_64 版ではないでしょうか。
なるほど、このライブラリだけ x86_64 でインストールするとなると他のライブラリとの互換性が気になります。他のライブラリは arm64 でこれだけ x86_64 でアプリケーションを動作させることはできるのでしょうか? それからインストールする際にも x86_64 版の Homebrew をインストールしたりする必要があっていろいろ環境的にめんどくさい気がします。やはり M1 Mac (非 Docker、非仮想 Linux 環境) で ruby-filemagic を含む開発は現実的ではないのでしょうか?
私の環境(M2/Macbook)ではとりあえずインストールできたようです。
動作確認はしていませんが。。。
% ruby -v
ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin23]
% brew info libmagic
==> libmagic: stable 5.45 (bottled)
Implementation of the file(1) command
https://www.darwinsys.com/file/
Installed
% gem list ruby-filemagic
*** LOCAL GEMS ***
ruby-filemagic (0.7.3)
Ruby のバージョンのところを見てもしやと思い、Ruby のバージョンを上げて試してみたらインストールできました。
環境構築をしようとしていたプロジェクトは古くて Ruby のバージョンが 2.5.1 (ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin22]) でした。これだとバージョンが古すぎて Ruby 自体も x86_64 のため各種ネイティブライブラリが x86_64 を要求するが arm64 との依存関係があるとインストールできない、ということだったみたいです。
手元に Ruby 3.0.7 (ruby 3.0.7p220 (2024-04-23 revision 724a071175) [arm64-darwin22]) の環境があったのでそちらで試したら動作しました。バージョン情報を載せていただいたおかげで気づけました、ありがとうございました。
※ 解決済みのためコメントを削除

回答1件
あなたの回答
tips
プレビュー