If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.
14.6.1 Install a Binary Crate from crates.io
You can use the cargo install command to install a binary crate from crates.io. This is not meant to replace system packages; it is intended as a convenient way for Rust developers to install tools shared by others.
Its limitation is that it can only install crates that have a binary target. A binary target is an executable program, produced by a crate that has src/main.rs or is otherwise configured as a binary crate.
Since there is the concept of a binary target, there is also the concept of a library target. A library target cannot be run by itself.
Usually, the README.md file contains a description of the crate and tells you whether the crate has a library target, a binary target, or both.
14.6.2 cargo install
Binary files installed by cargo install are placed in the bin folder under the home directory.
If you installed Rust with the default rustup configuration, the binary directory is $HOME/.cargo/bin.
To make programs installed by cargo install directly executable, you need to make sure that directory is in the $PATH environment variable.
For example, in Chapter 12 we mentioned the Rust implementation of the grep tool, called ripgrep, which is used to search files. To install ripgrep, we can run:
$ cargo install ripgrep
Updating crates.io index
Downloaded ripgrep v13.0.0
Downloaded 1 crate (243.3 KB) in 0.88s
Installing ripgrep v13.0.0
......
Compiling ripgrep v13.0.0
Finished release [optimized + debuginfo] target(s) in 3m 10s
Installing ~/.cargo/bin/rg
Installed package `ripgrep v13.0.0` (executable `rg`)
Some output has been omitted, but this is roughly what it looks like. The penultimate line shows that the program was installed at ~/.cargo/bin/rg.
In the terminal, use echo $PATH to check whether this directory is in the environment variable.
14.6.3 Extending Cargo with Custom Commands
Cargo is designed so that it can be extended with subcommands.
For example, if a binary in $PATH is named cargo-something, you can run it with cargo something, treating it as if it were a Cargo subcommand.
When you run cargo --list, custom commands like this are also listed. Being able to install extensions with cargo install and then run them like built-in Cargo tools is a very convenient benefit of Cargo’s design.