How to Solve Error Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

Posted on

Introduction

This article will show how to solve the specific error in the title. The error appears as part of a process for installing metasploit framework. The installation itself is using the ‘bundle’ tool. It is because metasploit framework is written in Ruby. Below is the complete output showing the error message :

user@hostname:/opt/metasploit-framework$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Following files may not be writable, so sudo is needed:
  /usr/local/bin
  /var/lib/gems/2.5.0
  /var/lib/gems/2.5.0/build_info
  /var/lib/gems/2.5.0/cache
  /var/lib/gems/2.5.0/doc
  /var/lib/gems/2.5.0/extensions
  /var/lib/gems/2.5.0/gems
  /var/lib/gems/2.5.0/specifications
Fetching rake 12.3.2
Your user account isn't allowed to install to the system RubyGems.
  You can cancel this installation and run:
      bundle install --path vendor/bundle
  to install the gems into ./vendor/bundle/, or you can enter your password
  and install the bundled gems to RubyGems using sudo.
  Password: 
Installing rake 12.3.2
Fetching Ascii85 1.0.3
Installing Ascii85 1.0.3
Fetching concurrent-ruby 1.0.5
Installing concurrent-ruby 1.0.5
Fetching i18n 0.9.5
Installing i18n 0.9.5
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 4.2.11
Installing activesupport 4.2.11
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubis 2.7.0
Installing erubis 2.7.0
Fetching mini_portile2 2.4.0
Installing mini_portile2 2.4.0
Fetching nokogiri 1.10.1
Installing nokogiri 1.10.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
    current directory: /tmp/bundler20190215-6232-1yag4b9nokogiri-1.10.1/gems/nokogiri-1.10.1/ext/nokogiri
/usr/bin/ruby2.5 -r ./siteconf20190215-6232-tyetzx.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1
Gem files will remain installed in /tmp/bundler20190215-6232-1yag4b9nokogiri-1.10.1/gems/nokogiri-1.10.1 for inspection.
Results logged to /tmp/bundler20190215-6232-1yag4b9nokogiri-1.10.1/extensions/x86_64-linux/2.5.0/nokogiri-1.10.1/gem_make.out
An error occurred while installing nokogiri (1.10.1), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.1' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
  factory_bot_rails was resolved to 5.0.1, which depends on
    railties was resolved to 4.2.11, which depends on
      actionpack was resolved to 4.2.11, which depends on
        actionview was resolved to 4.2.11, which depends on
          rails-dom-testing was resolved to 1.0.9, which depends on
            nokogiri
user@hostname:/opt/metasploit-framework$

The output above happens after solving the problem as in the full description in the article in this link.

Solution

The problem is a gem with the name of ‘nokogiri’, specifically with the version of ‘1.10.1’. The installation of the gem fails. So, in order to solve the problem as in the description in the previous section, just do the following steps :

1. First of all, install a new package which is the solution for the problem. The package name is ‘ruby-dev’. Type the following command to install that package :

