Skip to content

Building Ruby MongoDB Driver

linuxonz edited this page Apr 12, 2024 · 44 revisions

Building the Ruby Driver for MongoDB

Below versions of Ruby Driver for MongoDB are available in respective distributions at the time of creation of these build instructions:

  • Ubuntu (20.04, 22.04, 23.10) have 2.5.1-1
  • RHEL (8.6, 8.8, 8.9) have 2.5.1

The instructions provided below specify the steps to build MongoDB Ruby Driver version 2.20.0 on Linux on IBM Z for following distributions:

  • RHEL (7.8, 7.9, 8.6, 8.8, 8.9, 9.0, 9.2, 9.3)
  • SLES (12 SP5, 15 SP5)
  • Ubuntu (20.04, 22.04, 23.10)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it

Step 1: Build and Install MongoDB Ruby Driver

1.1) Install the build dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (7.8, 7.9)

    sudo yum install -y make gcc
    
    • MongoDB Ruby driver requires Ruby version >= 2.5. Instructions to build the latest Ruby are available here.
  • RHEL (8.6, 8.8, 8.9)

    sudo yum install -y make gcc redhat-rpm-config wget
    
  • RHEL (9.0, 9.2, 9.3)

    sudo yum install -y make gcc ruby ruby-devel redhat-rpm-config
    
  • SLES 12 SP5

    sudo zypper install -y make gcc 
    
    • MongoDB Ruby driver requires Ruby version >= 2.5. Instructions to build the latest Ruby are available here.
  • SLES (15 SP5)

    sudo zypper install -y make gcc wget
    
  • Ubuntu (20.04, 22.04, 23.10)

    sudo apt-get update
    sudo apt-get install -y make gcc ruby ruby-dev
    

1.2) Install ruby from script (RHEL 8.x, SLES 15.x)

wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Ruby/3.3.0/build_ruby.sh
bash build_ruby.sh -y  

1.3) Install the mongo gem

sudo gem install mongo -v 2.20.0                    # RHEL 8.x, 9.x, SLES 15.x and Ubuntu
sudo env PATH=$PATH gem install mongo -v 2.20.0     # RHEL 7.x and SLES 12.x        

Step 2: Basic validation test

The example code section given below is used to perform a basic test to ensure that the MongoDB Ruby Driver is working as expected, and can connect to, modify and query a MongoDB server. Instructions to install MongoDB can be found on their official website here.

2.1) Start MongoDB

To run this test, MongoDB must be running on the default port, 27017. The following commands are an example of how to start a MongoDB server and then connect to it with the client shell.

Issue the following command to start mongod and to check if it is up, connect to it with the MongoDB shell.

mongod > /tmp/mongodb.log &
mongosh --host localhost --port 27017

2.2) The Test Code

Create a file named test.rb with the content shown below.
If you are connecting to a remote server then you need to substitute the localhost with the hostname or IP address of the MongoDB server.

require 'mongo'

server="localhost"
test_db='ibm_test_db'
collection='mongodb_ruby_driver_2_x'

server_addr=server + ":27017"

Mongo::Logger.logger.level = ::Logger::FATAL   # hide DEBUG logging

db = Mongo::Client.new([ server_addr ], :database => test_db)

db[:collection].drop

result = db[:collection].insert_one({ company: 'IBM' , project: 'MongoDB Driver', language: 'Ruby', version: '2.20.0'})

3.times { |i| db[:collection].insert_one({ _id: i+1, line: i+1 }) }

db[:collection].find().each do |document|
   printf("%s\n", document) #=> Yields a BSON::Document.
end

Execute the test program by:

ruby test.rb

Executing the test program should produce a similar output to this (the Object IDs will vary):

{"_id"=>BSON::ObjectId('5cc9a7de87e6fd31c75b5fd9'), "company"=>"IBM", "project"=>"MongoDB Driver", "language"=>"Ruby", "version"=>"2.20.0"}
{"_id"=>1, "line"=>1}
{"_id"=>2, "line"=>2}
{"_id"=>3, "line"=>3}

References:

Clone this wiki locally