Overriding Rust installation default paths `$HOME/.cargo` and `$HOME/.rustup`
Asked Answered
E

2

14

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.

Enunciation answered 3/9, 2019 at 5:16 Comment(0)
D
9

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.

Dasie answered 3/9, 2019 at 7:22 Comment(0)
C
13

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
Carpic answered 8/7, 2020 at 18:53 Comment(1)
This appears to be the same as the accepted answer, which also says you should set CARGO_HOME and CARGO_HOME. Please edit this answer to clarify what new information is provided.Prevent
D
9

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.

Dasie answered 3/9, 2019 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.