How to install mongodb from source code

Post Reply
User avatar
binhlq
Posts: 378
Joined: Thu Apr 16, 2020 3:56 am

How to install mongodb from source code

Post by binhlq »

How to install mongodb from source code

Other MongoDB TopicsMongoDB Server Development

server

Jun 2020

7 / 8
Jun 2020

Jun 2020



satya_dommeti

Jun '20

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






kevinadiKevin AdistambhaMongoDB Employee

Jun '20

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:

Code: Select all

$ 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






StennieCultivator

Jun '20



 satya_dommeti:

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 .

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



satya_dommeti

1
Jun '20

Hi @Stennie 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.

Code: Select all

$ 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.

Code: Select all

$ 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

Code: Select all

$ 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

Code: Select all

$/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

Code: Select all

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






StennieCultivator

Jun '20

Hi Satya,

Please make sure you are following the 4.2 version of Building MongoDB 11 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++



 satya_dommeti:

This is my client requirement to provide security.

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 1). 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 1 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

https://www.mongodb.com/community/forum ... ode/5529/7

User avatar
binhlq
Posts: 378
Joined: Thu Apr 16, 2020 3:56 am

Building MongoDB

Post by binhlq »

Building MongoDB

Please note that prebuilt binaries are available on mongodb.org and may be the easiest way to get started, rather than building from source.

To build MongoDB, you will need:

  • A modern C++ compiler capable of compiling C++17. One of the following is required:
    • GCC 8.2 or newer
    • Clang 7.0 (or Apple XCode 10.2 Clang) or newer
    • Visual Studio 2019 version 16.4 or newer (See Windows section below for details)
  • On Linux and macOS, the libcurl library and header is required. MacOS includes libcurl.
    • Fedora/RHEL - dnf install libcurl-devel
    • Ubuntu/Debian - libcurl-dev is provided by three packages. Install one of them:
      • libcurl4-openssl-dev
      • libcurl4-nss-dev
      • libcurl4-gnutls-dev
    • On Ubuntu, the lzma library is required. Install liblzma-dev
  • Python 3.7.x and Pip modules:
    • See the section "Python Prerequisites" below.
  • About 13 GB of free disk space for the core binaries (mongod, mongos, and mongo) and about 600 GB for the install-all target.

MongoDB supports the following architectures: arm64, ppc64le, s390x, and x86-64. More detailed platform instructions can be found below.

MongoDB Tools

The MongoDB command line tools (mongodump, mongorestore, mongoimport, mongoexport, etc) have been rewritten in Go and are no longer included in this repository.

The source for the tools is now available at mongodb/mongo-tools.

Python Prerequisites

In order to build MongoDB, Python 3.7+ is required, and several Python modules must be installed. Note that on macOS the system python is still python2. You will need to use homebrew or macports or similar to obtain python3.

To install the required Python modules, run:

Code: Select all

$ python3 -m pip install -r etc/pip/compile-requirements.txt

Installing the requirements inside a python3 based virtualenv dedicated to building MongoDB is recommended.

Note: In order to compile C-based Python modules, you'll also need the Python and OpenSSL C headers. Run:

  • Fedora/RHEL - dnf install python3-devel openssl-devel
  • Ubuntu (20.04 and newer)/Debian (Bullseye and newer) - apt install python-dev-is-python3 libssl-dev
  • Ubuntu (18.04 and older)/Debian (Buster and older) - apt install python3.7-dev libssl-dev

SCons

If you only want to build the database server mongod:

Code: Select all

$ python3 buildscripts/scons.py install-mongod

Note: For C++ compilers that are newer than the supported version, the compiler may issue new warnings that cause MongoDB to fail to build since the build system treats compiler warnings as errors. To ignore the warnings, pass the switch --disable-warnings-as-errors to scons.

Code: Select all

$ python3 buildscripts/scons.py install-mongod --disable-warnings-as-errors

To install mongod directly to /opt/mongo

Code: Select all

$ python3 buildscripts/scons.py DESTDIR=/opt/mongo install-mongod

To create an installation tree of the servers in /tmp/unpriv that can later be copied to /usr/priv

Code: Select all

$ python3 buildscripts/scons.py DESTDIR=/tmp/unpriv PREFIX=/usr/priv install-servers

If you want to build absolutely everything (mongod, mongo, unit tests, etc):

Code: Select all

$ python3 buildscripts/scons.py install-all-meta

SCons Targets

The following targets can be named on the scons command line to build only certain components:

  • install-mongod
  • install-mongos
  • install-mongo or install-shell
  • install-servers (includes mongod and mongos)
  • install-core (includes mongod, mongos, mongo)
  • install-all

Where to find Binaries

The build system will produce an installation tree into $DESTDIR/$PREFIX. DESTDIR by default is build/install while PREFIX is by default empty. This means that with all of the listed targets all built binaries will be in build/install/bin by default.

Windows

See the windows build manual

Build requirements:

  • Visual Studio 2017 version 15.9 or newer
  • Python 3.7

Or download a prebuilt binary for Windows at www.mongodb.org.

Debian/Ubuntu

To install dependencies on Debian or Ubuntu systems:

Code: Select all

# apt-get install build-essential

OS X

Install Xcode 10.2 or newer.

FreeBSD

Install the following ports:

  • devel/libexecinfo
  • lang/llvm70
  • lang/python

Add CC=clang70 CXX=clang++70 to the scons options, when building.

OpenBSD

Install the following ports:

  • devel/libexecinfo
  • lang/gcc
  • lang/python
https://github.com/mongodb/mongo/blob/m ... uilding.md

User avatar
binhlq
Posts: 378
Joined: Thu Apr 16, 2020 3:56 am

Re: How to install mongodb from source code

Post by binhlq »

python -m pip install regex

python -m pip install -r etc/pip/compile-requirements.txt


pip install -m pywin32

python buildscripts/scons.py MONGO_VERSION=5.1.0 DESTDIR=F:\Trading\mongo-master\build\install\bin install-all

Post Reply

Return to “MongoDB”