如何在 Ubuntu 20.04 上安装 Rust

前些天发现了一个人工智能学习网站,通俗易懂,风趣幽默,最重要的屌图甚多,忍不住分享一下给大家。点击跳转到网站。

如何在 Ubuntu 20.04 上安装 Rust

介绍

Rust 编程语言,是一种功能强大的通用编程语言。Rust 在语法上与C++类似,用于广泛的软件开发项目,包括浏览器组件、游戏引擎和操作系统。

在本文中,将在 Ubuntu 20.04 上安装最新版本的 Rust(1.66),然后创建、编译和运行测试程序。

**注意:**本文也适用于 Ubuntu 22.04,但是,可能会出现apt upgrade针对各种问题的交互式对话框。例如,系统可能会询问是否要在需要时自动重新启动服务,或者是否要替换已修改的配置文件。这些问题的答案取决于自身的软件和偏好,本文不会提及。

先决条件

需要一台 Ubuntu 20.04 服务器,并具有启用 sudo 的非 root 用户和防火墙。可以按照Ubuntu 20.04 初始服务器设置教程进行设置。

第 1 步 — 使用rustup在 Ubuntu 上安装 Rust

尽管在 Linux 上安装 Rust 有多种不同的方法,但推荐的方法是使用rustup命令行工具。

运行命令下载该rustup工具并安装 Rust 的最新稳定版本:

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

系统会提示选择安装类型:

Outputsammy@ubuntu:~$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | 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/sammy/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/sammy/.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/sammy/.cargo/bin

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

  /home/sammy/.profile
  /home/sammy/.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 installation (default)
2) Customize installation
3) Cancel installation
>

这里使用默认选项 1。但是,如果你熟悉rustup安装程序并想要自定义安装,则可以选择选项 2。输入选择并按Enter

选项 1 的输出为:

Outputinfo: 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'
info: latest update on 2023-01-10, rust version 1.66.1 (90743e729 2023-01-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 67.4 MiB /  67.4 MiB (100 %)  40.9 MiB/s in  1s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
  6.6 MiB /   6.6 MiB (100 %)   5.5 MiB/s in  1s ETA:  0s
info: installing component 'clippy'
info: installing component 'rust-docs'
 19.1 MiB /  19.1 MiB (100 %)   2.4 MiB/s in  7s ETA:  0s
info: installing component 'rust-std'
 30.0 MiB /  30.0 MiB (100 %)   5.6 MiB/s in  5s ETA:  0s
info: installing component 'rustc'
 67.4 MiB /  67.4 MiB (100 %)   5.9 MiB/s in 11s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.66.1 (90743e729 2023-01-10)


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, run:
source "$HOME/.cargo/env"
sammy@ubuntu:~$ 

接下来,运行以下命令将Rust工具链目录添加到PATH环境变量中:

source $HOME/.cargo/env

第 2 步 — 验证安装

通过请求版本来验证 Rust 安装:

rustc --version

rustc --version命令返回系统上安装的 Rust 编程语言的版本。例如:

Outputsammy@ubuntu:~$ rustc --version
rustc 1.66.1 (90743e729 2023-01-10)
sammy@ubuntu:~$ 

第 3 步 — 安装编译器

Rust 需要一个链接器程序将编译后的输出连接到一个文件中。gcc包中的GNU编译器集合 包含一个链接器。如果不安装gcc,那么在尝试编译时可能会出现以下错误:

error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: aborting due to previous error

可以使用apt安装build-essential软件包。

首先,更新Apt包索引:

sudo apt update

如果出现提示,请输入密码以继续。apt update命令输出可以升级的软件包列表。例如:

Outputsammy@ubuntu:~$ sudo apt update
[sudo] password for sammy: 
Hit:1 http://mirrors.digitalocean.com/ubuntu focal InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease [114 kB]                                   
Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease                                   
Get:4 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease [108 kB]                               
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 Packages [2336 kB]
Get:7 http://mirrors.digitalocean.com/ubuntu focal-updates/main Translation-en [403 kB]
Get:8 http://mirrors.digitalocean.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.2 kB]
Get:9 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted amd64 Packages [1560 kB]
Get:10 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted Translation-en [220 kB]
Get:11 http://mirrors.digitalocean.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B]
Get:12 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 Packages [1017 kB]
Get:13 http://mirrors.digitalocean.com/ubuntu focal-updates/universe Translation-en [236 kB]
Get:14 http://mirrors.digitalocean.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.2 kB]
Get:15 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 Packages [25.2 kB]
Get:16 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse Translation-en [7408 B]
Get:17 http://mirrors.digitalocean.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [604 B]
Get:18 http://mirrors.digitalocean.com/ubuntu focal-backports/main amd64 Packages [45.7 kB]
Get:19 http://mirrors.digitalocean.com/ubuntu focal-backports/main Translation-en [16.3 kB]
Get:20 http://mirrors.digitalocean.com/ubuntu focal-backports/main amd64 c-n-f Metadata [1420 B]
Get:21 http://mirrors.digitalocean.com/ubuntu focal-backports/universe amd64 Packages [24.9 kB]
Get:22 http://mirrors.digitalocean.com/ubuntu focal-backports/universe Translation-en [16.3 kB]
Get:23 http://mirrors.digitalocean.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [880 B]
Get:24 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1960 kB]
Get:25 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [320 kB]
Get:26 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [11.7 kB]
Get:27 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1463 kB]
Get:28 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [207 kB]
Get:29 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [624 B]
Get:30 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [786 kB]
Get:31 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [152 kB]
Get:32 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [16.9 kB]
Get:33 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [22.2 kB]
Get:34 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5464 B]
Get:35 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [516 B]
Fetched 11.2 MB in 5s (2131 kB/s)                          
Reading package lists... Done
Building dependency tree       
Reading state information... Done
103 packages can be upgraded. Run 'apt list --upgradable' to see them.
sammy@ubuntu:~$ 

