🎓 A1Learner
🔍

Rust Installing Binaries

cargo install is a store counter for tools written in Rust. Cargo builds the named tool on this machine, and the finished program lands in one known folder.

Install a tool

cargo install downloads a crate from crates.io, builds it, and installs its program on your machine. Only a package with a binary target can be installed. It needs a src/main.rs file. Or it needs another file named as a binary. A library-only crate has nothing runnable. It has nothing to install.

cargo install ripgrep

The program name can differ

The package is called ripgrep, but the program you run is called rg. Always check the crate README for the real program name:

rg --help

Where installed programs live

Installed programs all land in one folder: $HOME/.cargo/bin, the bin folder of your Cargo home. That folder must be in your $PATH. Or your terminal will not find the tools by name. Installing Rust with rustup normally sets this up. It sets it up for you.

ls ~/.cargo/bin

Extend Cargo itself

Cargo can be extended the same way: any program in your $PATH named cargo-something runs as cargo something. This command shows the custom commands Cargo has found, alongside built-ins like check:

cargo --list

Example

fn main() {
    println!("cargo install ripgrep");
    println!("installs the program rg into ~/.cargo/bin");
    println!("run it with: rg --help");
}

Key points

  • cargo install builds one named tool and places its program ready to run by name.

In this example, the three lines trace one install from start to finish. The first line is the command you type: cargo install ripgrep. Cargo downloads the crate and builds it, and — second line — places the finished program, called rg, into ~/.cargo/bin. The third line runs it: rg --help. The package is called ripgrep. But the program you run is rg. So always check the crate README. It gives the real program name.

Note

cargo install is a convenience for Rust developers sharing tools with each other.

It is not a replacement for your system package manager and its regular apps.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why can a library-only crate not be installed with cargo install?