如何在 Ubuntu 和其它的 Linux 发行版安装 Rust 和 Cargo
Rust Cargo

> 
> 了解在 Linux 上安装 Rust 编程语言及其包管理器 Cargo 的各种方法。
> 
> 
> 


![](/data/attachment/album/202110/31/150031j9e0xgeg9jpxeip0.jpg)


自从 Mozilla 贡献了 [Rust](https://www.rust-lang.org/) 语言之后，它就获得了更加突出和受欢迎的地位。口说无凭。[Rust 将被使用在 Linux 内核中](https://www.zdnet.com/article/rust-in-the-linux-kernel-why-it-matters-and-whats-happening-next/)，它是继 C 语言之后的第二种编程语言。


许多开发人员也开始使用 Rust 语言制作很棒的基于命令行的工具。这些工具通常可以通过 [Cargo 包管理器](https://crates.io/) 获得。


这就是为什么在 Linux 中安装 Rust 支持对程序员和最终用户都很重要的原因。


官方 Rust 文档建议通过以下这种方式下载并执行安装程序脚本在 Linux 中安装 Rust：



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

```

这是可行的。但是，你的发行版可能已经打包了 Rust。使用你的发行版的 [包管理器](https://itsfoss.com/package-manager/) 也是一种选择。


让我向你介绍官方 Rust 方式和包管理器方式的 Rust 安装步骤。


### 方法1：使用官方方法在任意的 Linux 上安装 Rust


这种方法有几个优点：


* 你可以获取最新的 Rust 和 Cargo 版本
* Rust 仅仅是为当前的用户安装，并不是所有用户
* 你并不需要 root 用户或者 `sudo` 权限就可以为自己安装 Rust


一些人不喜欢从互联网下载并 [运行 shell](https://itsfoss.com/run-shell-script-linux/) 脚本，即使它来自官方。但是，由于它不需要 root 访问权限并且脚本来自官方，因此以这种方式安装它应该是安全的。


首先，确保你已经安装了 `curl`。如果没安装，使用你的发行版的包管理器安装它。你可以使用 `apt` 命令在 [Ubuntu 和 Debian 上安装 Curl](https://itsfoss.com/install-curl-ubuntu/)。



```
sudo apt install curl

```

接下来，使用这条命令下载脚本并运行它：



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

```

![](/data/attachment/album/202110/31/150036mba1shcjwihv7xsk.png)


它会询问你输入所需的安装类型。选择选项 `1`：


![](/data/attachment/album/202110/31/150036o1igbiwb46f66i44.png)


脚本完成安装后，你需要获取配置文件的来源，以使更改应用于你的 shell。


![](/data/attachment/album/202110/31/150036jad8dbuydb7f43d3.png)


应用文件：



```
source $HOME/.cargo/env

```

完成后，通过检查已安装的版本来验证 Rust 是否可用：



```
rustc --version

```

![Verifying Rust installation](/data/attachment/album/202110/31/150036kc0coytuqdbt9ydc.png)


太酷了。如果 Rust 发布了新版本，你可以使用如下命令更新已安装的版本：



```
rustup update

```

在你看到其它安装方法之前，让我快速展示如何移除以这种方式安装的 Rust。


#### 删除以官方方式安装的 Rust


在终端中，使用如下的命令从系统中移除 Rust：



```
rustup self uninstall

```

询问时按 `Y`，你将从系统中删除 Rust。


![Removing Rust from Linux](/data/attachment/album/202110/31/150037tsyyz4us55ooudsg.png)


官方方式安装已经完成。让我们看看如何使用 `apt` 命令在 Ubuntu 上安装 Rust。


### 方法2：使用 apt 命令在 Ubuntu 上安装 Rust


为什么要使用包管理器？


* 它在整个系统范围内安装 Rust，可供系统上的所有用户使用
* 它与其它系统更新一起集中更新（如果你的发行版添加了新版本的 Rust）


Ubuntu 中有两个主要的 Rust 包：


* `rustc`：用于 Rust 编程语言的 Rust 编译器
* `cargo`：Cargo 是 Rust 的包管理器，它会自动安装 `rustc`


作为一个普通用户，你将使用 Cargo 来安装基于 Rust 的应用程序。作为程序员，你需要 Cargo 来获取其它 Rust 包或创建自己的包。


由于 `cargo` 包含 `rustc`，所以我建议安装它，以便一次性安装所有必需的软件包。



```
sudo apt install cargo

```

系统会要求你输入账号密码。


![](/data/attachment/album/202110/31/150037fz271l9qblfllldf.png)


当然，你可以自由使用 `apt install rustc`，只安装 Rust。这取决于你的选择。


你可以验证是否为你和所有其他用户安装了 Rust（如果你愿意）：


![](/data/attachment/album/202110/31/150037s4330z0m3gx63mzz.png)


很好。让我也快速展示卸载步骤。


#### 使用 apt remove 移除 Rust


要移除 Rust，你可以先移除 Cargo，然后使用 `autoremove` 命令移除随它安装的依赖项。



```
sudo apt remove cargo

```

现在运行 `autoremove`：



```
sudo apt autoremove

```

就是这样。你现在了解了在 Ubuntu 和其它 Linux 发行版上安装 Rust 的所有基本知识。随时欢迎提出问题和建议。




---


via: <https://itsfoss.com/install-rust-cargo-ubuntu-linux/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[perfiffer](https://github.com/perfiffer) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

*Brief: Learn various ways of installing Rust programming language on Linux along with its package manager Cargo.*

Ever since Mozilla dumped [Rust](https://www.rust-lang.org/?ref=itsfoss.com), it has gained even more prominence and popularity. Don’t just take my words for it. [Rust is going to be included in the Linux kernel](https://www.zdnet.com/article/rust-in-the-linux-kernel-why-it-matters-and-whats-happening-next/?ref=itsfoss.com), only the second programming language after C.

Many developers have also started making awesome command line based tools in Rust. These tools are often available through the [Cargo package manager](https://crates.io/?ref=itsfoss.com).

This is why it matters to install Rust support in Linux, both for the programmers and the end users.

Officially, Rust documents suggest installing Rust in Linux by downloading and executing the installer script in this manner:

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


That could work. However, your distribution probably already packages Rust. Using your distribution’s [package manager](https://itsfoss.com/package-manager/) is also an option.

Let me walk you through the Rust installation steps for both official Rust way and package manager way.

## Method 1: Installing Rust on any Linux using the official method

*There are a few advantages of this method:*

- You get the latest Rust and Cargo version
- Rust is only installed for the current user, not system-wide
- You do not need to be root or have sudo access to install Rust for yourself this way

A few people dislike downloading and [running shell scripts](https://itsfoss.com/run-shell-script-linux/) off the internet, even if it is coming from the official sources. However, since it does not need root access and the script is coming from the official sources, it should be safe to install it this way.

First, **make sure that you have Curl installed**. Use your distribution’s package manager to install it, if it is not already installed. You can [install Curl on Ubuntu](https://itsfoss.com/install-curl-ubuntu/) and Debian using the apt command:

`sudo apt install curl`


Next, use this command to download the script and run it:

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


![Run the installer script provided in the official website to start the installation process](https://itsfoss.com/content/images/2023/03/installing-rust-linux.webp)

It will ask for your input on the kind of installation you want. **Go with option 1**:

![Select the default install option, which is number 1, when asked to select the method of installation](https://itsfoss.com/content/images/2023/03/installing-rust-linux-2.webp)

Once the script finishes the installation process, you need to source the configuration file to make the changes available for your shell.

![Execute the command that sorces the configuration, once the installation is finished](https://itsfoss.com/content/images/2023/03/installing-rust-linux-3.webp)

Source the file:

`source $HOME/.cargo/env`


Once it is done, verify that rust is available for you by checking the installed version:

`rustc --version`


![Verify the instaaltion of Rust by checing the version of rust installed in the terminal](https://itsfoss.com/content/images/2023/03/verify-rust-version.webp)

Here's a replay of the Rust installation (click to enlarge):

`rustup update`

Let me quickly show how to remove rust installed this way before you see the other installation method.

### Removing Rust installed the official way

In a terminal, use the following command to remove Rust from your system:

`rustup self uninstall`


Press Y when asked and you’ll have Rust removed from the system.

![Remove the installed rust through Shell script](https://itsfoss.com/content/images/2023/03/remove-rust-linux.webp)

Here's a replay of removing Rust installed the official way.

That’s settled then. Let’s see how to use the apt command to install Rust on Ubuntu.

## Method 2: Installing Rust support on Ubuntu using apt

*Why would you want to use the package manager?*

- It installs Rust system-wide and thus makes it available for all the users on the system
- It gets updated centrally with other system updates (if your distribution adds a new version of Rust)

There are two main Rust packages available in Ubuntu:

- rustc: The rust compiler used for Rust programming language
- cargo: Cargo is the Rust package manager and it automatically installs rustc

As a regular user, you would be using Cargo for installing Rust-based applications. As a programmer, you’ll need Cargo to get other Rust packages or create your own.

Since Cargo includes `rustc`

, I would advise installing it so that all required packages are installed in one go.

`sudo apt install cargo`


You’ll be asked to enter your account’s password.

![Install Rust using th APT packag manager. Provide the password one asked.](https://itsfoss.com/content/images/2023/03/install-rust-using-apt-ubuntu.webp)

You are, of course, free to use `apt install rustc`

and install only Rust. That choice lies with you.

You may verify that Rust is installed for you and all the other users (if you want to):

![Verify the installed Rust by checking version. (Both the per user and system-wide)](https://itsfoss.com/content/images/2023/03/verify-rust-install-ubuntu.webp)

Here's a replay of Rust installation with apt in Ubuntu.

That’s good. Let me quickly show the uninstall step as well.

### Removing Rust using apt remove

To remove Rust, you can remove Cargo first and then use the autoremove command to remove the dependencies installed with it.

`sudo apt remove cargo`


Now run the autoremove:

`sudo apt autoremove`


Here's the replay uninstall Rust with apt command:

That’s it. You now know all the essentials about installing Rust on Ubuntu and other Linux distributions. Questions and suggestions are always welcome.