# Package Memgraph

This guide will show you how to package Memgraph as a `.deb` or `.rpm` package. 
There are three main ways to package Memgraph:

- [Using the `mgbuild.sh` script](#using-the-mgbuildsh-script), which builds Memgraph in a Docker container. (Recommended)
- [Using the `package.sh` script](#using-the-packagesh-script), which packages a local build directory.
- [Using CPack](#using-cpack), which invokes CPack manually.

> **Note**
>
> As of version 3.13 the produced packages are **distro-agnostic**: one `.deb`
>     (all Debian-family distributions) and one `.rpm` (all RHEL-family
>     distributions) per architecture, running on any Linux with glibc 2.31 or
>     newer. The `OS` value below only selects the Docker **build environment**,
>     not the target distribution. Available build environments: `centos-9`,
>     `centos-10`, `debian-12`, `debian-13`, `fedora-42`, `fedora-43`,
>     `rocky-10`, `ubuntu-22.04`, `ubuntu-24.04`, `ubuntu-26.04`.

## Using the `mgbuild.sh` script

The `mgbuild.sh` script is a convenience script that builds Memgraph in a Docker container. Prior to
packaging Memgraph, [you need to build it first](https://memgraph.com/docs/getting-started/build-memgraph-from-source#build-with-docker).

Using the same export environment variables as in the 
[build-memgraph-from-source](https://memgraph.com/docs/getting-started/build-memgraph-from-source#build-with-docker) guide, run the 
`mgbuild.sh` script by running the following command to build the package:

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

Then it can be copied out of the container:

```bash
./release/package/mgbuild.sh \
    --toolchain $TOOLCHAIN \
    --os "$OS" \
    --arch $ARCH \
    copy \
    --package \
    --dest-dir output
```

Stop the container:

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

Now, the package can be found in the `output` directory and it can be installed using `apt` or 
`dnf`, depending on the distribution. For example, for Ubuntu 24.04:

```bash
sudo apt install ./output/memgraph_<version>_<arch>.deb
```

> **Note**
>
> For Ubuntu 24.04, the package can also be used to create a Docker image. To do this, we must first
>     build a custom OpenSSL package:
>
> ```bash
./release/package/mgbuild.sh \
    --toolchain $TOOLCHAIN \
    --os "$OS" \
    --arch $ARCH \
    build-ssl \
    --version "3.5.4"
```
>
>     The following command will then create an Ubuntu-based Docker image:
>
> ```bash
./release/package/mgbuild.sh \
    --toolchain $TOOLCHAIN \
    --os "$OS" \
    --arch $ARCH \
    --build-type $BUILD_TYPE \
    package-docker \
    --src-dir output \
    --dest-dir docker-output
```
>
>     resulting in a Docker image tarball in the `docker-output` directory. This can then be loaded
>     into Docker using the `docker load` command.
>
> ```bash
docker load -i docker-output/memgraph-<version>-docker.tar.gz
```
>
>     The image can then be run using the `docker run` command.
>
> ```bash
docker run -p 7687:7687 -p 7444:7444 --name memgraph memgraph/memgraph:<version>
```

## Using the `package.sh` script

After following the build instructions using
[`build.sh`](https://memgraph.com/docs/getting-started/build-memgraph-from-source#build-with-buildsh), or
[`conan` and `cmake`](https://memgraph.com/docs/getting-started/build-memgraph-from-source#build-with-conan-and-cmake), the
`package.sh` script in the repository root packages the build directory directly on the host.
It takes a target and a package format:

```bash
source /opt/toolchain-v8/activate
./package.sh memgraph deb
```

- `TARGET` is one of `memgraph` (the `memgraph` + `memgraph-debuginfo` packages), `mage`
  (the `memgraph-mage` package, plus `memgraph-mage-debuginfo` when the build used
  `--split-debug`), or `all` (everything the build was configured for).
- `FORMAT` is `deb` or `rpm`.

For example, to package the MAGE query modules as an RPM from a
`./build.sh --mage only` build:

```bash
./package.sh mage rpm
```

The script checks that the build directory is actually configured for the requested target
(e.g. packaging `mage` requires a build with `--mage on` or `--mage only`), selects the right
CPack components, and verifies the expected number of packages was produced. The packages are
written to `build/output/`. Use `--build-dir DIR` to package from a non-default build
directory.

## Using CPack

`package.sh` is a thin wrapper around CPack — if you need full control, you can also invoke
CPack manually.

Prior to packaging, create the output directory:

```bash
mkdir -p build/output && cd build/output
```

Activate the toolchain:

```bash
source /opt/toolchain-v8/activate
```

Then run the following commands to build the package, depending upon the distibution:

- For RPM-based distributions:

```bash
cpack -G RPM --config ../CPackConfig.cmake
```

- For DEB-based distributions:

```bash
cpack -G DEB --config ../CPackConfig.cmake
```

Once built, the packages can be installed using `apt` or `dnf`.
