웹사이트 검색

Ubuntu 20.04에 Rust를 설치하는 방법


소개

Rust-lang이라고도 하는 Rust 프로그래밍 언어는 강력한 범용 프로그래밍 언어입니다. Rust는 문법적으로 C++와 유사하며 브라우저 구성 요소, 게임 엔진 및 운영 체제를 포함한 광범위한 소프트웨어 개발 프로젝트에 사용됩니다.

이 튜토리얼에서는 최신 버전의 Rust를 Ubuntu 20.04에 설치한 다음 테스트 프로그램을 생성, 컴파일 및 실행합니다. 이 튜토리얼의 예제는 Rust 버전 1.66의 설치를 보여줍니다.

참고: 이 튜토리얼은 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

설치 유형을 선택하라는 메시지가 표시됩니다.

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

rustc --version 명령은 시스템에 설치된 Rust 프로그래밍 언어의 버전을 반환합니다. 예를 들어:

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

3단계 - 컴파일러 설치

Rust는 컴파일된 출력을 하나의 파일로 결합하기 위해 링커 프로그램이 필요합니다. build-essential 패키지의 GNU 컴파일러 컬렉션(gcc)에는 링커가 포함되어 있습니다. 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 패키지 색인을 업데이트합니다.

  1. sudo apt update

메시지가 표시되면 계속하려면 암호를 입력하십시오. apt update 명령은 업그레이드할 수 있는 패키지 목록을 출력합니다. 예를 들어:

Output
sammy@ubuntu:~$ sudo apt update [sudo] password for sammy: Hit:1 http://mirrors.linux-console.net/ubuntu focal InRelease Get:2 http://mirrors.linux-console.net/ubuntu focal-updates InRelease [114 kB] Hit:3 https://repos-droplet.linux-console.net/apt/droplet-agent main InRelease Get:4 http://mirrors.linux-console.net/ubuntu focal-backports InRelease [108 kB] Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:6 http://mirrors.linux-console.net/ubuntu focal-updates/main amd64 Packages [2336 kB] Get:7 http://mirrors.linux-console.net/ubuntu focal-updates/main Translation-en [403 kB] Get:8 http://mirrors.linux-console.net/ubuntu focal-updates/main amd64 c-n-f Metadata [16.2 kB] Get:9 http://mirrors.linux-console.net/ubuntu focal-updates/restricted amd64 Packages [1560 kB] Get:10 http://mirrors.linux-console.net/ubuntu focal-updates/restricted Translation-en [220 kB] Get:11 http://mirrors.linux-console.net/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B] Get:12 http://mirrors.linux-console.net/ubuntu focal-updates/universe amd64 Packages [1017 kB] Get:13 http://mirrors.linux-console.net/ubuntu focal-updates/universe Translation-en [236 kB] Get:14 http://mirrors.linux-console.net/ubuntu focal-updates/universe amd64 c-n-f Metadata [23.2 kB] Get:15 http://mirrors.linux-console.net/ubuntu focal-updates/multiverse amd64 Packages [25.2 kB] Get:16 http://mirrors.linux-console.net/ubuntu focal-updates/multiverse Translation-en [7408 B] Get:17 http://mirrors.linux-console.net/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [604 B] Get:18 http://mirrors.linux-console.net/ubuntu focal-backports/main amd64 Packages [45.7 kB] Get:19 http://mirrors.linux-console.net/ubuntu focal-backports/main Translation-en [16.3 kB] Get:20 http://mirrors.linux-console.net/ubuntu focal-backports/main amd64 c-n-f Metadata [1420 B] Get:21 http://mirrors.linux-console.net/ubuntu focal-backports/universe amd64 Packages [24.9 kB] Get:22 http://mirrors.linux-console.net/ubuntu focal-backports/universe Translation-en [16.3 kB] Get:23 http://mirrors.linux-console.net/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

nano 또는 선호하는 텍스트 편집기를 사용하여 Rust 코드를 저장할 파일을 testdir에 생성합니다.

  1. nano test.rs

모든 Rust 프로그램에 대해 .rs 확장자를 사용해야 합니다.

다음 코드를 test.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를 제거할 수도 있습니다.

Rust를 제거하려면 다음 명령을 입력하십시오.

  1. rustup self uninstall

제거 프로세스를 계속하려면 Y를 입력하라는 메시지가 표시됩니다.

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 튜토리얼을 통해 학습을 계속하십시오.