Ruby GEM Mongo
While trying to use the Ruby gem
utility to install the MongoDB gem, I encountered an error on a new Fedora 27 instance. This is the error message:
Fetching: bson-4.3.0.gem (100%) Building native extensions. This could take a while... ERROR: Error installing mongo: ERROR: Failed to build gem native extension. current directory: /usr/local/share/gems/gems/bson-4.3.0/ext/bson /usr/bin/ruby -r ./siteconf20180517-49401-1htl7zc.rb extconf.rb mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h extconf failed, exit code 1 Gem files will remain installed in /usr/local/share/gems/gems/bson-4.3.0 for inspection. Results logged to /usr/local/lib64/gems/ruby/bson-4.3.0/gem_make.out [student@localhost ~]$ ruby --version ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux] |
There wasn’t much on the error but I checked the Ruby installation and ruby-devel
package wasn’t installed by default. That’s odd since I choose to install the development components on the workstation.
Not a problem, I simply ran the yum utility as root
through a sudoer user to install the ruby-devel package:
yum install -y ruby-devel |
You should see a successful installation log like:
Display ruby-devel
Package Log File →
Last metadata expiration check: 2:44:39 ago on Thu 17 May 2018 11:01:10 AM MDT. Dependencies resolved. ================================================================================================ Package Arch Version Repository Size ================================================================================================ Installing: ruby-devel x86_64 2.4.3-87.fc27 updates 119 k Transaction Summary ================================================================================================ Install 1 Package Total download size: 119 k Installed size: 283 k Downloading Packages: ruby-devel-2.4.3-87.fc27.x86_64.rpm 269 kB/s | 119 kB 00:00 ------------------------------------------------------------------------------------------------ Total 118 kB/s | 119 kB 00:01 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : ruby-devel-2.4.3-87.fc27.x86_64 1/1 Verifying : ruby-devel-2.4.3-87.fc27.x86_64 1/1 Installed: ruby-devel.x86_64 2.4.3-87.fc27 Complete! |
As I suspected, it fixed the problem immediately when I ran gem
utility to install the mongo
gem. The syntax to install the mongo
gem is:
gem install mongo |
The console output should be:
Building native extensions. This could take a while... Successfully installed bson-4.3.0 Fetching: mongo-2.5.3.gem (100%) Successfully installed mongo-2.5.3 Parsing documentation for bson-4.3.0 Installing ri documentation for bson-4.3.0 Parsing documentation for mongo-2.5.3 Installing ri documentation for mongo-2.5.3 Done installing documentation for bson, mongo after 3 seconds 2 gems installed |
You can now write and run a Ruby test MongoDB connection program. Here’s a basic MongoDBTest.rb
Ruby file:
#!/usr/bin/ruby # Require libraries. require 'rubygems' require 'mongo' # Create a new connection to MongoDB. $client = Mongo::Client.new(['localhost']) puts 'Connected to MongoDB' puts '====================' puts 'Database Names:' puts '---------------' $client.database_names.each{|name| puts name} |
After you create the MongoDBTest.rb
file, you need to change its permissions with the chmod
utility as follows:
chmod 755 MongoDBTest.rb |
Then, you can run it as follows from the directory where you created the file:
./MongoDBTest.rb |
Unless you’ve added something to the base MongoDB instance, it should print:
Connected to MongoDB ==================== Database Names: --------------- admin local |
As always, I hope this helps somebody looking for a straightforward example and solution.