# ubuntu安装rust

# 方法1)使用 rustup官方推荐

第一步:脚本安装

下载并运行 rustup 安装脚本。

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

输出信息:

(base) gun@gun:~/桌面$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/gun/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/gun/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/gun/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/gun/.profile
  /home/gun/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
872.8 KiB / 872.8 KiB (100 %) 271.7 KiB/s in  3s         
info: latest update on 2025-04-03, rust version 1.86.0 (05f9846f8 2025-03-31)
info: downloading component 'cargo'
  8.8 MiB /   8.8 MiB (100 %) 246.4 KiB/s in 35s         
info: downloading component 'clippy'
  2.8 MiB /   2.8 MiB (100 %)  25.6 KiB/s in  4m  2s             
info: downloading component 'rust-docs'
 21.2 MiB /  21.2 MiB (100 %)  95.0 KiB/s in  5m 25s             
info: downloading component 'rust-std'
 27.1 MiB /  27.1 MiB (100 %)  74.6 KiB/s in  8m 28s              
info: downloading component 'rustc'
 72.8 MiB /  72.8 MiB (100 %) 105.6 KiB/s in 18m 51s             
info: downloading component 'rustfmt'
110.8 KiB /   2.4 MiB (  4 %)  20.6 KiB/s in 44s ETA:  1m 54s
info: retrying download for 'https://static.rust-lang.org/dist/2025-04-03/rustfmt-1.86.0-x86_64-unknown-linux-gnu.tar.xz'
  2.4 MiB /   2.4 MiB (100 %) 320.9 KiB/s in 24s             
info: installing component 'cargo'
  8.8 MiB /   8.8 MiB (100 %)   3.6 MiB/s in  2s         
info: installing component 'clippy'
info: installing component 'rust-docs'
 21.2 MiB /  21.2 MiB (100 %)   2.2 MiB/s in 18s             
info: installing component 'rust-std'
 27.1 MiB /  27.1 MiB (100 %)   4.9 MiB/s in  6s         
info: installing component 'rustc'
 72.8 MiB /  72.8 MiB (100 %)   5.3 MiB/s in 14s         
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.86.0 (05f9846f8 2025-03-31)


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, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
source "$HOME/.cargo/env.nu"    # For nushell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

通常可以通过运行以下命令之一完成(注意开头的点号):

. "$HOME/.cargo/env"  
1
  1. 开头的点号 "." 是 POSIX shell 的 source 命令简写形式

第二步:验证是否安装成功

需要关注的点

Rust 现已安装完成,太棒了!

要开始使用,您可能需要重启当前的 shell 会话。 这将重新加载 PATH 环境变量,使其包含 Cargo 的二进制目录($HOME/.cargo/bin)。

要配置当前的 shell 环境,您需要加载 位于 $HOME/.cargo 目录下对应的环境文件。

通常可以通过运行以下命令之一来实现(请注意开头的点号): . "HOME/.cargo/env.fish" # 适用于 fish source "$HOME/.cargo/env.nu" # 适用于 nushell

(技术说明:

  1. 开头的点号"."是 POSIX shell 中 source 命令的简写形式
  2. 不同的 shell 使用不同的语法:
    • 传统 shell(Bash/Zsh)使用点号或 source 命令
    • 现代 shell(Fish/Nushell)有各自的语法
  3. 此消息表示 Rust 工具链已安装完成,但需要手动重新加载或重启 shell 来更新环境变量)

特别说明:对于 fish shell 用户,请确保使用正确的命令: source "$HOME/.cargo/env.fish" # 这是专门为 fish shell 准备的配置命令

rustc --version
1
(base) gun@gun:/home$ rustc --version
rustc 1.86.0 (05f9846f8 2025-03-31)
1
2
cargo --version
1
(base) gun@gun:/home$ cargo --version
cargo 1.86.0 (adf9b6ad1 2025-02-28)
1
2

# 依赖问题1

Ubuntu 缺少 C 语言编译工具链(Rust 需要它来编译某些底层代码)

安装基础编译工具链

包含 gcc, g++, make 等

sudo apt install build-essential
1

安装 Rust 的 Linux 依赖

可能不需要安装这些依赖

pkg-config、libssl-dev可能不需要这些依赖。

跨平台一致性

  • Windows 的 Rust 安装包通常自带 MSVC 工具链
  • Linux 需要显式安装开发依赖
sudo apt install pkg-config libssl-dev
1

libssl-dev可能在国内无法安装。

sudo apt install pkg-config
1

验证安装

应显示版本号

gcc --version
1

输出的gcc版本日志:

(base) gun@gun:~/桌面/ubuntu_rust_code/rust_code_test01$ gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1
2
3
4
5

应显示路径如 /usr/bin/cc

which cc  
1

执行命令后控制台输出的日志信息:

(base) gun@gun:~/桌面/ubuntu_rust_code/rust_code_test01$ which cc  
/usr/bin/cc
1
2

# pkg-config依赖的作用

sudo apt install pkg-config
1

执行上面的安装日志信息:

(base) gun@gun:~/桌面/ubuntu_rust_code/rust_code_test01$ sudo apt install pkg-config
[sudo] gun 的密码: 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
将会同时安装下列软件:
  libpkgconf3 pkgconf pkgconf-bin
下列【新】软件包将被安装:
  libpkgconf3 pkg-config pkgconf pkgconf-bin
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 76 个软件包未被升级。
需要下载 75.4 kB 的归档。
解压缩后会消耗 283 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 libpkgconf3 amd64 1.8.1-2build1 [30.7 kB]
获取:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 pkgconf-bin amd64 1.8.1-2build1 [20.7 kB]
获取:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 pkgconf amd64 1.8.1-2build1 [16.8 kB]
获取:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu noble/main amd64 pkg-config amd64 1.8.1-2build1 [7,264 B]
已下载 75.4 kB,耗时 10(7,356 B/s)                                          
正在选中未选择的软件包 libpkgconf3:amd64。
(正在读取数据库 ... 系统当前共安装有 193427 个文件和目录。)
准备解压 .../libpkgconf3_1.8.1-2build1_amd64.deb  ...
正在解压 libpkgconf3:amd64 (1.8.1-2build1) ...
正在选中未选择的软件包 pkgconf-bin。
准备解压 .../pkgconf-bin_1.8.1-2build1_amd64.deb  ...
正在解压 pkgconf-bin (1.8.1-2build1) ...
正在选中未选择的软件包 pkgconf:amd64。
准备解压 .../pkgconf_1.8.1-2build1_amd64.deb  ...
正在解压 pkgconf:amd64 (1.8.1-2build1) ...
正在选中未选择的软件包 pkg-config:amd64。
准备解压 .../pkg-config_1.8.1-2build1_amd64.deb  ...
正在解压 pkg-config:amd64 (1.8.1-2build1) ...
正在设置 libpkgconf3:amd64 (1.8.1-2build1) ...
正在设置 pkgconf-bin (1.8.1-2build1) ...
正在设置 pkgconf:amd64 (1.8.1-2build1) ...
正在设置 pkg-config:amd64 (1.8.1-2build1) ...
正在处理用于 man-db (2.12.0-4build2) 的触发器 ...
正在处理用于 libc-bin (2.39-0ubuntu8.4) 的触发器 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Last Updated: 4/25/2025, 8:53:41 AM