Ubuntu 20.04 に Rust をインストールする方法

はじめに

Rustプログラミング言語は、rust-langとしても知られる強力な汎用プログラミング言語です。RustはC++と文法的に似ており、ブラウザのコンポーネント、ゲームエンジン、オペレーティングシステムなど、さまざまなソフトウェア開発プロジェクトに使用されています。

このチュートリアルでは、Ubuntu 20.04に最新バージョンのRustをインストールして、テストプログラムを作成、コンパイル、実行します。このチュートリアルの例では、Rustのバージョン1.66のインストール方法を紹介しています。

Note

注:このチュートリアルはUbuntu 22.04でも有効ですが、apt upgradeを実行する際にさまざまな質問に対して対話型のダイアログが表示されることがあります。たとえば、必要に応じて自動的にサービスを再起動するか、変更した設定ファイルを置き換えるかどうかなどを尋ねられることがあります。これらの質問への回答はソフトウェアや個人の好みによって異なり、このチュートリアルの範囲外です。

前提条件

このチュートリアルを完了するためには、sudoを有効にした非ルートユーザーとファイアウォールを備えたUbuntu 20.04サーバーが必要です。Ubuntu 20.04の初期サーバーセットアップチュートリアルに従って、これをセットアップすることができます。

ステップ1 — rustupツールを使用してUbuntuにRustをインストールする

LinuxでRustをインストールする方法はいくつかありますが、推奨される方法はrustupコマンドラインツールを使用することです。

rustupツールをダウンロードして、最新の安定版のRustをインストールするためのコマンドを実行してください。

  1. curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh

 

インストールの種類を選択するように求められます。 (Insutōru no shurui o sentaku suru you ni motomeraremasu.)

Output

sammy@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の出力は次のとおりです。

Output

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’ 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環境変数に追加するため、以下のコマンドを実行してください。

  1. source $HOME/.cargo/env

 

ステップ2 — インストールの確認

バージョンをリクエストして、Rustのインストールを確認してください。

  1. rustc –version

 

システムにインストールされたRustプログラミング言語のバージョンを取得するために、rustc –versionコマンドを使用します。例えば、

Output

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

ステップ3 — コンパイラのインストール

Rustは、コンパイルされた出力ファイルを1つのファイルに結合するためにリンカープログラムが必要です。ビルド必須パッケージの中には、GNUコンパイラコレクション(gcc)にリンカーが含まれています。もしgccをインストールしない場合、コンパイルを試みた際に以下のエラーが発生する可能性があります。

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

error: aborting due to previous error

ビルドに必要なパッケージをインストールするために、aptを使用します。

最初に、Aptパッケージのインデックスを更新してください。

  1. sudo apt update

 

必要に応じて、続行するためにパスワードを入力してください。apt updateコマンドはアップグレード可能なパッケージのリストを出力します。例えば、

Output

sammy@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:~$

次に、古くなったパッケージをアップグレードしてください。

  1. sudo apt upgrade

 

アップグレードを続けるように求められた場合は、Yを入力してください。

アップグレードが完了したら、build-essentialパッケージをインストールしてください。

  1. sudo apt install build-essential

 

インストールを続けるように促されたら、Yを入力してください。エラーメッセージがなく、コマンドプロンプトに戻ったら、インストールは完了です。

ステップ4 – テストプログラムの作成、コンパイル、実行

このステップでは、Rustを試して正常に動作していることを確認するためのテストプログラムを作成します。

テストスクリプトを保存するためのディレクトリを作成してください。

  1. mkdir ~/rustprojects
  2. cd ~/rustprojects
  3. mkdir testdir
  4. cd testdir

 

ナノ、またはお気に入りのテキストエディタを使用して、Rustのコードを保存するためのファイルをtestdirに作成してください。

  1. nano test.rs

 

すべてのRustプログラムには.rs拡張子を使用する必要があります。

以下のコードをtest.rsにコピーして、ファイルを保存してください。

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

rustcコマンドを使用してコードをコンパイルしてください。

  1. rustc test.rs

 

生成された実行可能ファイルを実行してください。

  1. ./test

 

プログラムはターミナルに出力します。

Output

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

他のよく使われるRustコマンド

UbuntuでRustのインストールを定期的に更新するのは良いアイデアです。

以下のコマンドを入力して、Rustをアップデートしてください。

  1. rustup update

 

システムから関連するリポジトリと一緒に、錆も取り除くことができます。

以下のコマンドを入力してRustをアンインストールしてください。

  1. rustup self uninstall

 

アンインストールのプロセスを続行するためにYを入力してください。 (Aninsutōru no purosesu o zokkō suru tame ni Y o nyūryoku shite kudasai.)

Output

ammy@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を入力してください。」

Output

Continue? (y/N) y info: removing rustup home info: removing cargo home info: removing rustup binaries info: rustup is uninstalled sammy@ubuntu:~/rustprojects/testdir$

あなたのシステムから錆が取り除かれました。

結論

今、UbuntuにRustをインストールしてテストを行ったので、Ubuntuのチュートリアルで学習を続けてください。

コメントを残す 0

Your email address will not be published. Required fields are marked *