What is the proper way to start a Rust program on FreeBSD?

I'm trying to get a Rust program to start automatically on FreeBSD using the rc.d system.

I have a script like this:


. /etc/rc.subr



load_rc_config $name
: ${rustapps_enable:=no}
: ${rustapps_msg="Nothing started."}


    /root/.cargo/bin/cargo run --manifest-path /usr/local/rustapps/hello_server/Cargo.toml &


run_rc_command "$1"

When I execute it, I get this error:

root@freebsd:/usr/local/etc/rc.d # error: no default toolchain configured

However, I have no problem when I execute the line (/root/.cargo/bin/cargo run --manifest-path /usr/local/rustapps/hello_server/Cargo.toml &) to start it.

Therefore, what is the proper way to get a Rust program started automatically on FreeBSD?

I actually find it a bit strange to use the cargo command to start a Rust program. Can it be compile into a full-standalone executable?

Anyway, for now, I just need my problem solved - to get it started automatically on FreeBSD. Please help. Thanks!

Building a stand-alone executable is definitely preferred. In hello_server, run cargo build —release, and then check the hello_server/target/release/ directory for the executable (maybe it will be hello_server in your case). Then you can reference the executable in your startup script.


Are you running as a non root user when you successfully use Cargo? Toolchain selections could be user specific.

Either that or check your shell initialization scripts. Like .bashrc, .profile, etc.

I don’t use BSD — Linux man myself — but my suspicions would be one of these things.

Edit: but yes the real solution is to compile the binary ahead of time if you can.

Yup, your suggestion worked! (I had been too engrossed in development mode and didn't know by simply using the "--release" flag, cargo will build a standalone executable. :slight_smile:

1 Like

As further info, the --release flag tells cargo to compile using the "release profile". You can customize this using directives in your crate's Cargo.toml file.


I like to make these changes from the defaults. Builds are slower, but the excutables are faster:

debug = true
lto = true
codegen-units = 1

I'm having another problem after resolving this. I've posted another topic: Release build is unable to read .env variables I got it started automatically from FreeBSD's rc.d system, but it is unable to read the .env file to access the database. The preferred solution is not to set any hard links/location within the project. How can I resolve it?