TL;DR
sudo apt install build-essential
で解決した。
環境
- Ubuntu 22.04
- Ruby 3.0.2p107 (Ubuntu 22.04 標準)
発生した事象
sudo gem install middleman
を実行したところ、エラーが発生した。
$ sudo gem install middleman
Fetching backports-3.24.1.gem
Fetching addressable-2.8.6.gem
Fetching activesupport-7.0.8.gem
Building native extensions. This could take a while...
ERROR: Error installing middleman:
ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/3.0.0/gems/ffi-1.16.3/ext/ffi_c
/usr/bin/ruby3.0 -I /usr/lib/ruby/vendor_ruby -r ./siteconf20240102-433292-z6i1fr.rb extconf.rb
checking for ffi.h... *** 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=/usr/bin/$(RUBY_BASE_NAME)3.0
--with-ffi_c-dir
--without-ffi_c-dir
--with-ffi_c-include
--without-ffi_c-include=${ffi_c-dir}/include
--with-ffi_c-lib
--without-ffi_c-lib=${ffi_c-dir}/lib
--enable-debug
--disable-debug
--enable-system-libffi
--disable-system-libffi
--with-libffi-config
--without-libffi-config
--with-pkg-config
--without-pkg-config
--with-ffi-dir
--without-ffi-dir
--with-ffi-include
--without-ffi-include=${ffi-dir}/include
--with-ffi-lib
--without-ffi-lib=${ffi-dir}/lib
/usr/lib/ruby/3.0.0/mkmf.rb:471:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /usr/lib/ruby/3.0.0/mkmf.rb:613:in `try_cpp'
from /usr/lib/ruby/3.0.0/mkmf.rb:1124:in `block in have_header'
from /usr/lib/ruby/3.0.0/mkmf.rb:971:in `block in checking_for'
from /usr/lib/ruby/3.0.0/mkmf.rb:361:in `block (2 levels) in postpone'
from /usr/lib/ruby/3.0.0/mkmf.rb:331:in `open'
from /usr/lib/ruby/3.0.0/mkmf.rb:361:in `block in postpone'
from /usr/lib/ruby/3.0.0/mkmf.rb:331:in `open'
from /usr/lib/ruby/3.0.0/mkmf.rb:357:in `postpone'
from /usr/lib/ruby/3.0.0/mkmf.rb:970:in `checking_for'
from /usr/lib/ruby/3.0.0/mkmf.rb:1123:in `have_header'
from extconf.rb:10:in `system_libffi_usable?'
from extconf.rb:46:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/var/lib/gems/3.0.0/extensions/x86_64-linux/3.0.0/ffi-1.16.3/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/3.0.0/gems/ffi-1.16.3 for inspection.
Results logged to /var/lib/gems/3.0.0/extensions/x86_64-linux/3.0.0/ffi-1.16.3/gem_make.out
$ cat /var/lib/gems/3.0.0/extensions/x86_64-linux/3.0.0/ffi-1.16.3/mkmf.log
package configuration for libffi is not found
have_header: checking for ffi.h... -------------------- no
"x86_64-linux-gnu-gcc -o conftest -I/usr/include/x86_64-linux-gnu/ruby-3.0.0 -I/usr/include/ruby-3.0.0/ruby/backward -I/usr/include/ruby-3.0.0 -I. -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -ffile-prefix-map=/build/ruby3.0-ohOwi0/ruby3.0-3.0.2=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC conftest.c -L. -L/usr/lib/x86_64-linux-gnu -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fstack-protector-strong -rdynamic -Wl,-export-dynamic -lruby-3.0 -lm -lc"
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5: return !!argv[argc];
6: }
/* end */
"x86_64-linux-gnu-gcc -E -I/usr/include/x86_64-linux-gnu/ruby-3.0.0 -I/usr/include/ruby-3.0.0/ruby/backward -I/usr/include/ruby-3.0.0 -I. -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -ffile-prefix-map=/build/ruby3.0-ohOwi0/ruby3.0-3.0.2=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC conftest.c -o conftest.i"
conftest.c:3:10: fatal error: ffi.h: No such file or directory
3 | #include <ffi.h>
| ^~~~~~~
compilation terminated.
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <ffi.h>
/* end */
--------------------
解決方法
sudo apt install build-essential
で解決した。