Install Rust toolchain to a project specific location

Hi, I'm having issues understanding the exact behavior of having a customized Rust toolchain installation. I want to install cargo and related Rust toolchains to a specified location inside my project for our other tooling to use.
According to the document I found here (Installation - The rustup book), I can set RUSTUP_HOME and CARGO_HOME to the destination I want before I call rustup. But I still have some questions that I can't get answers to from the related docs:

  • According to the same doc, I need to ensure RUSTUP_HOME and CARGO_HOME are defined in the env vars and CARGO_HOME/bin is in the PATH. Is that enough to use the toolchain I installed? Or is there any further actions I need to do to avoid interleaving between the user-level installation under $HOME/.cargo and $HOME/.rustup?
  • What's the role of CARGO_HOME/env? Initially I though when sourcing this file, it's like Python's virtual env and it does all env settings automatically. But by looking at its content, it seems to only add CARGO_HOME/bin to the PATH.

In general, are there any references/resources I can look into for interacting with Rust toolchains with both the default installation (under $HOME) and customized installation?

Hi,
If you have a custom installation, RUSTUP_HOME and CARGO_HOME is enough. However, you should take care to the following points:

  • when you switch from a project to an other, be sure to correctly set RUSTUP_HOME and CARGO_HOME. These env variables are not only used for the installation, they are needed when compiling projects.
  • be sure to set the correct value for PATH. The important point here is the PATH resolution order. When looking for the cargo binary, it will take the first found when iterating from the first path to the last one. It means that if you do PATH="/path/to/.cargo/bin:$PATH" everything should work fine, even if you've previously used an other environment without cleaning PATH. However, take care to not do the opposite PATH="$PATH:/path/to/.cargo/bin". This won't work correctly because the first environment you've entered in will still be used.

To finish, .cargo/env is really just an helper script to set PATH properly. You should have a look at the Rustup book and the Cargo book I think you may find what you're looking for.

1 Like

Thank you!