웹사이트 검색

Ubuntu 20.04에 Rust 프로그래밍 언어를 설치하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • Ubuntu 22.04(Jammy Jellyfish)
  • Ubuntu 20.04(Focal Fossa)

이 페이지에서

  1. 전제 조건
  2. 러스트 설치
  3. Rust로 샘플 애플리케이션 만들기\n
  4. 결론

Rust는 2006년 Graydon Hoare가 개발한 오픈 소스이자 현재 매우 인기 있는 프로그래밍 언어입니다. Rust는 매우 빠르고 세그폴트를 방지하며 스레드 및 메모리 안전을 보장합니다. 비용이 들지 않는 추상화, 데이터 경합 없는 스레드, 의미 체계 이동, 효율적인 C 바인딩, 최소 런타임 및 패턴 일치를 지원합니다. C++과 매우 유사하며 여러 플랫폼에서 실행할 수 있습니다.

이 튜토리얼에서는 Ubuntu 20.04에 Rust 프로그래밍 언어를 설치하는 방법을 보여줍니다.

전제 조건

  • Ubuntu 20.04를 실행하는 서버.\n
  • 루트 암호는 서버에서 구성됩니다.\n

녹 설치

Rust를 설치하려면 Curl 및 기타 패키지를 시스템에 설치해야 합니다.

apt-get install curl build-essential make gcc -y

Curl 패키지를 설치한 후 아래와 같이 Rust 설치 스크립트를 다운로드하여 실행합니다.

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | 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:

  /root/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /root/.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:

  /root/.cargo/bin

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

  /root/.profile
  /root/.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

1을 입력하고 Enter 키를 눌러 계속하십시오. 다음 출력이 표시되어야 합니다.

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 2021-06-17, rust version 1.53.0 (53cb7b09b 2021-06-17)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 48.4 MiB /  48.4 MiB (100 %)  26.8 MiB/s in  1s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 16.1 MiB /  16.1 MiB (100 %)   1.9 MiB/s in  6s ETA:  0s
info: installing component 'rust-std'
 25.3 MiB /  25.3 MiB (100 %)   5.8 MiB/s in  4s ETA:  0s
info: installing component 'rustc'
 48.4 MiB /  48.4 MiB (100 %)   7.1 MiB/s in  7s 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.53.0 (53cb7b09b 2021-06-17)


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

설치 후 현재 쉘에 대해 Rust 환경을 활성화해야 합니다. 다음 명령을 사용하여 활성화할 수 있습니다.

source ~/.profile
source ~/.cargo/env

다음으로 다음 명령을 사용하여 Rust 버전을 확인합니다.

rustc --version

다음 출력이 표시되어야 합니다.

rustc 1.53.0 (53cb7b09b 2021-06-17)

Rust로 샘플 애플리케이션 만들기

다음으로 Rust를 테스트하기 위한 샘플 애플리케이션을 만들어야 합니다.

먼저 애플리케이션용 디렉터리를 만듭니다.

mkdir app

다음으로 디렉터리를 앱으로 변경하고 다음 명령을 사용하여 샘플 애플리케이션을 만듭니다.

cd app
nano app.rs

다음 코드를 추가합니다.

fn main() {
    println!("Hello World, this is my first app");
}

파일을 저장하고 닫은 후 다음 명령을 사용하여 프로그램을 컴파일합니다.

rustc app.rs

이렇게 하면 현재 디렉터리에 app이라는 실행 파일이 생성됩니다.

다음으로 다음 명령을 사용하여 프로그램을 실행합니다.

./app

다음 출력이 표시되어야 합니다.

Hello World, this is my first app

Rust 패키지를 업데이트하려면 다음 명령을 실행합니다.

rustup update

다음 출력이 표시되어야 합니다.

info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.53.0 (53cb7b09b 2021-06-17)

시스템에서 Rust를 제거하려면 다음 명령을 실행하십시오.

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

info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled

결론

축하합니다! Ubuntu 20.04 서버에 Rust를 성공적으로 설치했습니다. 이제 Rust를 사용하여 매우 적은 메모리 사용량으로 매우 빠른 코드를 작성할 수 있습니다.