Skip to main content
The cargo module manages Rust packages using cargo, the Rust package manager and build tool.

Functions

packages

Install, remove, or update cargo packages.
from pyinfra.operations import cargo

cargo.packages(
    name="Install Rust tools",
    packages=["ripgrep", "fd-find"],
)
packages
str | list[str]
List of packages to ensure.
present
bool
default:true
Whether the packages should be present.
latest
bool
default:false
Whether to upgrade packages without a specified version.
Package versions can be pinned like cargo: <pkg>@<version>

Common Use Cases

Install Development Tools

from pyinfra.operations import cargo

cargo.packages(
    name="Install Rust development tools",
    packages=[
        "cargo-watch",
        "cargo-edit",
        "cargo-outdated",
    ],
)

Install Command-Line Tools

from pyinfra.operations import cargo

cargo.packages(
    name="Install CLI tools written in Rust",
    packages=[
        "ripgrep",      # Fast grep alternative
        "fd-find",      # Fast find alternative
        "bat",          # Cat with syntax highlighting
        "exa",          # Modern ls replacement
        "tokei",        # Code statistics
    ],
)

Install Specific Versions

from pyinfra.operations import cargo

cargo.packages(
    name="Install specific versions",
    packages=[
        "[email protected]",
        "[email protected]",
    ],
)

Update Tools

from pyinfra.operations import cargo

cargo.packages(
    name="Update Rust tools",
    packages=["ripgrep", "bat"],
    latest=True,
)

Remove Tools

from pyinfra.operations import cargo

cargo.packages(
    name="Remove unused tools",
    packages=["old-tool"],
    present=False,
)

Install from Git Repository

from pyinfra.operations import server

server.shell(
    name="Install from git repository",
    commands=[
        "cargo install --git https://github.com/user/repo",
    ],
)

Install with Features

from pyinfra.operations import server

server.shell(
    name="Install with specific features",
    commands=[
        "cargo install ripgrep --features 'pcre2'",
    ],
)

Cargo-Specific Tips

Cargo installs binaries to ~/.cargo/bin by default:
from pyinfra.operations import server

# Ensure ~/.cargo/bin is in PATH
server.shell(
    name="Add cargo bin to PATH",
    commands=[
        'echo "export PATH=$HOME/.cargo/bin:$PATH" >> ~/.bashrc',
    ],
)
Update cargo and rustc:
from pyinfra.operations import server

server.shell(
    name="Update Rust toolchain",
    commands=["rustup update"],
)
View installed cargo packages:
from pyinfra.operations import server

server.shell(
    name="List cargo packages",
    commands=["cargo install --list"],
)
Clean cargo’s build cache:
from pyinfra.operations import server

server.shell(
    name="Clean cargo cache",
    commands=["cargo cache --autoclean"],
)
Install with exact dependency versions:
from pyinfra.operations import server

server.shell(
    name="Install with locked dependencies",
    commands=["cargo install ripgrep --locked"],
)
Install for different targets:
from pyinfra.operations import server

# Add target
server.shell(
    name="Add ARM target",
    commands=["rustup target add aarch64-unknown-linux-gnu"],
)

# Install for target
server.shell(
    name="Install for ARM",
    commands=[
        "cargo install ripgrep --target aarch64-unknown-linux-gnu",
    ],
)

Best Practices

from pyinfra.operations import server, cargo

# Install Rust using rustup
server.shell(
    name="Install Rust",
    commands=[
        "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y",
    ],
)

# Then install cargo packages
cargo.packages(
    name="Install tools",
    packages=["ripgrep"],
)
Cargo is excellent for installing fast CLI tools:
from pyinfra.operations import cargo

# Modern Unix tools written in Rust
cargo.packages(
    name="Install modern CLI tools",
    packages=[
        "ripgrep",      # Better grep
        "fd-find",      # Better find
        "bat",          # Better cat
        "exa",          # Better ls
        "du-dust",      # Better du
        "sd",           # Better sed
        "hyperfine",    # Benchmarking
        "tokei",        # Code statistics
    ],
)
from pyinfra.operations import cargo

# Regular updates
cargo.packages(
    name="Update all Rust tools",
    packages=[
        "ripgrep",
        "bat",
        "fd-find",
    ],
    latest=True,
)
Here are some popular CLI tools you can install with cargo:

ripgrep

Fast recursive grep alternative
cargo.packages(packages=["ripgrep"])

fd-find

Fast and user-friendly find alternative
cargo.packages(packages=["fd-find"])

bat

Cat clone with syntax highlighting
cargo.packages(packages=["bat"])

exa

Modern replacement for ls
cargo.packages(packages=["exa"])

tokei

Code statistics tool
cargo.packages(packages=["tokei"])

hyperfine

Command-line benchmarking tool
cargo.packages(packages=["hyperfine"])

Important Notes

Installing cargo packages compiles from source, which can take significant time and requires a C compiler and build tools.
Cargo packages are installed to ~/.cargo/bin by default. Ensure this directory is in your PATH.
For faster installations on multiple machines, consider setting up a binary cache or using pre-compiled binaries where available.

Build docs developers (and LLMs) love