如何在 Ubuntu 20.04 上安装 Rust

这篇具有很好参考价值的文章主要介绍了如何在 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。文章来源地址https://www.toymoban.com/news/detail-852396.html

到了这里,关于如何在 Ubuntu 20.04 上安装 Rust的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 如何在Ubuntu 20.04|18.04上安装 FreeSwitch

    PBX 是 Private Branch Exchange 的缩写。它是在组织内部使用的专用电话系统,并根据特定组织的需求进行定制。组织中的 PBX 用户可以在内部进行内部通信,也可以与外界进行外部通信。VoIP(IP 语音)、ISDN(综合业务数字网络)或模拟等渠道促进了外部通信。 使用 PBX 电话系统的

    2024年02月09日
    浏览(63)
  • 如何在ubuntu20.04安装中文输入法

             在 Ubuntu 20.04 上安装中文输入法有许多选择,以下是其中两种最常用的方法: 打开终端,并执行以下命令以安装 Fcitx 框架和相应的中文输入法: 安装完成后,打开“Settings”(设置)应用程序,然后选择“Region Language”(区域和语言)选项卡。 在“Input Sources”

    2024年02月05日
    浏览(48)
  • Ubuntu20.04(linux)安装JDK1.8

    Ubuntu20.04安装JDK1.8 1.下载 : jdk-8u371-linux-x64.tar.gz 2.解压到指定目录 3.在/usr/lib/ 创建 jvm 文件夹,并将解压的jdk移动到/usr/lib/jvm(非root用户使用sudo) 4.打开/etc/profile,配置环境变量 vi /etc/profile(编辑文件) Shift + g 将光标定位到行尾 这个时候按一下 Insert 键,进入编辑模式 按Esc键 输

    2024年02月12日
    浏览(57)
  • 如何在 Ubuntu 20.04 上安装和使用 Docker

    前些天发现了一个人工智能学习网站,通俗易懂,风趣幽默,最重要的屌图甚多,忍不住分享一下给大家。点击跳转到网站。 介绍 Docker是一个可以简化 容器 中应用程序进程管理过程的应用程序。容器允许你在资源隔离的进程中运行应用程序。它们与虚拟机类似,但容器更可

    2024年01月17日
    浏览(36)
  • ubuntu20.04如何安装nvidia-docker?

    导入 NVIDIA GPG 密钥 添加 NVIDIA Docker 存储库 安装 nvidia-container-toolkit 检查nvidia-docker是否安装成功 如果 nvidia-docker 安装成功,命令将启动一个包含 NVIDIA GPU 的容器并运行 nvidia-smi 命令,该命令可以显示有关 GPU 状态的信息。输出结果将显示 GPU 型号、显存大小、温度等信息。 如

    2024年02月15日
    浏览(38)
  • 如何在 Ubuntu 20.04 上安装 Apache Kafka

    前些天发现了一个人工智能学习网站,通俗易懂,风趣幽默,最重要的屌图甚多,忍不住分享一下给大家。点击跳转到网站。 介绍 Apache Kafka是一种分布式消息代理,旨在处理大量实时数据。Kafka 集群具有高度可扩展性和容错性。与ActiveMQ和RabbitMQ等其他消息代理相比,它还具

    2024年02月01日
    浏览(24)
  • Linux学习之Ubuntu 20.04安装5.4.0内核

    参考博客:Ubuntu20.04编译内核教程 sudo lsb_release -a 可以看到我当前的系统是 Ubuntu 20.04.4 , sudo uname -r 可以看到我的系统内核版本是 5.4.0-100-generic 。 sudo apt-get install -y libncurses5-dev flex bison libssl-dev 安装所需要的依赖。 sudo apt-get install linux-source 按两下 Tab ,看一下可以下载的源

    2024年02月06日
    浏览(36)
  • Linux(7)Ubuntu20.04 arm64安装Docker

    vi /etc/apt/sources.list 这个命令后面跟了几个软件包的名字,它们分别是: apt-transport-https:这个软件包允许apt使用HTTPS协议来访问软件源。 ca-certificates:这个软件包提供了一些受信任的证书颁发机构的证书,用来验证HTTPS连接的安全性。 curl:这个软件包提供了一个命令行工具,

    2024年02月10日
    浏览(46)
  • ROS Neotic(Ubuntu 20.04)如何正确安装OpenCV

    鱼香ROS提供了一个全面且方便的脚本,可以直接运行来安装系统对应版本的ROS,同时也可以使用该命令来安装许多有趣的东西 本文安装的为无contrib版本的OpenCV GitHub OpenCV 项目地址 在源代码的目录下依次执行上方的命令,第四步cmake的过程中可以增加相关的参数来自定义Open

    2024年02月05日
    浏览(32)
  • 【Linux】Ubuntu20.04版本安装谷歌中文输入法【教程】

    使用下面的命令行下载 fcitx-googlepinyin 等待下载完成之后,可进行下一步 在菜单中找到 语言支持 第一次打开语言支持,会提示没有安装完全,点击 安装 即可 将键盘默认输入法系统改为 fcitx ,然后 重启系统 选择 配置当前输入法 将 Google拼音 放置在第一位即可完成配置 关闭

    2024年02月03日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包