Installing Binaries with Cargo Install

Beginner

This tutorial is from open-source community. Access the source code

Introduction

Welcome to Installing Binaries With Cargo Install. This lab is a part of the Rust Book. You can practice your Rust skills in LabEx.

In this lab, you will learn how to use the cargo install command to install binary crates locally and make them runnable by adding the installation directory to your $PATH.

Installing Binaries with cargo install

The cargo install command allows you to install and use binary crates locally. This isn't intended to replace system packages; it's meant to be a convenient way for Rust developers to install tools that others have shared on https://crates.io. Note that you can only install packages that have binary targets. A binary target is the runnable program that is created if the crate has a src/main.rs file or another file specified as a binary, as opposed to a library target that isn't runnable on its own but is suitable for including within other programs. Usually, crates have information in the README file about whether a crate is a library, has a binary target, or both.

All binaries installed with cargo install are stored in the installation root's bin folder. If you installed Rust using rustup.rs and don't have any custom configurations, this directory will be _HOME/.cargo/bin_. Ensure that directory is in your `PATHto be able to run programs you’ve installed withcargo install`.

For example, in Chapter 12 we mentioned that there's a Rust implementation of the grep tool called ripgrep for searching files. To install ripgrep, we can run the following:

$ 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
   --snip--
   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`)

The second-to-last line of the output shows the location and the name of the installed binary, which in the case of ripgrep is rg. As long as the installation directory is in your $PATH, as mentioned previously, you can then run rg --help and start using a faster, Rustier tool for searching files!

Summary

Congratulations! You have completed the Installing Binaries With Cargo Install lab. You can practice more labs in LabEx to improve your skills.

Other Tutorials you may like