Error: An error occurred while installing nokogiri, and Bundler cannot continue on Ubuntu

install-nokogiri-error-ubuntu

Issue

I got this error on when try to install nokogiri on a Ubuntu 18.10 server. It seems like I've not install the required packages correct on the system.

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

    current directory: /root/alert/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.3/ext/nokogiri
/usr/bin/ruby2.5 -I /usr/local/lib/site_ruby/2.5.0 -r ./siteconf20190630-30475-1wuk0lw.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 /root/alert/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.3 for inspection.
Results logged to /root/alert/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/nokogiri-1.10.3/gem_make.out

An error occurred while installing nokogiri (1.10.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  nokogiri

root@server:~/alert# gem install nokogiri -v '1.10.3' --source 'https://rubygems.org/'
Fetching nokogiri-1.10.3.gem
Fetching mini_portile2-2.4.0.gem
Successfully installed mini_portile2-2.4.0
Building native extensions. This could take a while...
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/nokogiri-1.10.3/ext/nokogiri
/usr/bin/ruby2.5 -I /usr/local/lib/site_ruby/2.5.0 -r ./siteconf20190630-30630-1ukvdw7.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 /var/lib/gems/2.5.0/gems/nokogiri-1.10.3 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/nokogiri-1.10.3/gem_make.out

Solution

If you also got this issue on Ubuntu, use the following steps. The installation document is list on the official site of nokogiri, you can also check it out on that website.

sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev
gem install nokogiri
bundle