@@ -7,6 +7,8 @@ will complain that you already have Rust installed in `/usr` and refuse to
77install. However, you can install Rust via ` rustup ` and have it coexist with
88your packaged Rust toolchain.
99
10+ ## Set up rustup with an existing Rust toolchain
11+
1012When you initially install Rust with ` rustup ` , pass the ` -y ` option to make it
1113ignore the packaged Rust toolchain and install a ` rustup ` -managed toolchain
1214into ` ~/.cargo/bin ` . Add that directory to your ` $PATH ` (or let ` rustup ` do it
@@ -28,5 +30,63 @@ you may want to make it your [default toolchain]:
2830rustup default system
2931```
3032
33+ ## Ensure the correct ` $PATH ` configuration
34+
35+ There are times when the above steps don't work, and you may see strange error
36+ messages when running commands that should have been proxied by rustup.
37+ For example, when running ` cargo +stable --version ` , you may encounter the
38+ following error:
39+
40+ ``` text
41+ error: no such command: `+stable`
42+
43+ Cargo does not handle `+toolchain` directives.
44+ Did you mean to invoke `cargo` through `rustup` instead?
45+ ```
46+
47+ This means ` cargo ` is currently not a ` rustup ` proxy, and your ` $PATH ` needs
48+ to be fixed.
49+
50+ In fact, on any machine with rustup installed, you would like to have ** rustup
51+ proxies showing up first in ` $PATH ` ** , shadowing any other Rust installations.
52+ Don't worry: these shadowed installations can then be adopted by rustup with the
53+ ` rustup toolchain link ` command as mentioned above.
54+
55+ The exact steps to be taken to make rustup proxies come first may vary according
56+ to your system environment, but usually it is about changing the evaluation
57+ order of certain lines in your shell configuration file(s).
58+
59+ To make it clearer, let's look at the example of a Mac with both regular rustup
60+ fetched from [ rustup.rs] and homebrew-installed ` rust ` .
61+ The ** right way** to configure ` .profile ` in this environment would be:
62+
63+ ``` bash
64+ eval $( /opt/homebrew/bin/brew shellenv)
65+ . $HOME /.cargo/env
66+ ```
67+
68+ In this example, both of these lines all _ prepend_ to ` $PATH ` , so the last one
69+ takes over, letting the rustup proxies shadow the homebrew-installed ` rust ` .
70+ On the other hand, putting these lines the other way around will cause the
71+ aforementioned error.
72+
73+ When in doubt, you can always debug your shell configuration by printing the
74+ status of your current ` $PATH ` with ` echo $PATH | xargs -n1 ` and paying
75+ attention to the order of ` $CARGO_HOME/bin ` (which defaults to
76+ ` $HOME/.cargo/bin ` ) compared to your package manager's ` bin ` directory.
77+
78+ After the fix, the output of ` cargo +stable --version ` should be similar to one
79+ of the following, depending on whether you have had the ` stable ` toolchain
80+ installed:
81+
82+ - ``` text
83+ cargo 1.85.1 (d73d2caf9 2024-12-31)
84+ ```
85+
86+ - ``` text
87+ error: toolchain 'stable' is not installed
88+ ```
89+
90+ [ rustup.rs ] : https://rustup.rs
3191[ toolchain override shorthand ] : ../overrides.md#toolchain-override-shorthand
3292[ default toolchain ] : ../overrides.md#default-toolchain
0 commit comments