How to install mongodb from source code

Hello All,

Hope you’re doing good…

I would like to know how to build mongodb with source code. We have a requirement to install mongodb from source code instead of RPM’s . I downloaded source code from Try MongoDB Atlas Products | MongoDB

Please help me out in this regard.

Thanks,
Satya

Hi @satya_dommeti and welcome to the community!

I would recommend taking a look at the Building MongoDB docs from the public MongoDB GitHub repo.

Best of luck.

2 Likes

Hi @Doug_Duncan Thank you for your response. The document you shared is very helpful.

I am trying build mongodb from source code but getting the below error. It would be great if you help me in this regard.

python3.8 buildscripts/scons.py install-mongod

scons: Reading SConscript files …
scons: running with args /usr/local/bin/python3.8 buildscripts/scons.py install-mongod
Invalid MONGO_VERSION ‘’, or could not derive from version.json or git metadata. Please add a conforming MONGO_VERSION=x.y.z[-extra] as an argument to SCons

OS version :
Red Hat Enterprise Linux Server release 7.6 (Maipo)

Thanks,
Satya

Hi Satya,

I tried to compile mongod using the documentation pointed at by @Doug_Duncan on my Mac, and it seems straightforward. Here’s exactly what I did:

$ git clone https://github.com/mongodb/mongo.git

$ cd mongo

$ git checkout r4.2.8

$ pip3 install -r etc/pip/compile-requirements.txt

$ python3 buildscripts/scons.py mongod
... 25 minutes later ...
Install file: "build/opt/mongo/mongod" as "mongod"
scons: done building targets.

$ build/opt/mongo/mongod --version
db version v4.2.8
git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64 

Seems to be working just fine. One caveat is to check out the build instructions for the version you wanted to build, since for version 4.2.8 that I tried, the build target is called mongod. For the master branch, it’s called install-mongod.

The error you posted: could not derive from version.json or git metadata seems to indicate that you did not do a git clone of the mongo repository, so it doesn’t know which version to build. You might want to check if the step-by-step method using git clone like I did above works first before trying anything else, to confirm that you have the required tools.

Best regards,
Kevin

1 Like

Hi Satya,

Please provide some more detail on your requirements:

  • What version of MongoDB Server (x.y.z) are you trying to build?

    Build requirements and instructions may vary between major server releases, so make sure you are following relevant instructions and have met all of the prerequisites (versions of C++ compiler, Python, and SCons are especially important).

  • What is your motivation for building from source?

    If you aren’t modifying source code, there may be an alternative approach to suggest (for example, offline installation of RPMs for an airgapped server). If you are trying to build a version of MongoDB server that doesn’t have prebuilt binaries for RHEL 7, that would also be helpful context.

Regards,
Stennie

1 Like

Hi @Stennie_X Thanks for your reply. please find my inline comments

What version of MongoDB Server ( x.y.z ) are you trying to build?

I’m trying to build MongoDB server 4.2.3. The error which I posted is resolved after adding a parameter MONGO_VERSION=4.2.3 as below.

$ python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3 

But it is refusing to build with compiler that does not meet requirement.

$ python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3
scons: Reading SConscript files ...
scons: running with args /usr/local/bin/python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3
scons version: 3.0.4
python version: 3 8 3 'final' 0
CC is gcc
gcc found in $PATH at /opt/rh/devtoolset-8/root/usr/bin/gcc
CXX is g++
g++ found in $PATH at /opt/rh/devtoolset-8/root/usr/bin/g++
Checking whether the C compiler works... yes
Checking whether the C++ compiler works... yes
Checking that the C++ compiler can link a C++ program... yes
Checking if C++ compiler "g++" is GCC... yes
Checking if C compiler "gcc" is GCC... yes
Detected a x86_64 processor
Checking if target OS linux is supported by the toolchain... yes
**Checking if C compiler is GCC 8.2 or newer...no**
**Checking if C++ compiler is GCC 8.2 or newer...no**
ERROR: Refusing to build with compiler that does not meet requirements
See /home/mongoadm/mongo-r4.2.3/build/scons/config.log for details
$ tail  /home/mongoadm/mongo-r4.2.3/build/scons/config.log
  |int main(int argc, char* argv[]) {
  |    return 0;
  |}
  |
Compiling build/scons/opt/sconf_temp/conftest_12.o
build/scons/opt/sconf_temp/conftest_12.cpp:7:2: error: #error GCC 8.2 or newer is required to build MongoDB
 #error GCC 8.2 or newer is required to build MongoDB
  ^
scons: Configure: no

It is showing that GCC 8.2 or newer is required to build MongoDB. But I already installed GCC 8.3 version

$/opt/rh/devtoolset-8/root/usr/bin/gcc --version
gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)

$/opt/rh/devtoolset-8/root/usr/bin/g++ --version
g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)

cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.6 (Maipo)

I tried below command also but no luck

python3.8 buildscripts/scons.py install-mongod MONGO_VERSION=4.2.3     --CC=/opt/rh/devtoolset-8/root/usr/bin/gcc --CXX=/opt/rh/devtoolset-8/root/usr/bin/g++ --disable-warnings-as-errors

What is your motivation for building from source?

This is my client requirement to provide security. Client don’t want to install mongodb with RPM’s

Regards,
Satya

Hi Satya,

Please make sure you are following the 4.2 version of Building MongoDB instructions (which should be docs/building.md in your source checkout).

The SCons install-mongod target used in your output is for a newer server release branch (4.4 or master) and should be mongod for 4.2.

It looks like you have an appropriate version of gcc/g++ installed, but perhaps there are conflicting versions in your path.

–CC=/opt/rh/devtoolset-8/root/usr/bin/gcc --CXX=/opt/rh/devtoolset-8/root/usr/bin/g++

These options should be set as environment variables, not as parameters. Try including these in your SCons command line without the leading --:

CC=/opt/rh/devtoolset-8/root/usr/bin/gcc CXX=/opt/rh/devtoolset-8/root/usr/bin/g++

I noticed you are building MongoDB 4.2.3. The latest 4.2 release is currently 4.2.8 (see MongoDB 4.2 Minor Releases). I strongly recommend staying current with minor releases for the latest security and stability fixes.

If your client is concerned about security and stability, the official packages should provide more assurance on that front than building from source. All builds are extensively tested via our public (and open source) Evergreen CI and published packages are signed for verification.

If you build from source, there may be variations in your build environment that cause unexpected issues.

Regards,
Stennie

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.