Log inSign up

Rust on a VPS: toolchain, build and service

Install a C linker first (build-essential or gcc), then run the official rustup script, which puts rustc and cargo in ~/.cargo/bin. Create a project with cargo new, compile it with cargo build --release, copy the binary to /usr/local/bin and give it a systemd unit. A reverse proxy such as Nginx adds HTTPS in front.

Topic: Go, Ruby, Java and other runtimes · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why use rustup instead of the distro rustc?

rustup installs the current stable compiler, updates it in one command and can add other toolchains or targets per project. Rust ships a new stable release every six weeks and many crates raise their minimum supported version quickly, so the older rustc packaged in Ubuntu, Debian or the RHEL family often cannot build current dependencies. The distribution compiler is still fine for small jobs such as enabling YJIT while compiling Ruby, but for your own projects rustup is the standard route.

rustup installs per user into your home directory and needs no root, except for the system build tools below.

What do you install before rustup?

Rust uses the system C compiler as its linker, and many crates compile small pieces of C or link against OpenSSL. Install a toolchain, pkg-config and the OpenSSL headers:

# Ubuntu and Debian
sudo apt update
sudo apt install -y build-essential pkg-config libssl-dev curl git

# Rocky Linux and AlmaLinux 9
sudo dnf install -y gcc gcc-c++ make pkgconf-pkg-config openssl-devel curl git

Compiling Rust is memory hungry, especially the final link of a release build with many dependencies. On a 1 GB plan add a swap file before your first build, or build on a larger machine and copy the binary.

How do you install Rust with rustup?

Run the official installer as your normal user, accept the default profile, then load the environment into the current shell. The installer adds the same line to your shell profile for future logins.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "$HOME/.cargo/env"
rustc --version
cargo --version
rustup show

The -y flag answers the prompts with the defaults: the stable toolchain for x86_64-unknown-linux-gnu with rustfmt and clippy. If you would rather review the script first, download it to a file, read it and then run it.

How do you create a small web service?

Start a binary project and add the axum web framework with the tokio runtime. cargo add writes the dependencies into Cargo.toml at their current versions.

cargo new hello-rs
cd hello-rs
cargo add axum
cargo add tokio --features full

Replace src/main.rs with a service that answers on two routes and binds to localhost:

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let addr = std::env::var("ADDR").unwrap_or_else(|_| "127.0.0.1:8080".to_string());
    let app = Router::new()
        .route("/", get(|| async { "hello from rust\n" }))
        .route("/healthz", get(|| async { "ok" }));
    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    println!("listening on {}", addr);
    axum::serve(listener, app).await.unwrap();
}
cargo run &
sleep 5
curl http://127.0.0.1:8080/
kill %1

The first build downloads and compiles every dependency, so it takes longer than later ones. If the curl call runs before the server is ready, wait a little and try again.

How do you build a lean release binary?

Release builds are optimized and much faster than debug builds. A few profile settings in Cargo.toml make the binary smaller as well:

[profile.release]
lto = true
codegen-units = 1
strip = true
cargo build --release
ls -lh target/release/hello-rs
ldd target/release/hello-rs

The binary links only against glibc and a few system libraries, so it runs on any server with the same or newer glibc. If the link step is killed for lack of memory, limit parallel jobs with cargo build --release -j 1, or drop lto = true, which uses the most memory.

How do you run it with systemd?

Copy the binary with install so it gets correct ownership and, on Rocky and Alma, the right SELinux label. Then create a hardened unit that runs it as a temporary system user.

sudo install -m 0755 target/release/hello-rs /usr/local/bin/hello-rs
sudo nano /etc/systemd/system/hello-rs.service
[Unit]
Description=hello-rs web service
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/hello-rs
Environment=ADDR=127.0.0.1:8080
DynamicUser=yes
Restart=on-failure
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now hello-rs
sudo ss -ltnp | grep 8080
curl http://127.0.0.1:8080/healthz

Logs printed by the program land in the journal: journalctl -u hello-rs -f. If the app needs to keep files, add StateDirectory=hello-rs to get a writable /var/lib/hello-rs.

How do you publish it over HTTPS?

Put Nginx in front on ports 80 and 443 and request a certificate with Certbot. The server block and Certbot commands are the same as for any local HTTP service; the Go service guide shows a complete example, and the Nginx reverse proxy guide explains each header. Open only the web ports and SSH:

# Ubuntu and Debian
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable

# Rocky Linux and AlmaLinux 9
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
sudo setsebool -P httpd_can_network_connect 1

Keep port 8080 closed: it is bound to 127.0.0.1 and only Nginx needs to reach it.

How do you keep the toolchain and dependencies current?

Update the compiler with rustup and dependencies with cargo, then rebuild and redeploy. Running binaries are not affected until you replace them.

rustup update
cargo update
cargo install cargo-audit
cargo audit
cargo build --release
sudo install -m 0755 target/release/hello-rs /usr/local/bin/hello-rs
sudo systemctl restart hello-rs

cargo audit checks Cargo.lock against the RustSec advisory database and lists crates with known vulnerabilities. Commit Cargo.lock for services, so every build uses the exact versions you tested. The target directory grows quickly; cargo clean frees the space when disk runs low.

What are the usual build errors?

  • linker cc not found: install build-essential or gcc.
  • Could not find openssl or pkg-config: install pkg-config and libssl-dev or openssl-devel.
  • rustc too old for a crate: run rustup update, or check that which cargo points into ~/.cargo/bin and not at a distro package.
  • signal 9 during build: out of memory; add swap or use -j 1.
  • status 203/EXEC from systemd: wrong path or SELinux label; reinstall with install or run restorecon.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4release builds without swap2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A1-1running a binary built elsewhere1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months

Questions about Install Rust and build a service

Can I install Rust for every user on the server?

rustup is designed per user. For a shared setup, set RUSTUP_HOME and CARGO_HOME to a path such as /opt/rust during install and add its bin folder to PATH in /etc/profile.d.

Do I need Rust installed to run the compiled service?

No. The release binary is all the server needs. You can build in CI or on a workstation with the same target and copy the file over.

How do I uninstall Rust?

Run rustup self uninstall. It removes the toolchains and the ~/.cargo and ~/.rustup directories.

Why is the first cargo build so slow?

Cargo compiles every dependency from source once. Later builds reuse the compiled artifacts in target and only rebuild what changed.

How much memory does a Rust web service use at runtime?

Small axum services often use only a few megabytes. Building them needs far more memory than running them.