接下来,升级所有过时的软件包:

sudo apt upgrade

如果提示继续升级,请输入Y

升级完成后,安装build-essential软件包:

sudo apt install build-essential

当提示继续安装时输入Y。当终端返回到命令提示符并且没有错误消息时,安装完成。

第 4 步 — 创建、编译和运行测试程序

在此步骤中,将创建一个测试程序来尝试 Rust 并验证它是否正常工作。

首先创建一些目录来存储测试脚本:

mkdir ~/rustprojects
cd ~/rustprojects
mkdir testdir
cd testdir

使用nano或喜欢的文本编辑器创建一个文件testdir来存储 Rust 代码:

nano test.rs

需要为所有 Rust 程序使用.rs扩展。

将以下代码复制到test.rs并保存文件:

test.rs

fn main() {
    println!("Congratulations! Your Rust program works.");
}

使用以下命令编译代码:

rustc test.rs

运行生成的可执行文件:

./test

程序打印到终端:

Outputsammy@ubuntu:~/rustprojects/testdir$ ./test
Congratulations! Your Rust program works.
sammy@ubuntu:~/rustprojects/testdir$

其他常用 Rust 命令

定期更新 Ubuntu 上的 Rust 安装是必要的。

输入以下命令来更新 Rust:

rustup update

还可以从系统中删除 Rust 及其关联的存储库。

输入以下命令来卸载 Rust:

rustup self uninstall

系统会提示输入Y以继续卸载过程:

Outputammy@ubuntu:~/rustprojects/testdir$ rustup self uninstall


Thanks for hacking in Rust!

This will uninstall all Rust toolchains and data, and remove
$HOME/.cargo/bin from your PATH environment variable.

Continue? (y/N)

输入Y以继续:

OutputContinue? (y/N) y

info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled
sammy@ubuntu:~/rustprojects/testdir$ 

Rust 已从系统中删除。

结论

现在已经在 Ubuntu 上安装并测试了 Rust。

你可能感兴趣的:(Linux,ubuntu,rust,linux)