LoginSignup
73
58

More than 1 year has passed since last update.

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

Last updated at Posted at 2019-04-14

UbuntuにRustをインストールしたのでメモとして残しておきます。この記事ではUbuntu18.04にRust1.34.0を入れた記事になっています。Ubuntu22.04にRust1.62.0をインストールした記事はこちらにあります。

環境

  • Ubuntu 18.04
  • rust 1.34.0

インストール

curl https://sh.rustup.rs -sSf | shを実行するだけで簡単にインストールできます。途中でインストールの方法を選択できますがここではデフォルトの1を選択しています。rustup.rsは中身はBashスクリプトになっていてrustupやrustc、cargoなどを$HOME/.cargo/binにダウンロードしてくるようです。スクリプトの中身はrustup.rsのGitHubのrustup-init.shです。

~$ curl 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.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:

  /home/yoshi/.cargo/bin

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

  /home/yoshi/.profile

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
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1 <--ここで1を入力する

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2019-04-11, rust version 1.34.0 (91856ed52 2019-04-10)
info: downloading component 'rustc'
 85.3 MiB /  85.3 MiB (100 %)  15.9 MiB/s ETA:   0 s                
info: downloading component 'rust-std'
 56.2 MiB /  56.2 MiB (100 %)  16.3 MiB/s ETA:   0 s                
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: installing component 'rustc'
 85.3 MiB /  85.3 MiB (100 %)  12.5 MiB/s ETA:   0 s                
info: installing component 'rust-std'
 56.2 MiB /  56.2 MiB (100 %)  15.0 MiB/s ETA:   0 s                
info: installing component 'cargo'
info: installing component 'rust-docs'
 10.2 MiB /  10.2 MiB (100 %)   4.2 MiB/s ETA:   0 s                
info: default toolchain set to 'stable'

  stable installed - rustc 1.34.0 (91856ed52 2019-04-10)


Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH 
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env

インストール先

インストールメッセージの最後の方にRustを使用するにはCargoのbinディレクトリ($HOME/.cargo/bin)を環境変数PATHに追加してくださいとあります。lsコマンドでそのディレクトリを確認するとRustコンパイラのrustc以外に、コンパイルマネージャとパッケージマネージャを兼ねるcargo、ドキュメンテーションツールのrustdoc、これらツールチェーンのインストーラrustupもインストールされています。rust-lldbとかrust-gdbとかデバッガらしきものもインストールされています。

~$ ls $HOME/.cargo/bin/
cargo  cargo-clippy  cargo-fmt  cargo-miri  clippy-driver  rls  rust-gdb  rust-lldb  rustc  rustdoc  rustfmt  rustup

パスの設定

親切にもパスを通すスクリプト$HOME/.cargo/envを用意してくれています。中身はexportコマンドで$HOME/.cargo/binをパスに追加しています。これをsourceコマンドで読み込みます。

~$ cat $HOME/.cargo/env
export PATH="$HOME/.cargo/bin:$PATH"
~$ source $HOME/.cargo/env

実行確認

cargorustcrustdocrustupは無事インストールされています。

~$ cargo --version
cargo 1.34.0 (6789d8a0a 2019-04-01)
~$ rustc --version
rustc 1.34.0 (91856ed52 2019-04-10)
~$ rustdoc --version
rustdoc 1.34.0 (91856ed52 2019-04-10)
~$ rustup --version
rustup 1.17.0 (069c88ed6 2019-03-05)

rust-lldbでデバッグしたかったのですが、rust-lldbを実行するとlldbがないと怒られました。aptコマンドでlldbを入れると無事起動しました。

~$ rust-lldb 
/home/yoshi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rust-lldb: 34: exec: lldb: not found

~$ sudo apt install lldb

~$ lldb --version
lldb version 6.0.0

~$ rust-lldb 
(lldb) command script import "/home/yoshi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/etc/lldb_rust_formatters.py"
(lldb) type summary add --no-value --python-function lldb_rust_formatters.print_val -x ".*" --category Rust
(lldb) type category enable Rust
(lldb) 

ビルド

さっそく簡単なプログラムを書いて動かしてみます。

  • rustcで直接コンパイル

    ~$ vim hello.rs
    fn main() {
      println!("Hello, world!");
    }
    ~$ rustc hello.rs -o hello
    ~$ ./hello 
    Hello, world!
    
  • cargoでコンパイル

    ~$ cargo new hello
      Created binary (application) `hello` package
    ~$ cd hello
    ~$ cargo build
      Compiling hello v0.1.0 (/home/yoshi/rust/hello)
      Finished dev [unoptimized + debuginfo] target(s) in 0.26s
    ~$ cargo run
      Finished dev [unoptimized + debuginfo] target(s) in 0.00s
      Running `target/debug/hello`
    Hello, world!
    

cargoのオプション

その他のcargoのオプションです。そのうち細かいところも見ていこうと思います。

オプション 説明
build プロジェクトのビルドをする
check ビルドできるかのチェックをする
clean プロジェクトのクリーンをする
doc ドキュメントを作成する
new プロジェクトの作成
init 既存のディレクトリでプロジェクトの作成
run 実行ファイルを実行する
test テストを実行する
bench ベンチマークを実行する
update Cargo.lockに記述された依存をアップデートする
search crate.ioからパッケージを探します
public パッケージをcreate.ioに公開します
install rust製のバイナリをインストールする
uninstall rust製のバイナリアンインストールします
73
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
73
58