Search code examples
bashrustdebiansudowindows-subsystem-for-linux

Make rustc, cargo, rustup, rustdoc commands work without sudo inside the Windows Subsystem for Linux


I'm running a WSL Debian distro because I wanted to follow along with the InterMezzOS tutorials. I followed the installation instructions for Rust and ran curl https://sh.rustup.rs -sSf | sh, but I can only make the rustc command available by running source $HOME/.cargo/env AFTER running sudo su, and when I exit sudo I can't call it, and when I retype sudo su it doesn't work anymore, I have to type it again.

Is there any way I can make it available so the commands work every time I open my shell, without running sudo every time?


Solution

  • Right after you install Rust, you can find where it was installed in the Linux filesystem by running whereis rustc

    It should output something like this:

    rustc: /home/damianrivas/.cargo/bin/rustc
    

    You need to add that to your path, so open up .bashrc in an editor

    nano ~/.bashrc
    

    (if you use zsh like I do just replace it with ~/.zshrc)

    Scroll down to the bottom of the file (or use the keyboard shortcut alt + /, and add Rust's location to PATH by adding this at the end:

    # Add Rust to $PATH
    export PATH="$HOME/.cargo/bin:$PATH"
    

    I have $HOME because /home/damianrivas/ is my $HOME path. I could've also put

    export PATH="/home/damianrivas/.cargo/bin:$PATH"
    

    After this is done, exit all of your open WSL terminals before opening up a new one. You can restart your PC to be sure. Then run rustc --version when you open up a new terminal and it should work.