简体   繁体   English

使 rustc、cargo、rustup、rustdoc 命令在 Linux 的 Windows 子系统中无需 sudo 即可工作

[英]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.我正在运行 WSL Debian 发行版,因为我想学习 InterMezzOS 教程。 I followed the installation instructions for Rust and ran curl https://sh.rustup.rs -sSf | sh我按照 Rust 的安装说明运行curl https://sh.rustup.rs -sSf | sh 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. curl https://sh.rustup.rs -sSf | sh ,但我只能通过在运行sudo su之后运行source $HOME/.cargo/env来使rustc命令可用,当我退出sudo我无法调用它,当我重新输入sudo su它不再工作,我得再打一次。

Is there any way I can make it available so the commands work every time I open my shell, without running sudo every time?有什么方法可以使它可用,以便每次打开 shell 时命令都可以工作,而不必每次都运行sudo

Right after you install Rust, you can find where it was installed in the Linux filesystem by running whereis rustc在你安装 Rust 之后,你可以通过运行whereis rustc找到它在 Linux 文件系统中的安装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您需要将其添加到您的路径中,因此在编辑器中打开.bashrc

nano ~/.bashrc

(if you use zsh like I do just replace it with ~/.zshrc ) (如果您像我一样使用 zsh 只需将其替换为~/.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:向下滚动到文件底部(或使用键盘快捷键alt + / ,并通过在末尾添加以下内容将 Rust 的位置添加到PATH

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

I have $HOME because /home/damianrivas/ is my $HOME path.我有$HOME因为/home/damianrivas/是我的$HOME路径。 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.完成此操作后,退出所有打开的 WSL 终端,然后再打开一个新终端。 You can restart your PC to be sure.您可以重新启动您的 PC 以确保。 Then run rustc --version when you open up a new terminal and it should work.然后在打开新终端时运行rustc --version ,它应该可以工作。

Notice that curl https://sh.rustup.rs -sSf | sh注意curl https://sh.rustup.rs -sSf | sh 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. curl https://sh.rustup.rs -sSf | sh将增加/home/<user>/.profile该追加的二进制文件在一条线上~/.cargo/bin ,包括rustup和rustc。 The addition of the binaries in .cargo however does not take immediate effect.然而,在.cargo添加二进制文件不会立即生效。 An easy way to associate it with your bash terminal is (immediately after installation):将它与 bash 终端关联的一种简单方法是(安装后立即):

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.在您的特定情况下,由于您是 root,您应该将/root/home/.cargo/root/home/.profile复制到您自己的用户主目录中。

Following their documentation I do this:按照他们的文档,我这样做:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

...


Current installation options:


   default host triple: aarch64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation


< I enter 1>

Then the installation completes with this:然后安装完成:

Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env

So I do the following所以我做以下

  • You may have to replace ~.bashrc with ~/.zshrc or something similar.你可能需要用~/.zshrc或类似的东西替换~.bashrc
echo "export PATH='\$HOME/.cargo/bin:\$PATH'" >> ~/.bashrc"
source ~/.bashrc

And I check if rust is installed我检查是否安装了 Rust

$ whereis rustc
rustc: /usr/bin/rustc /usr/share/rustc /home/nvidia/.cargo/bin/rustc

$ which rustc
/home/nvidia/.cargo/bin/rustc

$ rustc --version
rustc 1.53.0 (53cb7b09b 2021-06-17)

This looks good, but now let's check out sudo so that system scripts can run without issues.这看起来不错,但现在让我们看看 sudo 以便系统脚本可以毫无问题地运行。


$ sudo whereis rustc
rustc:

$ sudo rustc --version
sudo: rustc: command not found

OMG!我的天啊! And this is how to fix it这是修复它的方法

$ sudo apt-get install -y rustc

$ sudo rustc --version
rustc 1.47.0


This worked for me:这对我有用:

#For Mac and Linux Users
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

#window users
Download rustup-init.exe from www.rustup.rs and run.

Note:#run this after
source $HOME/.cargo/env

To verify the current Rust version, use the rustc --version or rustc -V command.要验证当前的 Rust 版本,请使用 rustc --version 或 rustc -V 命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM