温馨提示×

ubuntu rust配置步骤是什么

小樊
39
2026-10-28 06:54:47
栏目: 编程语言

下面给你一份在 Ubuntu 上配置 Rust 开发环境的完整步骤,从零开始,适合新手。


一、更新系统(推荐)

sudo apt update
sudo apt upgrade -y

二、安装必要依赖

sudo apt install -y build-essential curl git
  • build-essential:提供 gcc、make 等编译工具
  • curl:用于下载 Rust 安装脚本
  • git:版本管理(常用)

三、安装 Rust(官方推荐方式)

1. 使用 rustup 安装

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

安装过程中:

  • 选择 1) Proceed with installation (default)
  • 等待安装完成

2. 加载环境变量

source "$HOME/.cargo/env"

(以后每次新开终端会自动加载)


四、验证安装

rustc --version
cargo --version

看到类似输出说明成功:

rustc 1.78.0 (xxxxxx)
cargo 1.78.0 (xxxxxx)

五、配置镜像(国内加速,可选但推荐)

编辑环境变量文件

nano ~/.cargo/config.toml

写入(以清华镜像为例):

[source.crates-io]
replace-with = 'tuna'

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"

保存退出。


六、创建第一个 Rust 项目

cargo new hello_rust
cd hello_rust
cargo run

输出:

Hello, world!

七、常用命令速查

命令 作用
cargo new 项目名 新建项目
cargo build 编译
cargo run 编译并运行
cargo check 只检查不编译
rustup update 升级 Rust

八、VS Code 配置(推荐)

sudo snap install code --classic

安装插件:

  • Rust Analyzer
  • Code LLDB(调试用)

如果你需要:

  • Ubuntu 服务器无界面配置
  • 交叉编译(如 ARM)
  • Rust + WebAssembly
  • Rust + Docker

告诉我你的使用场景,我可以给你更针对性的配置方案。

0 踩