温馨提示×

debian中rust路径如何设

小樊
46
2026-09-26 09:45:20
栏目: 编程语言

在 Debian 中设置 Rust 的路径,通常取决于你是通过什么方式安装的 Rust。下面分情况说明。


一、通过 rustup 安装(推荐方式)

如果你是用官方方式安装的:

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

1. Rust 默认路径

  • 安装目录:~/.cargo
  • 可执行文件:~/.cargo/bin
  • 工具链目录:~/.rustup

2. 设置 PATH(最重要)

安装完成后,rustup 通常会自动在 ~/.bashrc 或 ~/.profile 中添加:

source "$HOME/.cargo/env"

如果没有,可以手动添加:

echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

验证:

which cargo
which rustc

二、通过 Debian apt 安装

sudo apt install rustc cargo

路径位置

  • rustc:/usr/bin/rustc
  • cargo:/usr/bin/cargo

✅ 不需要手动设置 PATH,因为 /usr/bin 已在 PATH 中。

查看版本:

rustc --version
cargo --version

⚠️ 注意:apt 中的 Rust 版本通常较旧,不推荐用于开发。


三、为系统所有用户设置(不常见)

如果你把 Rust 装在 /opt/rust 之类的地方:

sudo nano /etc/profile.d/rust.sh

写入:

export PATH="/opt/rust/bin:$PATH"

然后:

source /etc/profile

四、常见问题

1. cargo: command not found

说明 PATH 没设置好,检查:

echo $PATH
ls ~/.cargo/bin

2. 切换 shell(zsh / fish)

  • zsh:改 ~/.zshrc
  • fish:
    set -Ua fish_user_paths ~/.cargo/bin
    

总结(最常用)

✅ Debian + rustup 用户只需:

export PATH="$HOME/.cargo/bin:$PATH"

如果你愿意,可以告诉我:

  • 你是桌面还是服务器
  • 用 apt 还是 rustup
  • 遇到什么具体报错

我可以更精准地帮你配置。

0 踩