9

In Rust, by default, files are placed in $HOME/.cargo and $HOME/.rustup. Is there any way to override these defaults?

I am trying to debug an obscure issue and I want to try changing the file locations.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
Martin Ellison
  • 1,043
  • 1
  • 13
  • 25

2 Answers2

9

Before installing Rust, set the environment variables CARGO_HOME and RUSTUP_HOME, and make sure they're set when using the toolchain.

Add the below to your shell profile (~/.bashrc, ~/.zshrc, etc.):

  1. Environment variables for cargo and rustup

    export CARGO_HOME=/path/to/your/custom/location
    export RUSTUP_HOME=/path/to/your/custom/location
    
  2. Install Rust using rustup - this is the recommended method

    RUSTUP_HOME=$RUSTUP_HOME CARGO_HOME=$CARGO_HOME bash -c 'curl https://sh.rustup.rs -sSf | sh'
    
  3. Add Cargo's bin directory to $PATH:

    export PATH=$PATH:$CARGO_HOME/bin
    
  4. Source Cargo's environment

    source "$CARGO_HOME/env"
    
  5. Restart shell

Verify:

rustc --version
cargo --version
HAPPY
  • 169
  • 1
  • 10
  • 1
    This appears to be the same as the [accepted answer](https://stackoverflow.com/a/57766821/155423), which also says you should set `CARGO_HOME` and `CARGO_HOME`. Please [edit] this answer to clarify what new information is provided. – Shepmaster Jul 08 '20 at 19:03
8

This is explained in the documentation:

rustup allows you to customise your installation by setting the environment variables CARGO_HOME and RUSTUP_HOME before running the rustup-init executable. As mentioned in the Environment Variables section, RUSTUP_HOME sets the root rustup folder, which is used for storing installed toolchains and configuration options. CARGO_HOME contains cache files used by cargo.

Don't forget to update the $PATH or you won't be able to use the binaries. Also, if you want that setup to be permanent, export those variables from your shell configuration (e.g. .bashrc or .zshrc):

Note that you will need to ensure these environment variables are always set and that CARGO_HOME/bin is in the $PATH environment variable when using the toolchain.

TheDudeAbides
  • 1,821
  • 1
  • 21
  • 29
Boiethios
  • 38,438
  • 19
  • 134
  • 183