Is there a command to update Cargo to the latest official release? - rust

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

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.

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)

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)

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

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

Related

Rust can't find 'crate' for 'std'

I'm trying to get a .wasm file via RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown but get the error:
error[E0463]: can't find crate for `std`
When i type rustup target add wasm32-unknown-unknown i get:
info: component 'rust-std' for target 'wasm32-unknown-unknown' is up to date
I installed Rust and Rustup separate via chocolatey.
rustc 1.58.0
rustup 1.24.3
info: The currently active rustc version is rustc 1.60.0-nightly (5e57faa78 2022-01-19)
You need to add the wasm32-unknown-unknown target for the nightly toolchain if that's what you are using:
rustup target add --toolchain nightly wasm32-unknown-unknown
I suffered the same symptoms after initialized a new cargo project on macOS but my problem was not adding to the wrong toolchain.
In my case, the problem was multiple cargo binaries. I discovered this after running which cargo and seeing it installed via homebrew. I did rustup self uninstall (perhaps unnecessary), homebrew uninstall rust and, then reinstalled rust according to the official docs and wasm32-unknown-unknown and then the cargo build --release --target wasm32-unknown-unknown command worked as expected.

How can fix anchor test error after installing rust?

I installed rust on Ubuntu and checked that version but there's rustc version error.
myunixx#LAPTOP-JSMKVSS3:~$ rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `(error reading rustc version)`
I need 'rustc' to use anchor test. But I can't use it due to this error. I've reinstalled rust but there's no change. How can i fix this problem?
myunixx#LAPTOP-JSMKVSS3:~/myepicproject$ anchor test
Failed to obtain package metadata: `cargo metadata` exited with an error: error: process didn't exit successfully: `rustc -vV` (exit status: 127)
--- stderr
/home/myunixx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc: error while loading shared libraries: /home/myunixx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/../lib/libstd-7c582493123fc1dd.so: file too short
It looks like there was an issue installing rustc, even though you have rustup. A few things you can try:
maybe Rust can fix itself: run rustup update
explicitly add a toolchain: run rustup toolchain install stable
reinstall rustup: run rustup self uninstall and then go back through the instructions at https://www.rust-lang.org/tools/install
More information about rustup at its book:
https://rust-lang.github.io/rustup/installation/index.html

How can I prevent Cargo from automatically trying to download a newer version of the compiler?

I need to compile an older version of Parity which only compiles with version 1.28 of the Rust compiler. To install the older version, I did this:
rustup.sh -y --default-toolchain 1.28.0
This seems to work:
root#2afa3b8dc256:/build# cargo --version
cargo 1.28.0 (96a2c7d16 2018-07-13)
root#2afa3b8dc256:/build# rustc --version
rustc 1.28.0 (9634041f0 2018-07-30)
When I try to compile the project, it immediately tries to download a new version of the compiler:
root#2afa3b8dc256:/parity# cargo build --all
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
320.1 KiB / 320.1 KiB (100 %) 271.0 KiB/s ETA: 0 s
info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11-07)
info: downloading component 'rustc'
How can I prevent Cargo from doing that?
You can specify the used toolchain version for a specific directory by using rustup override. For example:
rustup override set 1.28.0

rust compile x86 library on x86_64 machine

I have ubuntu x86_64 container and cargo build goes well.
But i need to build x86 library version too.
As far as I understand i need to add i686 toolchain and target.
rustup target add i686-unknown-linux-gnu done successful
rustup toolchain install stable-i686-unknown-linux-gnu finished with error
$ rustup toolchain install stable-i686-unknown-linux-gnu
info: syncing channel updates for 'stable-i686-unknown-linux-gnu'
info: latest update on 2018-11-08, rust version 1.30.1 (1433507eb 2018-11-07)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
stable-i686-unknown-linux-gnu installed - (error reading rustc version)
and
$ rustup default stable-i686
info: using existing install for 'stable-i686-unknown-linux-gnu'
info: default toolchain set to 'stable-i686-unknown-linux-gnu'
stable-i686-unknown-linux-gnu unchanged - (error reading rustc version)
Do I missed something or took wrong approach?
Instead of changing your toolchain, you have to add the target to your current toolchain (make sure to switch back to your original toolchain first).
$ rustup target install i686-unknown-linux-gnu
$ cargo build --target=i686-unknown-linux-gnu
Of course, you need to install the 32-bit libraries on your system as well, e.g. on ubuntu you install them by
$ sudo apt install gcc-multilib
(for more information about that see How to Compile 32-bit Apps on 64-bit Ubuntu?)

Build a Rust project on Travis CI with a specific nightly version [duplicate]

I have downloaded the latest Rust nightly and added this dependency to Cargo.toml:
[dependencies.http]
git = "https://github.com/chris-morgan/rust-http.git"
I'm getting lots of errors for cargo build:
...
error: aborting due to 7 previous errors
...
error: aborting due to previous error
Could not compile `regex`.
I guess this is because some dependencies have not been updated to the latest rust version. Is it possible to download the nightly from yesterday or the day before?
Installed versions:
$ rustc --version
rustc 0.13.0-nightly (c89417130 2015-01-02 21:56:13 +0000)
$ cargo --version
cargo 0.0.1-pre-nightly (1a1868b 2014-12-31 21:39:41 +0000)
If you are using rustup (the currently preferred manner of installing Rust):
rustup install nightly-2016-06-03
If you want to use the standalone Rust installers, previous versions are kept. From this Reddit thread, which links to this Rust issue:
They are officially hosted.
wget https://static.rust-lang.org/dist/2014-12-08/rust-nightly-x86_64-apple-darwin.pkg
wget https://static.rust-lang.org/dist/2014-12-12/rust-nightly-x86_64-unknown-linux-gnu.tar.gz
If you are still using rustup.sh (not .rs), you should be able to use something like:
rustup.sh --channel=nightly --date=2016-06-03

Resources