[ruby-on-rails] Unable to install gem - Failed to build gem native extension - cannot load such file -- mkmf (LoadError)

Ruby 1.9.3

The part of Gemfile

#...............
gem "pony"
gem "bcrypt-ruby", :require => "bcrypt"
gem "nokogiri" 
#..................

When I'm trying to install gems, I get an error

alex@ubuntu:~/$ bundle
Fetching gem metadata from http://rubygems.org/.........
Fetching gem metadata from http://rubygems.org/..
Enter your password to install the bundled RubyGems to your system: 
#####............................................................
Installing bcrypt-ruby (3.0.1) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb 
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
        from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from extconf.rb:36:in `<main>'


Gem files will remain installed in /home/alex/.bundler/tmp/5526/gems/bcrypt-ruby-3.0.1 for inspection.
Results logged to /home/alex/.bundler/tmp/5526/gems/bcrypt-ruby-3.0.1/ext/mri/gem_make.out
An error occurred while installing bcrypt-ruby (3.0.1), and Bundler cannot continue.
Make sure that `gem install bcrypt-ruby -v '3.0.1'` succeeds before bundling.

Then I'm doing this

sudo gem install bcrypt-ruby -v '3.0.1'
Building native extensions.  This could take a while...
ERROR:  Error installing bcrypt-ruby:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
        from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from extconf.rb:36:in `<main>'


Gem files will remain installed in /var/lib/gems/1.9.1/gems/bcrypt-ruby-3.0.1 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/bcrypt-ruby-3.0.1/ext/mri/gem_make.out

and getting an error as well.

What did I miss?

This question is related to ruby-on-rails ruby ubuntu gem bcrypt

The answer is


What ended up working for me after a few hours of pain..

if you're running brew..

brew install ruby

in the terminal output/log, identify the path where ruby was installed, brew suggests 'You may want to add this to your PATH', so that's what we'll do. For example, mine is

/usr/local/lib/ruby/gems/3.0.0/bin

Add this to your path by running (omitting braces)

echo 'export PATH"{the_path_you_found_above}:$PATH"' >> ~/.bash_profile

then update your environment by running

source ~/.bash_profile

now, try running your install, i.e.,

sudo gem install middleman

I created a small hackMD to install cocoapods on MacOS 10.15 (Catalina) and 11 (Big Sur)

https://hackmd.io/@sBJPlhRESGqCKCqV8ZjP1A/S1UY3W7HP

Installing Cocoapods on MacOS Catalina(MacOS 10.15.X) and Big Sur (MacOS 11)

  1. Make sure you have xcode components are installed.

  2. Download 'Command Line Tools' (about 500MB) directly from this link (Requires you to have apple account) https://developer.apple.com/downloads/index.action

  3. Install the downloaded file

  4. Click on Install

  5. Install COCOAPODS files in terminal sudo gem install -n /usr/local/bin cocoapods


After some search for a solution, it turns out the -dev package is needed, not just ruby1.8. So if you have ruby1.9.1 doing

sudo apt-get install ruby1.9.1-dev

or to install generic ruby version, use (as per @lamplightdev comment):

sudo apt-get install ruby-dev

should fix it.

Try to locate mkmf to see if the file is actually there.


  1. Make sure ruby-dev is installed
  2. Make sure make is installed
  3. If you still get the error, look for suggested packages. If you are trying to install something like gem install pg you will also need to install the lib libpq-dev (sudo apt-get install libpq-dev).

I had the same issue trying to install jquery-rails. The fix was

sudo apt-get install zlibc zlib1g zlib1g-dev

I found that I needed to install another version of ruby. So running the command

$ sudo apt-get install ruby1.9.1-dev

and then attempt to install the extension

If you run into issues where it is telling you that you don't have g++ you can run the following command to install it

$ sudo apt-get install g++

In Mac, for me this works:

CONFIGURE_OPTS="--enable-shared" rbenv install 2.2.2

For MacOS users:

Just do this and easily it will solve your problem:

brew install cocoapods

This worked for me. bundle config --global build.snappy --with-opt-dir="$(brew --prefix snappy)"


In case anyone in the future had this problem, I'm using a Mac and just had to install the Command Line Tools using 'xcode-select --install'


Just finished a 2 hour wild goose chase trying to solve this. None of the posted answers worked for me. Im on a Mac (Mojave Version 10.14.6, Xcode Version 11.3).

It turns out the ruby file headers were missing so i had to run open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

That didnt work for me at first because the version of CommandLineTools i had installed did not have the "Packages" folder. So i uninstalled and reinstalled like this:

rm -rf /Library/Developer/CommandLineTools

xcode-select --install

Then i ran the previous command again:

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

After install the error was fixed!


It also helps to ensure libmysqlclient-dev is installed (Ubuntu 14.04)


For WSL (Windows Subsystem for Linux) you need install build-essential package:

sudo apt install build-essential

Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found

Examples related to gem

Embed ruby within URL : Middleman Blog Why won't bundler install JSON gem? Installing PG gem on OS X - failure to build native extension How to install a specific version of a ruby gem? Unable to install gem - Failed to build gem native extension - cannot load such file -- mkmf (LoadError) How to upgrade rubygems "Could not find a valid gem in any repository" (rubygame and others) Pull is not possible because you have unmerged files, git stash doesn't work. Don't want to commit Uninstall old versions of Ruby gems Could not locate Gemfile

Examples related to bcrypt

How to decrypt hash stored by bcrypt Unable to install gem - Failed to build gem native extension - cannot load such file -- mkmf (LoadError) How can bcrypt have built-in salts? What column type/length should I use for storing a Bcrypt hashed password in a Database? How do you use bcrypt for hashing passwords in PHP?