【问题标题】:Make rustc, cargo, rustup, rustdoc commands work without sudo inside the Windows Subsystem for Linux使 rustc、cargo、rustup、rustdoc 命令在 Windows 子系统 for Linux 中无需 sudo 即可工作
【发布时间】:2018-11-08 05:42:16
【问题描述】:

我正在运行 WSL Debian 发行版,因为我想学习 InterMezzOS 教程。我按照 Rust 的安装说明运行了curl https://sh.rustup.rs -sSf | sh,但我只能通过在运行source $HOME/.cargo/env 之后运行source $HOME/.cargo/env 来使rustc 命令可用,并且当我退出sudo 时我不能调用它,并且当我重新输入sudo su 时,它不再起作用了,我必须再次输入。

有什么方法可以让它在我每次打开 shell 时都能正常工作,而不是每次都运行 sudo

【问题讨论】:

  • 在运行 sudo su 之后——为什么?如果不使用 sudo 会怎样?
  • @Shepmaster 它说“没有这样的文件或目录”。我试过做chown,它说的是同样的事情。我也必须安装 Rust 做sudo,除非我添加一个“-y”标志,否则它不会让我这样做,我无法弄清楚如何工作。这是问题的一部分吗?
  • 为什么要使用 sudo? rustc、cargo、rustup 和 rustdoc 都不需要 root。实际上在编译时使用 root 是很危险的。 (至少在 C 世界中,不确定 Rust)
  • @hellow 当我尝试使用 curl https://sh.rustup.rs -sSf | sh: error: $HOME differs from euid-obtained home directory: you may be using sudo error: if this is what you want, restart the installation with -y'` 安装时,它给了我这个错误。我将 bash 设置为自动在我的 Windows 文件夹中打开,这是问题的一部分吗?

标签: bash rust debian sudo windows-subsystem-for-linux


【解决方案1】:

在安装 Rust 之后,您可以通过运行 whereis rustc 找到它在 Linux 文件系统中的安装位置

它应该输出如下内容:

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

您需要将其添加到您的路径中,因此请在编辑器中打开 .bashrc

nano ~/.bashrc

(如果你像我一样使用 zsh,只需将其替换为 ~/.zshrc

向下滚动到文件底部(或使用键盘快捷键alt + /,并将Rust 的位置添加到PATH,方法是在末尾添加:

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

我有$HOME,因为/home/damianrivas/ 是我的$HOME 路径。我也可以放

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

完成此操作后,退出您打开的所有 WSL 终端,然后再打开一个新终端。你可以重新启动你的电脑来确定。然后在打开一个新终端时运行rustc --version,它应该可以工作了。

【讨论】:

  • 我以前从未使用过whereis,但它的手册页说(强调我的):whereis 实用程序检查 标准二进制目录指定的程序,打印出它找到的任何程序的路径。rustc没有安装到标准路径。在 macOS 10.13 上,whereis rustc 不打印任何内容,而 rustc --version 确实有效。更传统的方法是which rustc,这对我有用。
  • Rustup 已经应该将适当的目录添加到 PATH 变量中;我在.bash_profile 中有这样一个条目。如果您可以重现它在 WSL 中不这样做的行为,则应将其报告为错误。
  • @Shepmaster,我相信我的问题是我在 WSL 和 Git Bash 中的 $HOME 目录是相同的,所以我的 Linux Rust 覆盖了我的另一个?无论如何,我通过将 WSL 中的主目录设置为我创建的比常规 Git Bash 主目录高一级的文件夹来解决了这个问题。
  • 我看到了关于在他的道路上添加锈迹的讨论,所以我认为它对他来说是缺失的。 @Ben,我很高兴你能够让它工作!
  • 另外,man whereis 向我展示了这一点:whereis 定位指定命令名称的二进制文件、源文件和手册文件。 我没有看到任何提及“标准二进制文件”目录。”也许该命令在 Ubuntu 和 maxOS 中的行为不同?我确实查看了which 的手册页,它看起来确实是一个更好的工具。谢谢!
【解决方案2】:

请注意,curl https://sh.rustup.rs -sSf | sh 将在/home/<user>/.profile 中添加一行,在~/.cargo/bin 中附加二进制文件,包括 rustup 和 rustc。但是,在.cargo 中添加二进制文件不会立即生效。将它与您的 bash 终端关联的一种简单方法是(在安装后立即):

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

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

【讨论】:

    【解决方案3】:

    按照他们的文档,我这样做:

    $ 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>
    
    

    然后安装完成:

    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
    

    所以我做了以下

    • 您可能需要将 ~.bashrc 替换为 ~/.zshrc 或类似名称。
    echo "export PATH='\$HOME/.cargo/bin:\$PATH'" >> ~/.bashrc"
    source ~/.bashrc
    
    

    我检查是否安装了 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)
    
    

    这看起来不错,但现在让我们检查一下 sudo,以便系统脚本可以正常运行。

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

    天哪!这就是解决它的方法

    $ sudo apt-get install -y rustc
    
    $ sudo rustc --version
    rustc 1.47.0
    
    
    

    【讨论】:

      【解决方案4】:

      这对我有用:

      #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
      

      要验证当前的 Rust 版本,请使用 rustc --version 或 rustc -V 命令。

      【讨论】:

        猜你喜欢
        • 2022-07-15
        • 2021-08-30
        • 2019-04-20
        • 2020-11-19
        • 1970-01-01
        • 2017-03-20
        • 2017-05-02
        • 2021-05-27
        • 1970-01-01
        相关资源
        最近更新 更多