首先確保系統包列表是最新的,避免后續安裝依賴沖突:
sudo apt update && sudo apt upgrade -y
Rust編譯及工具鏈需要curl
、build-essential
(包含gcc、make等)等工具,安裝命令如下:
sudo apt install curl build-essential -y
rustup
是Rust官方推薦的工具鏈管理工具,支持安裝、更新和管理多個Rust版本。運行以下命令安裝:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path -y
注:
--no-modify-path
選項將Rust安裝到/opt/rust
目錄(避免修改系統PATH),若需全局使用,可省略此選項。
安裝完成后,需將Rust工具鏈路徑添加到環境變量中,實現全局可用:
echo 'export RUSTUP_HOME=/opt/rust' | sudo tee -a /etc/profile.d/rust.sh
echo 'export PATH=$PATH:/opt/rust/bin' | sudo tee -a /etc/profile.d/rust.sh
source /etc/profile # 立即生效
若未使用
--no-modify-path
,可跳過此步(rustup會自動配置)。
通過以下命令檢查Rust編譯器(rustc
)和包管理器(cargo
)是否安裝成功:
rustc --version # 應顯示Rust編譯器版本(如rustc 1.75.0)
cargo --version # 應顯示Cargo版本(如cargo 1.75.0)
中國大陸用戶可使用清華或中科大鏡像源加速crates.io依賴下載,編輯~/.cargo/config
文件(不存在則創建):
mkdir -p ~/.cargo
cat <<EOF > ~/.cargo/config
[source.crates-io]
replace-with = 'tuna' # 或 'ustc'(中科大鏡像)
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
EOF
使用Cargo快速創建項目:
cargo new hello_world # 創建名為hello_world的項目目錄
cd hello_world # 進入項目目錄
cargo run # 編譯并運行項目(輸出"Hello, world!")
rustfmt
(Rust官方格式化工具):rustup component add rustfmt
clippy
(Rust lint工具,用于捕獲潛在錯誤):rustup component add clippy
推薦使用VS Code或IntelliJ IDEA搭配Rust插件:
~/.cargo/bin
)。通過以上步驟,即可在Debian系統上完成Rust開發環境的配置,滿足日常開發需求。若需切換Rust版本,可使用rustup install <version>
安裝指定版本,再用rustup default <version>
設置為默認版本。