Skip to content

Building and deploying Rust binaries with cargo

Build release Rust binaries with LTO and stripped symbols, package them as Debian packages or tarballs, and supervise them with systemd.

A Rust binary in target/release/ is good enough to copy to a host and run, but production deserves more thought. Link-time optimization shaves another 10-20% off binary size and a few percent off runtime, stripped symbols cut another half, and a proper Debian package or systemd unit turns the binary into a real service. This article covers the release-build flags and the packaging/supervision that follow.

How to verify

Check what’s in Cargo.toml and what’s deployed today:

grep -A 5 '\[profile.release\]' /var/www/app/Cargo.toml
ls -la target/release/myapp 2>/dev/null
file target/release/myapp 2>/dev/null
systemctl status myapp --no-pager | head 2>/dev/null
ls /usr/local/bin/myapp 2>/dev/null

If file target/release/myapp reports a 80 MiB binary with debug info, LTO and strip are not enabled — a few Cargo.toml lines will cut that to 10-15 MiB.

What’s happening

cargo build --release compiles with optimizations. By default that means opt-level = 3, no debug assertions, and no incremental compilation. That is the baseline — fast code, big binary, slowish link time.

LTO (link-time optimization) makes the linker re-optimize across crate boundaries. lto = "fat" is the aggressive version (longer build time, smaller and faster binary); lto = "thin" is a parallel-friendly compromise. For a production release where build time is paid once in CI, fat wins. Combined with codegen-units = 1 (no parallel codegen within the crate), you give the optimizer maximum context to work with.

Stripping is removing the symbol table and DWARF debug info from the binary. The runtime does not need them; only debuggers and profilers do. strip = true in [profile.release] does this at build time without a separate strip invocation. Keep the unstripped binary somewhere (CI artifacts, debug-info package) so you can resolve crash dumps later.

The procedure

  1. Add a tuned release profile to Cargo.toml:

    [profile.release]
    opt-level = 3
    lto = "fat"
    codegen-units = 1
    strip = true
    panic = "abort"

    panic = "abort" removes the unwind machinery — smaller binary, faster execution. Trade-off: a panic in any thread terminates the process. For a server, that is usually what you want (systemd will restart).

  2. Build the release binary:

    cd /var/src/myapp
    cargo build --release --target x86_64-unknown-linux-gnu
    ls -la target/x86_64-unknown-linux-gnu/release/myapp
    file target/x86_64-unknown-linux-gnu/release/myapp
  3. Package as a .deb for proper apt-style installation. cargo-deb reads [package.metadata.deb] in Cargo.toml:

    cargo install cargo-deb
    [package.metadata.deb]
    maintainer = "Ops <ops@example.com>"
    depends = "$auto"
    section = "utility"
    priority = "optional"
    assets = [
      ["target/release/myapp", "usr/local/bin/", "755"],
      ["myapp.service", "lib/systemd/system/", "644"],
      ["config/myapp.toml", "etc/myapp/", "644"],
    ]
    maintainer-scripts = "debian/"
    cargo deb --target x86_64-unknown-linux-gnu
    ls -la target/x86_64-unknown-linux-gnu/debian/*.deb

    The output .deb installs the binary, the systemd unit, and the config to the right places.

  4. systemd unit at myapp.service:

    [Unit]
    Description=MyApp Rust service
    After=network.target
    
    [Service]
    User=myapp
    Group=myapp
    ExecStart=/usr/local/bin/myapp --config /etc/myapp/myapp.toml
    Restart=on-failure
    RestartSec=2s
    
    NoNewPrivileges=yes
    ProtectSystem=strict
    ProtectHome=yes
    PrivateTmp=yes
    RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
    MemoryDenyWriteExecute=yes
    ReadWritePaths=/var/lib/myapp /var/log/myapp
    
    LimitNOFILE=65535
    
    [Install]
    WantedBy=multi-user.target
  5. Install on the host:

    sudo apt install ./myapp_1.0.0_amd64.deb
    sudo systemctl daemon-reload
    sudo systemctl enable --now myapp
    journalctl -u myapp -f

    Upgrades are sudo apt install ./myapp_1.1.0_amd64.deb — apt handles the unit reload and service restart per the maintainer-scripts.

Operational notes

  • lto = "fat" build time on a medium project can be 5-10 minutes; on CI that is acceptable but on a dev laptop you want lto = "thin" in a dev-release profile. [profile.release-dev] inherits = "release"; lto = "thin" gives you both.
  • panic = "abort" saves binary size but means you cannot catch panics with catch_unwind. If the app does dynamic plugin loading or hosts third-party code, leave panic = "unwind" (the default).
  • A static musl binary (--target x86_64-unknown-linux-musl) is ~20% slower than a glibc-linked one in some workloads because musl’s allocator is conservative. For most network-bound services the difference is invisible; for memory-allocation-heavy code measure before committing to musl.
  • cargo-chef or sccache dramatically speed up CI builds by caching the dependency layer. On a lto = "fat" build, the dependency build dominates wall-clock time — a cache hit saves minutes per CI run.
  • For crash analysis, ship the unstripped binary to a debug-info package or store it as a CI artifact. Without symbols, a backtrace is hex addresses; with them, you get function names and line numbers.

For the toolchain side, see Installing the Rust toolchain. Stack Harbor’s managed operations practice ships Rust services as Debian packages with sandboxed systemd units — apt manages the lifecycle, journald captures the logs, and an upgrade is apt install and a unit restart.