Ubuntu 20.04 にSwiftをインストールする
はじめに
Ubuntu 20.04 にSwiftをインストールするためのメモ。
環境
$ hostnamectl status
Static hostname: ---
Icon name: computer-vm
Chassis: vm
Machine ID: ---
Boot ID: ---
Virtualization: kvm
Operating System: Ubuntu 20.04.4 LTS
Kernel: Linux 5.4.0-105-generic
Architecture: x86-64Swiftをインストール
1. Swiftのパッケージをダウンロード
公式ページより、Swiftのパッケージをダウンロードする。
Download Swift からダウンロードする場合、OSとアーキテクチャの選択を間違わないように。本メモでは、OSをUbuntu 20.04、アーキテクチャをx86_64、Swiftバージョンを5.6.1のパッケージを選択する。
$ wget //download.swift.org/swift-5.6.1-release/ubuntu2004/swift-5.6.1-RELEASE/swift-5.6.1-RELEASE-ubuntu20.04.tar.gz2. パッケージを展開
パッケージを展開するディレクトリを作成する。
$ sudo mkdir /usr/share/swiftダウンロードしたパッケージを作成したディレクトリに展開する。
$ sudo tar -xvf swift-5.6.1-RELEASE-ubuntu20.04.tar.gz -C /usr/share/swift3. PATHを追加
コマンドラインからswiftコマンドを実行するためにシェルにPATHを追加し、有効化する。本環境では、Bashを利用している。
$ echo "export PATH=/usr/share/swift/swift-5.6.1-RELEASE-ubuntu20.04/usr/bin/:$PATH" >> ~/.bashrc \
&& source ~/.bashrc4. Swiftのバージョンを確認
swiftコマンドを実行し、バージョンを確認する。
$ swift --version
Swift version 5.6.1 (swift-5.6.1-RELEASE)
Target: x86_64-unknown-linux-gnu5. Hello Worldを実行
「Hello World」をコマンドラインに出力する。
a. ファイルを作成する場合
hello.swiftを作成し、実行する例です。
$ echo 'print("Hello World")' > ./hello.swifthello.swiftを実行する。
$ swift ./hello.swift
Hello Worldb. 対話型で実行する例です。
コマンドラインでswiftコマンドを引数なしで実行する。
$ swift
Swift version 5.6.1 (swift-5.6.1-RELEASE)
Target: x86_64-unknown-linux-gnu
Welcome to Swift!
Subcommands:
swift build Build Swift packages
swift package Create and work on packages
swift run Run a program from a package
swift test Run package tests
swift repl Experiment with Swift code interactively (default)
Use `swift --help` for descriptions of available options and flags.
Use `swift help <subcommand>` for more information about a subcommand.
Welcome to Swift version 5.6.1 (swift-5.6.1-RELEASE).
Type :help for assistance.
1> print("Hello World")
Hello World
2> ^D対話型を終了する場合は、Ctrl + dを入力してください。
終わり
About
関西在住のエンジニア
言語: Ruby・Go・PHP
フレームワーク: RoR・Gin
趣味: 自作PC