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
-
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). -
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 -
Package as a
.debfor proper apt-style installation.cargo-debreads[package.metadata.deb]inCargo.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/*.debThe output
.debinstalls the binary, the systemd unit, and the config to the right places. -
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 -
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 -fUpgrades 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 wantlto = "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 withcatch_unwind. If the app does dynamic plugin loading or hosts third-party code, leavepanic = "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-cheforsccachedramatically speed up CI builds by caching the dependency layer. On alto = "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.