204

I used cargo install to globally install a package, such as rustfmt or racer.

How can I update the installed package without first deleting it (cargo uninstall) and then running cargo install again.

Is there an update command?

3
  • Nope. You can discuss it in this issue. Commented Dec 27, 2015 at 20:14
  • 6
    Rerunning the initial cargo install thing will cause it to be updated. Commented May 29, 2023 at 15:31
  • 1
    @BallpointBen yep, this is what the accepted answer indicates Commented May 31, 2023 at 20:01

6 Answers 6

267

There is no such command in vanilla cargo (well, there's cargo install but that's for dependencies), but since cargo supports third-party subcommands there is an answer: the cargo-update crate.

Install as usual with

cargo install cargo-update 

then use

cargo install-update -a 

to update all installed packages, for more usage information and examples see the cargo install-update manpage.

Disclaimer: am author

Sign up to request clarification or add additional context in comments.

5 Comments

needs cmake to be installed
@rofrol As is clearly outlaid in the Installation sexion of the Manpage
"well, there's cargo install but that's for dependencies" actually cargo install can now be used to install crates globally: Install a Rust binary. Default location is $HOME/.cargo/bin
This should be a standard feature.
@MichaelMantion for real, it's insane in this day and age of security holes abound, that somebody decided to add a way to install binaries on your PATH but didn't think to provide a way to update them.
121

As of Rust 1.41.0, you can use the following command to update crates to their latest version:

cargo install <crate> 

This came from pull request #6798 (Add install-upgrade) and was stabilized in #7560 (Stabilize install-upgrade).

How does it work?

Instead of failing when cargo install detects a package is already installed, it will upgrade if the versions don't match, or do nothing (exit 0) if it is considered "up-to-date".

Forcing an upgrade / re-installation

The following command will always uninstall, download and compile the latest version of the crate - even if there's no newer version available. Under normal circumstances the install-upgrade feature should be preferred as it does save time and bandwidth if there's no new version of the crate.

cargo install --force <crate> 

Documentation

Further information can be found in the GitHub issue rust-lang/cargo#6797 and in the official documentation chapter.

4 Comments

Will this also compile them with nightly?
This is now stabilized, and the default behavior. You no longer need to use the nightly channel or provide the -Z install-upgrade argument. github.com/rust-lang/cargo/pull/7560
If you wish to update all packages a simple shell command such as cargo install --list | grep '^[a-zA-Z0-9_\-]* v[0-9.]*:$' | cut -d ' ' -f1 | xargs cargo install would suffice
Automatic upgrade does not seem to be the default behavior in Rust 1.55.0; I had wasm-pack 0.10.0 installed, but when I tried to update it to 0.10.1 using cargo install wasm-pack I got an error: error: binary 'wasm-pack' already exists in destination and had to use --force.
38

Here is a one-liner to update all installed Cargo crates, except those installed from a local folder:

cargo install $(cargo install --list | egrep '^[a-z0-9_-]+ v[0-9.]+:$' | cut -f1 -d' ') 

Explanation:

  • List installed packages
  • Filter to lines which contain package names and versions, and exclude ones with filesystem paths
  • Cut those lines to only include the package name
  • cargo install with the resulting package names

5 Comments

Thank you for an answer without external dependencies.
cargo install $(cargo install --list | awk '/:$/ { print $1; }')
If you'll pardon the golf, cargo install --list | egrep -o '^[^ ]+' is a bit shorter for listing the installed packages. Are there ever non-indented lines that are not packages?
Note that this will give an unexpected error if there are no crates installed.
And you can take one of these one liners (thank you all!), chuck it into a script called e.g. cargo-upgrade-all in your PATH, and voila! you can run cargo upgrade-all including automatic autocomplete ;-). Thanks, cargo!
30

A solution I've found is to add the --force flag to the install command. For example cargo install --force clippy. This will effectively re-install the latest version.

⚠️ NOTE: Using the --force parameter is no longer necessary in newer versions of the cargo CLI.

Comments

1

Rust compiles each package from source when you update it, which can take some time. If you want prebuilt binaries (faster updates) to be used when possible, use cargo‑binstall

To set it up and update all your installed Rust binaries:

cargo install cargo-binstall cargo install cargo-update cargo-install-update install-update --all 

If cargo-binstall is present, cargo-update will automatically use it to install updates faster whenever prebuilt binaries are available.

As a side note, you can also install packages using cargo-binstall. It first looks up the package on crates.io and tries to find a ready-made version in the project’s repository. If that doesn’t work, it checks a third-party host called Quickinstall, and as a last resort, it just uses cargo install to build the package from source.

Warning: The Quickinstall host is a third-party service. Use it at your own discretion.

Comments

0

Without any unexpected error if there are no crates installed.

Use:

cargo install --list | grep -E '^[a-z0-9_-]+ v[0-9.]+:$' | cut -f1 -d' ' | xargs cargo install 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.