Failed to build gem native extension


#1

Hi,
I need gem 'mongo' to be installed with metadata.rb file.
When I try to bootstrap new server (Ubuntu 18.04, chef-client 14.0.202) I get

Installing Cookbook Gems:

Running handlers:
[2018-05-04T13:49:39+00:00] ERROR: Running exception handlers
Running handlers complete
[2018-05-04T13:49:39+00:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 13 seconds                                                                                             
[2018-05-04T13:49:39+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2018-05-04T13:49:39+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2018-05-04T13:49:39+00:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '5'
---- Begin output of bundle install ----
STDOUT: Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Fetching gem metadata from https://www.rubygems.org/..........
Resolving dependencies...
Fetching bson 4.3.0
Installing bson 4.3.0 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/bson-4.3.0/ext/bson
/opt/chef/embedded/bin/ruby -r ./siteconf20180504-3942-1qctwp.rb extconf.rb
creating Makefile

current directory:
/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/bson-4.3.0/ext/bson
make "DESTDIR=" clean
sh: 1: make: not found

current directory:
/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/bson-4.3.0/ext/bson
make "DESTDIR="
sh: 1: make: not found

make failed, exit code 127

Gem files will remain installed in
/opt/chef/embedded/lib/ruby/gems/2.5.0/gems/bson-4.3.0 for inspection.
Results logged to
/opt/chef/embedded/lib/ruby/gems/2.5.0/extensions/x86_64-linux/2.5.0/bson-4.3.0/gem_make.out

An error occurred while installing bson (4.3.0), and Bundler cannot continue.
Make sure that `gem install bson -v '4.3.0'` succeeds before bundling.

In Gemfile:
  mongo was resolved to 2.5.2, which depends on
    bson
STDERR: 
---- End output of bundle install ----
Ran bundle install returned 5

If I remove it from metadata.rb, bootsrap server and then put it back it installs.


#2

This is a quite common problem with cookbook gems. Cookbook gems are installed as one of the first steps of the chef run so they can be used during the chef run. However, some gems require packages like gcc and make to be installed. Its kind of a Hen-Egg problem. You cookbook installs the dependencies required to install the gem but the gem is required during the chef run.
There are many many ways to solve this but in the end you will need some kind of bootstrapping mechanism (which could also be a chef cookbook that runs independently via “chef-client -o my-company-bootstrap”) that has little to no outside dependencies and runs prior to everything else.