Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 622
Ben Gubler Avatar asked May 29 '18 14:05

Ben Gubler


2 Answers

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.

like image 134
Damian Rivas Avatar answered Sep 30 '22 06:09

Damian Rivas


Notice that curl https://sh.rustup.rs -sSf | sh will add to /home/<user>/.profile a line that appends binaries in ~/.cargo/bin, including rustup and rustc. The addition of the binaries in .cargo however does not take immediate effect. An easy way to associate it with your bash terminal is (immediately after installation):

echo $(cat ~/.profile | tail -1) >> ~/.bashrc
source ~/.bashrc

In your particular case, since you are root, you should copy /root/home/.cargo and /root/home/.profile into your own users home directory.

like image 45
Gabriel Fernandez Avatar answered Sep 30 '22 06:09

Gabriel Fernandez