cargo: no such subcommand: +nightly

I was trying to run the following command:

$ cargo +nightly install [package]
error: no such subcommand: `+nightly`

But … that didn’t work. This was my fix:

Remove Rust/Cargo from default package manager#

I first removed the cargo package I had installed via apt from my system:

$ apt remove rustc rust-gdb libstd-rust-dev libstd-rust-1.43

This was on a rather old Ubuntu 18.04 LTS, so it had an outdated version of Rust/Cargo. To see which Rust packages you have installed, try this:

$ dpkg -l | grep -i rust

And see what packages come up with the name “rust” in them.

Install latest cargo release#

From the official docs :

$ curl https://sh.rustup.rs -sSf | sh

Almost there!

Install package dependencies#

But it still didn’t work straight away:

$ cargo +nightly install [package]
error: toolchain 'nightly-x86_64-unknown-linux-gnu' is not installed

Almost there! First, install the nightly toolchain using rustup (which is now installed, thanks to the installer we ran above):

$ rustup toolchain install nightly
[...]
nightly-x86_64-unknown-linux-gnu installed - rustc 1.50.0-nightly (a0d664bae 2020-11-22)

Now, the +nightly package install works:

$ cargo +nightly install [package]
[...]
   Compiling rustc-hex v2.1.0
   Compiling fake_instant v0.4.0
   Compiling ansi_term v0.11.0
   Compiling gimli v0.23.0
   Compiling urlencoding v1.1.1
   ...
    Building [=============>       ] 107/411: ipnet, humantime, parity-wasm, object

(Note: I had to install pkg-config first before the cargo install could compile the package.)