3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows11にインストールしたWSL(Ubuntu)に Swift をインストールする手順をまとめます。

$ uname -a
Linux myPC 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy

↑この環境で確認しましたが、ここで説明する手順は、Ubuntu のバージョンに依らないと思います。

すべてのコマンドは、ubuntu ターミナルで実行します。

1. 前提ライブラリをインストール

$ sudo apt update
$ sudo apt install -y gcc-multilib

2. Swiftアーカイブをダウンロード

↑こちらのサイトから、該当するUbuntuバージョンとCPUアーキテクチャに対応するアーカイブURLを確認して、wgetでダウンロードする。

例えば、Ubuntu 22.04.3 LTS、インテルCPUの場合のアーカイブURL:https://download.swift.org/swift-5.10.1-release/ubuntu2204/swift-5.10.1-RELEASE/swift-5.10.1-RELEASE-ubuntu22.04.tar.gz

$ wget https://download.swift.org/swift-5.10.1-release/ubuntu2204/swift-5.10.1-RELEASE/swift-5.10.1-RELEASE-ubuntu22.04.tar.gz

3. 解凍(デプロイ)

ダウンロードしたアーカイブファイル名をlsで確認してtarで解凍する。

$ ls
swift-5.10.1-RELEASE-ubuntu22.04.tar.gz

$ tar xzf swift-5.10.1-RELEASE-ubuntu22.04.tar.gz

./swift-5.10.1-RELEASE-ubuntu22.04/usr/配下に展開される。

4. PATHの追加

$ export PATH=/path/to/usr/bin:"${PATH}"

必要により、.bashrc等に追加しておく。

今回は、以下のパスを追加した。

$ export PATH=$HOME/swift-5.10.1-RELEASE-ubuntu22.04/usr/bin:"${PATH}"

以上で、インストール完了です。
実際に動作確認します。

5. 確認

$ swift --version
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: x86_64-unknown-linux-gnu
$ cat hello.swift
print("Hello!")

$ swift hello.swift
Hello!
$ swiftc hello.swift

$ ls
hello  hello.swift

$ ./hello
Hello!
Swift Package
$ mkdir swift_pj
$ cd swift_pj
$ swift package init --type executable
Creating executable package: swift_pj
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift

$ swift run
Building for debugging...
[8/8] Linking swift_pj
Build complete! (2.33s)
Hello, world!

Replも確認しておきます。

Repl
$ swift repl
Welcome to Swift version 5.10.1 (swift-5.10.1-RELEASE).
Type :help for assistance.
  1> print("hello")
hello
  2> ^D

すべて期待通りに動きました。


最後に、関連パスを確認しておきます。

$ swift -v -print-target-info
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: x86_64-unknown-linux-gnu
/home/nak435/swift-5.10.1-RELEASE-ubuntu22.04/usr/bin/swift-frontend -frontend -print-target-info -target x86_64-unknown-linux-gnu
{
  "compilerVersion": "Swift version 5.10.1 (swift-5.10.1-RELEASE)",
  "target": {
    "triple": "x86_64-unknown-linux-gnu",
    "unversionedTriple": "x86_64-unknown-linux-gnu",
    "moduleTriple": "x86_64-unknown-linux-gnu",
    "compatibilityLibraries": [ ],
    "librariesRequireRPath": false
  },
  "paths": {
    "runtimeLibraryPaths": [
      "/home/nak435/swift-5.10.1-RELEASE-ubuntu22.04/usr/lib/swift/linux"
    ],
    "runtimeLibraryImportPaths": [
      "/home/nak435/swift-5.10.1-RELEASE-ubuntu22.04/usr/lib/swift/linux",
      "/home/nak435/swift-5.10.1-RELEASE-ubuntu22.04/usr/lib/swift/linux/x86_64"
    ],
    "runtimeResourcePath": "/home/nak435/swift-5.10.1-RELEASE-ubuntu22.04/usr/lib/swift"
  }
}
3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?