温馨提示×

debian rust路径怎么设置

小樊
39
2026-09-02 04:53:48
栏目: 编程语言

在 Debian 上设置 Rust 的路径,通常取决于你是怎么安装 Rust 的。下面分情况说明:


一、使用 rustup 安装(推荐方式)

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

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

1. 默认路径

Rust 会安装在:

~/.cargo/bin

2. 设置 PATH(关键)

安装完成后,把下面这行加到你的 shell 配置文件中:

Bash 用户

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

Zsh 用户

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

验证:

which cargo
cargo --version

二、使用 Debian 官方 apt 安装

sudo apt update
sudo apt install rustc cargo

特点

  • 路径已经在系统 PATH 中(通常在 /usr/bin
  • 不需要手动设置 PATH

验证:

which cargo
cargo --version

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


三、手动指定 Rust 工具链路径(进阶)

如果你有自定义安装目录,比如 /opt/rust

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

可写入 ~/.bashrc/etc/profile.d/rust.sh(系统级)。


四、常见问题

1. cargo: command not found

说明 PATH 没生效,检查:

echo $PATH
ls ~/.cargo/bin

2. 多用户环境

系统级设置:

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

内容:

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

如果你能告诉我:

  • 是用 rustup / apt / 手动编译
  • 单用户还是服务器多用户

我可以给你更精确的配置方式。

0