182

I seem to have diverging versions of rustc and cargo (I think),

$ rustc -V
rustc 1.9.0 (e4e8b6668 2016-05-18)
$ cargo -V
cargo 0.10.0-nightly (10ddd7d 2016-04-08)

Is there a command akin to

pip install --upgrade pip 

for upgrading cargo? I.e. something like

cargo install --upgrade cargo
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Filip Allberg
  • 3,941
  • 3
  • 20
  • 37

5 Answers5

219

You should update rustc and cargo based on how you installed it. If you used rustup, a rustup update should suffice. If you used a package manager or a binary installer, check those sources for an update.

rustc and cargo are shipped together, but that doesn't mean that their versions need to match. In fact, they do not match until Rust 1.26.0, when the Cargo binary was changed to print the Rust version.

I have the same versions of rustc and cargo that you do; those are the ones that correspond to the Rust 1.9 release. There's nothing to worry about.


If you really want to, you can download a nightly version of Cargo or compile your own. As long as your version exists in your PATH before the older one, it will be used.

I used to do this with my local Rust builds in order to have a version of Cargo at all, although rustup now automatically uses the cargo from the most recent stable version when there isn't one available in the current toolchain, which is nice.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    What if I inherited the system and have no idea how rust was installed but need to 'rustup update' it? I can run rustup update all day but it doesn't update the version of ~/.cargo/bin/rustc to the version rustup says was just installed. – cloudsurfin Oct 07 '22 at 19:54
  • Nevermind, I was in the folder of a project that happened to have a rust-toolchain file contained in it, overriding the default toolchain. – cloudsurfin Oct 07 '22 at 19:57
209

tl;dr command rustup update will update both Rust and Cargo:

$ rustc --version
rustc 1.27.2 (58cc626de 2018-07-18)
$ cargo --version
cargo 1.27.0 (1e95190e5 2018-05-27)

$ rustup update stable
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2018-08-02, rust version 1.28.0 (9634041f0 2018-07-30)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'

$ rustc --version
rustc 1.28.0 (9634041f0 2018-07-30)
$ cargo --version
cargo 1.28.0 (96a2c7d16 2018-07-13)
JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63
Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
24

You also need to change the default:

> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)

> rustup update stable

> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)

> rustup default stable-x86_64-apple-darwin

> rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Pahlevi Fikri Auliya
  • 4,157
  • 8
  • 35
  • 71
9

Use cargo to update itself:

cargo install cargo --force

This recompiles the package and installs the latest version.

I decided to post this after seeing that rustup didn't update cargo to 1.57

cigien
  • 57,834
  • 11
  • 73
  • 112
Romek Cieciak
  • 115
  • 1
  • 2
  • 1
    While this might work sometimes, in general this is not supposed to work, and you are not supposed to be able to install a different version of Cargo via Cargo itself: https://github.com/rust-lang/cargo/issues/11266#issuecomment-1286032635 – interfect Oct 20 '22 at 19:37
-1

You can edit the version of cargo and rust you're using by using the rustup cli. You can give it a specific version or specify a channel like nightly or beta.

For example:

rustup override set nightly
Troy B.
  • 19
  • 3