user@hostname:/opt/metasploit-framework$ sudo apt-get -y install ruby-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  cabextract fonts-wine gstreamer1.0-plugins-base:i386 gyp icoutils libasn1-8-heimdal:i386 libasound2:i386 libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386 libavahi-common-data:i386
  libavahi-common3:i386 libbsd0:i386 libcairo2:i386 libcap2:i386 libcapi20-3 libcapi20-3:i386 libcdparanoia0:i386 libcups2:i386 libdbus-1-3:i386 libexif12:i386 libflac8:i386 libfontconfig1:i386
  libfreetype6:i386 libgd3:i386 libgl1:i386 libgl1-mesa-glx:i386 libglib2.0-0:i386 libglu1-mesa:i386 libglvnd0:i386 libglx-mesa0:i386 libglx0:i386 libgmp10:i386 libgnutls30:i386 libgphoto2-6:i386
  libgphoto2-port12:i386 libgsm1:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386 libgstreamer-plugins-base1.0-0:i386 libgstreamer1.0-0:i386 libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386
  libheimntlm0-heimdal:i386 libhogweed4:i386 libhx509-5-heimdal:i386 libidn2-0:i386 libieee1284-3:i386 libjack-jackd2-0:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386 libjs-async libjs-inherits
  libjs-node-uuid libk5crypto3:i386 libkeyutils1:i386 libkrb5-26-heimdal:i386 libkrb5-3:i386 libkrb5support0:i386 liblcms2-2:i386 libldap-2.4-2:i386 libllvm6.0:i386 libltdl7:i386 libmpg123-0:i386
  libmspack0 libnettle6:i386 libodbc1 libodbc1:i386 libogg0:i386 libopenal1:i386 libopus0:i386 liborc-0.4-0:i386 libosmesa6 libosmesa6:i386 libp11-kit0:i386 libpcap0.8:i386 libpixman-1-0:i386
  libpng16-16:i386 libpulse0:i386 libroken18-heimdal:i386 libsamplerate0:i386 libsane1:i386 libsasl2-2:i386 libsasl2-modules-db:i386 libsndfile1:i386 libsndio6.1:i386 libspeexdsp1:i386 libsqlite3-0:i386
  libssl1.1:i386 libtasn1-6:i386 libtheora0:i386 libtiff5:i386 libunistring2:i386 libusb-1.0-0:i386 libuv1-dev libv4l-0:i386 libv4lconvert0:i386 libvisual-0.4-0:i386 libvorbis0a:i386 libvorbisenc2:i386
  libwebp6:i386 libwind0-heimdal:i386 libwine libwine:i386 libwrap0:i386 libwxgtk3.0-gtk3-0v5 libx11-6:i386 libx11-xcb1:i386 libxau6:i386 libxcb-dri2-0:i386 libxcb-dri3-0:i386 libxcb-glx0:i386
  libxcb-present0:i386 libxcb-render0:i386 libxcb-shm0:i386 libxcb-sync1:i386 libxcb1:i386 libxcomposite1:i386 libxcursor1:i386 libxdamage1:i386 libxdmcp6:i386 libxext6:i386 libxfixes3:i386 libxi6:i386
  libxinerama1:i386 libxml2:i386 libxpm4:i386 libxrandr2:i386 libxrender1:i386 libxshmfence1:i386 libxslt1.1:i386 libxxf86vm1:i386 node-abbrev node-ansi node-ansi-color-table node-archy node-async
  node-balanced-match node-block-stream node-brace-expansion node-builtin-modules node-combined-stream node-concat-map node-cookie-jar node-delayed-stream node-forever-agent node-form-data
  node-fs.realpath node-fstream node-fstream-ignore node-github-url-from-git node-glob node-graceful-fs node-hosted-git-info node-inflight node-inherits node-ini node-is-builtin-module node-isexe
  node-json-stringify-safe node-lockfile node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once node-osenv
  node-path-is-absolute node-pseudomap node-qs node-read node-read-package-json node-request node-retry node-rimraf node-semver node-sha node-slide node-spdx-correct node-spdx-expression-parse
  node-spdx-license-ids node-tar node-tunnel-agent node-underscore node-validate-npm-package-license node-which node-wrappy node-yallist ocl-icd-libopencl1 ocl-icd-libopencl1:i386 pkg-php-tools
  python-wxgtk3.0 python-wxversion shtool wine-stable wine32:i386 wine64
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  ruby2.5-dev ruby2.5-doc
The following NEW packages will be installed:
  ruby-dev ruby2.5-dev ruby2.5-doc
0 upgraded, 3 newly installed, 0 to remove and 32 not upgraded.
Need to get 1.874 kB of archives.
After this operation, 18,6 MB of additional disk space will be used.
Get:1 http://xxx.xxxxxxxxxx.xxxxxx.xxx/ubuntu bionic-updates/main amd64 ruby2.5-dev amd64 2.5.1-1ubuntu1.1 [63,7 kB]
Get:2 http://xxx.xxxxxxxxxx.xxxxxx.xxx/ubuntu bionic/main amd64 ruby-dev amd64 1:2.5.1 [4.604 B]
Get:3 http://xxx.xxxxxxxxxx.xxxxxx.xxx/ubuntu bionic-updates/main amd64 ruby2.5-doc all 2.5.1-1ubuntu1.1 [1.806 kB]
Fetched 1.874 kB in 4s (469 kB/s)        
Selecting previously unselected package ruby2.5-dev:amd64.
(Reading database ... 338278 files and directories currently installed.)
Preparing to unpack .../ruby2.5-dev_2.5.1-1ubuntu1.1_amd64.deb ...
Unpacking ruby2.5-dev:amd64 (2.5.1-1ubuntu1.1) ...
Selecting previously unselected package ruby-dev:amd64.
Preparing to unpack .../ruby-dev_1%3a2.5.1_amd64.deb ...
Unpacking ruby-dev:amd64 (1:2.5.1) ...
Selecting previously unselected package ruby2.5-doc.
Preparing to unpack .../ruby2.5-doc_2.5.1-1ubuntu1.1_all.deb ...
Unpacking ruby2.5-doc (2.5.1-1ubuntu1.1) ...
Setting up ruby2.5-dev:amd64 (2.5.1-1ubuntu1.1) ...
Setting up ruby2.5-doc (2.5.1-1ubuntu1.1) ...
Setting up ruby-dev:amd64 (1:2.5.1) ...
user@hostname:/opt/metasploit-framework$

2. Next step, try to install the package again as follows :

user@hostname:/opt/metasploit-framework$ sudo gem install nokogiri -v '1.10.1'
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.1
Parsing documentation for nokogiri-1.10.1
Installing ri documentation for nokogiri-1.10.1
Done installing documentation for nokogiri after 6 seconds
1 gem installed
user@hostname:/opt/metasploit-framework$

3. Finally, try to execute the above command again to check if the solution is working or not.

Leave a Reply