Getting startedBuild Memgraph from source

Build Memgraph from source

Follow this guide if you want to compile a Memgraph from a source. Here you will find all the necessary steps, including setting up the necessary toolchain, compiling the code, and running Memgraph.

💡

Memgraph uses git for source version control. You will need to install git on your machine before you can download the source code.

💡

If you are using Mac M1 or above, please check our MacOS Lima Compilation Guide first.

Obtain the Source Code

After installing git, you are now ready to fetch your own copy of the Memgraph source code. Run the following command:

git clone git@github.com:memgraph/memgraph.git

The above will create a memgraph directory and put all source code there.

In your terminal, position yourself in the obtained memgraph directory.

cd memgraph

Choose build method

There are three different methods for building Memgraph:

  1. Using the build.sh script (Recommended)
  2. Manually with conan and cmake (Advanced)
  3. Docker (ideal for unsupported systems)

Before using methods 1 or 2, you must download dependencies and install the toolchain.

Download dependencies (required for methods 1 & 2)

Before you can compile Memgraph, you first need to download its dependencies.

Building Memgraph depends on some system-wide packages. The installation scripts can be found under environment/os/. The directory contains a dependencies management script for each supported operating system.

sudo ./environment/os/install_deps.sh install TOOLCHAIN_RUN_DEPS
sudo ./environment/os/install_deps.sh install MEMGRAPH_BUILD_DEPS
  • The first command installs all the packages necessary for the Memgraph toolchain to work correctly.
  • The second command installs the system-wide packages that are not necessary for the toolchain but are required by Memgraph.
💡

Based on your OS, version and architecture, execute appropriate scripts (e.g., on Debian 12, NOTE: the following commands/scripts are located under memgraph repository:

sudo ./environment/os/debian-12.sh install TOOLCHAIN_RUN_DEPS
sudo ./environment/os/debian-12.sh install MEMGRAPH_BUILD_DEPS

For ARM look for -arm in the script name (e.g., on Ubuntu 24.04 with Apple M processor):

sudo ./environment/os/ubuntu-24.04-arm.sh install TOOLCHAIN_RUN_DEPS
sudo ./environment/os/ubuntu-24.04-arm.sh install MEMGRAPH_BUILD_DEPS

Once everything is installed, you can proceed to the compilation.

Toolchain installation (required for methods 1 & 2)

Memgraph is compiled using our own custom toolchain that can be obtained from the toolchain repository. All our tools used in the development of Memgraph are manually built for each OS and collected in an archive. Please refer to the memgraph/environment folder for all related code.

💡

Keep in mind, cargo is also required to build Memgraph, follow the official Rust installation guide.

You should read the Toolchain to install the appropriate toolchain for your distribution.

Download the toolchain for your operating system from one of the following links:

Extract the toolchain with the following command:

sudo tar xzvfm {{toolchain-archive}}.tar.gz -C /opt
💡

After you have installed the toolchain, you should read the instructions for the toolchain in the toolchain install directory (/opt/toolchain-v7/README.md) and install dependencies that are necessary to run the toolchain.

If you have not already installed toolchain dependencies, please check and install required toolchain runtime dependencies as in the section above.

💡

If you put the toolchain on some other path, it’s possible to say to the cmake the root is there, that is done by setting MG_TOOLCHAIN_ROOT environment variable.

That’s also useful where you have different versions of libraries under system compare to what the Memgraph build process needs.

Use build.sh script

This method is the easiest way to build Memgraph directly on a compatible host system.

Calling the script with no arguments will build everything, including tests, for the Release build type:

./build.sh

Optionally you can specify target and build types, e.g.

./build.sh --target memgraph --build-type Debug

where the build type can be Release (default), Debug or RelWithDebInfo.

Other cmake options can be passed as well, e.g. to disable testing:

./build.sh --target memgraph --build-type Release -DMG_ENABLE_TESTING=OFF

The resulting binary will be in the build/ directory.

Use conan and cmake

This approach offers greater flexibility and granular control over the build process. While it performs the same operations as the build.sh script, it gives you direct access to configure individual build parameters and dependencies.

To build Memgraph using this method, follow these steps:

Create a Python virtual environment and install conan

python3 -m venv env
source env/bin/activate
pip install conan
conan profile detect

Run the init script to fetch other libs required for the build

./init

Install conan dependencies

export MG_TOOLCHAIN_ROOT=/opt/toolchain-v7
conan install . --build=missing -pr ./memgraph_template_profile -s build_type=Release
source build/generators/conanbuild.sh

Configure the project with cmake

Note that the name of the preset depends on the build type set in the previous step.

cmake --preset conan-release

Build the project

The following command will build everything, including tests.

cmake --build --preset conan-release -j$(nproc)

Or for a specific target (e.g. Memgraph binary)

cmake --build --preset conan-release --target memgraph -j$(nproc)

Use Docker

Start by setting the environment variables for the desired OS, architecture, and build type:

export OS="ubuntu-24.04"
export ARCH="amd"  # or arm
export BUILD_TYPE="Release"
export TOOLCHAIN="v7"

Next, pull the appropriate Docker image for the build, e.g.:

docker pull memgraph/mgbuild:v7_ubuntu-24.04

Available image tags can be found here.

Then spin up the container:

./release/package/mgbuild.sh \
  --toolchain $TOOLCHAIN \
  --os $OS \
  --arch $ARCH \
  --build-type $BUILD_TYPE \
  run

Then build Memgraph:

./release/package/mgbuild.sh \
  --toolchain $TOOLCHAIN \
  --os $OS \
  --arch $ARCH \
  --build-type $BUILD_TYPE \
  build-memgraph

Run Memgraph inside the container (the name of the container will look something like mgbuild_v7_ubuntu-24.04):

# find the name of the container
docker ps
 
# run Memgraph
docker exec -i mgbuild_v7_ubuntu-24.04 bash -c "cd /home/mg/memgraph && ./build/memgraph"

Stop the container:

./release/package/mgbuild.sh \
  --toolchain $TOOLCHAIN \
  --os $OS \
  --arch $ARCH \
  stop --remove

Run Memgraph

After the compilation, verify that Memgraph works:

./memgraph --version

The unit tests can be run using:

ctest -R unit -j$(nproc)

Or in Docker:

./release/package/mgbuild.sh \
  --toolchain $TOOLCHAIN \
  --os $OS \
  --arch $ARCH \
  --enterprise-license $MEMGRAPH_ENTERPRISE_LICENSE \
  --organization-name $MEMGRAPH_ORGANIZATION_NAME \
  test-memgraph unit