[RUBY] Resolve "can't find header files" error during gem install

Introduction

Can't find header files error when running bundle install on a machine on an EC2 instance. Make a note of the solution.

environment

--EC2 instance - ubuntu 18.04LTS

Error when bundle install

$ bundle install --path .bundle
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler inv
ocations, which bundler will no longer do in future versions. Instead please use `bundle config set p
ath '.bundle'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/.......
Using bundler 2.1.4
Using multipart-post 2.1.1
Using ruby2_keywords 0.0.2
Using faraday 1.1.0
Using faraday_middleware 1.0.0
Using gli 2.19.2
Using hashie 4.1.0
Using websocket-extensions 0.1.5
Fetching websocket-driver 0.7.3
Installing websocket-driver 0.7.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/ubuntu/slack_bots/.bundle/ruby/2.5.0/gems/websocket-driver-0.7.3/ext/websocket-driver
/usr/bin/ruby2.5 -r ./siteconf20201215-14622-s23dju.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
/home/ubuntu/slack_bots/.bundle/ruby/2.5.0/gems/websocket-driver-0.7.3 for inspection.
Results logged to
/home/ubuntu/slack_bots/.bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/websocket-driver-0.7.3/gem_make.out

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

In Gemfile:
  slack-ruby-client was resolved to 0.15.1, which depends on
    websocket-driver

It seems that an error occurred when installing the gem websocket-driver (0.7.3). As shown in the message, I tried running gem install websocket-driver -v '0.7.3' --source' https://rubygems.org/' by itself, and this time it looks like the following An error has occured.

$ sudo gem install websocket-driver -v '0.7.3' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
ERROR:  Error installing websocket-driver:
	ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/websocket-driver-0.7.3/ext/websocket-driver
/usr/bin/ruby2.5 -r ./siteconf20201215-14722-9uc5to.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/websocket-driver-0.7.3 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/websocket-driver-0.7.3/gem_make.out

It seems that the cause is that the header file for ruby ​​cannot be found as shown in the message mkmf.rb can't find header files for ruby ​​at /usr/lib/ruby/include/ruby.h. I didn't know the solution myself, so if I went through the error message obediently, there was a person who encountered a similar problem, so I referred to it.

-If gem install results in "can't find header files"

Solutions

The solution is to install ruby-dev that matches the version of ruby ​​you are using. This time it was ruby2.5, so install ruby2.5-dev.

$ sudo apt install ruby2.5-dev

Then I ran bundle install and it worked.

$ bundle install --path .bundle
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path '.bundle'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/.......
Using bundler 2.1.4
Using multipart-post 2.1.1
Using ruby2_keywords 0.0.2
Using faraday 1.1.0
Using faraday_middleware 1.0.0
Using gli 2.19.2
Using hashie 4.1.0
Using websocket-extensions 0.1.5
Fetching websocket-driver 0.7.3
Installing websocket-driver 0.7.3 with native extensions
Fetching slack-ruby-client 0.15.1
Installing slack-ruby-client 0.15.1
Bundle complete! 1 Gemfile dependency, 10 gems now installed.
Bundled gems are installed into `./.bundle`

in conclusion

The with native extensions that appears when you install a gem means that the gem depends on the c language library and needs to be compiled separately. This time the library for compiling websocket-driver was not installed on the machine.

-What is with native extensions when bundle install?

Recommended Posts

Resolve "can't find header files" error during gem install
Resolve Gem :: FilePermissionError from nokogiri error caused by bundle install