Download and Install
Download the C driver
The C driver consists of two libraries: libbson
and
libmongoc
. In this guide, you will learn how to install libmongoc
,
which comes bundled with libbson
.
You can install the C driver by using a package manager, or by installing the libraries from source. Different Linux distributions install different versions of the driver when installed with a package manager. Because of this, you should check the version your distribution includes before installing the driver.
On RedHat Enterprise Linux (RHEL) distributions, you can check the version by running the following command in your shell:
sudo dnf info mongo-c-driver-devel
On Debian-based distributions, you can check the version by running the following command in your shell:
apt-cache policy libmongoc-dev
If the version of the driver that is available is not the version you want, you can install the driver from source. To learn how to install the driver from source, see the Building the C Driver Libraries from Source guide.
To learn how to install the driver by using a package manager, select the tab corresponding to your operating system:
RedHat Enterprise Linux Distributions
You can install the driver on RedHat Enterprise Linux (RHEL) distributions, version 7 or later, or distributions based on RHEL, such as:
Before you install the driver on a RHEL distribution, not including
Fedora, you must first enable the Extra Packages for Enterprise
Linux (EPEL) repository. You can enable the EPEL repository by
installing the epel-release
package, as shown in the following
example:
yum install epel-release
Then, you can install the C driver
by using yum
. To do so, run the following command in
your shell:
yum install mongo-c-driver-devel
Note
Developer Packages
The preceding code example installs the developer packages, which are required to build applications that use the C driver. To install only the libraries, without development files, run the following command:
yum install mongo-c-driver
If you are installing on a RHEL distribution that is version 8 or
later, we recommend running the preceding code examples by using
dnf
instead of yum
.
Fedora
If you are using Fedora or an enterprise Linux distribution of
version 8 or later, you can install the driver by using the same
commands as shown in the preceding example
and replacing yum
with dnf
.
The following example shows how to install the C driver in a Fedora distribution:
dnf install mongo-c-driver-devel
Debian-Based Distributions
In Debian-Based Linux distributions, including Ubuntu and Ubuntu derivatives, the driver libraries are available through the Advanced Packaging Tool (APT). You can install the driver with APT by running the following command in your shell:
apt install libmongoc-dev
The preceding code example installs the developer packages, which are required to build applications that use the C driver. To install only the libraries, without development files, run the following command:
apt install libmongoc-1.0-0
For Windows environments, we recommend building the C driver libraries from source to ensure maximum compatibility with the local toolchain. You can automate building from source by using a from-source library package management tool, such as vcpkg.
Tip
To learn how to get started with vcpkg, see the vcpkg Get Started guide in the Microsoft documentation.
In vcpkg manifest mode, add the
driver library to your project's vcpkg.json
manifest file,
as shown in the following example:
{ // ... "dependencies": [ // ... "mongo-c-driver" ] }
When you build a CMake project with vcpkg integration and have a
vcpkg.json
manifest file, vcpkg automatically installs the project's
dependencies before proceeding with configuration.
If you are using vcpkg classic mode, you can install the driver by running the following command in your shell:
vcpkg install mongo-c-driver
After you install the driver, the standard CMake configuration file
is available, as well as the generated IMPORTED
targets. You can
create a CMakeLists.txt
file as shown in the following example:
find_package(mongoc-1.0 CONFIG REQUIRED) target_link_libraries(my-application PRIVATE $<IF:$<TARGET_EXISTS:mongo::mongoc_shared>,mongo::mongoc_shared,mongo::mongoc_static>)
Note
You can use the $<IF:$<TARGET_EXISTS...>:...>
generator
expression in the preceding example to switch the link type of
libmongoc
based on the form that is available from the
find_package()
command. libmongoc
supports building with
both dynamic and static library types, but vcpkg
installs only one type at a time.
To configure the CMake project with vcpkg integration, set the CMake toolchain file in the configuration command by running the following commands in your shell:
cmake -S . -B _build -D CMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
You can download the C driver on MacOS by using the Homebrew package manager. To do so, run the following command in your shell:
brew install mongo-c-driver
After you complete these steps, you have the C driver installed on your machine.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.