-
Notifications
You must be signed in to change notification settings - Fork 56
Building Ruby MongoDB Driver
Below versions of Ruby Driver for MongoDB are available in respective distributions at the time of creation of these build instructions:
- RHEL (8.8, 8.10) have
2.5.1
- Ubuntu (20.04, 22.04, 24.04, 24.10) have
2.5.1-1
The instructions provided below specify the steps to build MongoDB Ruby Driver version 2.21.0 on Linux on IBM Z for following distributions:
- RHEL (8.8, 8.10, 9.2, 9.4, 9.5)
- SLES 15 SP6
- Ubuntu (20.04, 22.04, 24.04, 24.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
export SOURCE_ROOT=/<source_root>/
-
MongoDB Ruby driver requires Ruby version >= 2.6. Instructions to build the latest Ruby are available here.
-
RHEL (8.8, 8.10)
sudo yum install -y make gcc redhat-rpm-config wget
-
RHEL (9.2, 9.4, 9.5)
sudo yum install -y make gcc ruby ruby-devel redhat-rpm-config
-
SLES 15 SP6
sudo zypper install -y make gcc wget
-
Ubuntu (20.04, 22.04, 24.04, 24.10)
sudo apt-get update sudo apt-get install -y make gcc ruby ruby-dev
wget -q https://raw.githubusercontent.com/linux-on-ibm-z/scripts/master/Ruby/3.3.5/build_ruby.sh
bash build_ruby.sh -y
sudo gem install mongo -v 2.21.0 # RHEL, SLES 15.6 and Ubuntu
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.
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
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.21.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.21.0"}
{"_id"=>1, "line"=>1}
{"_id"=>2, "line"=>2}
{"_id"=>3, "line"=>3}
- MongoDB Ruby Driver Overview - https://docs.mongodb.com/ruby-driver/current/
The information provided in this article is accurate at the time of writing, but on-going development in the open-source projects involved may make the information incorrect or obsolete. Please open issue or contact us on IBM Z Community if you have any questions or feedback.