LoginSignup
0
1
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

Windows11 に Swift をインストールする“やさしい”手順(Docker編)

Last updated at Posted at 2024-06-24

↑先日、こちらの記事で、Windows11にWindows向けのSwiftをインストールする手順をまとめましたが、数日調べてもswift hello.swiftがエラーになる原因が分からず、断念した。

原因不明のエラー
> swift hello.swift
<unknown>:0: error: could not load the swift standard library

その代わりに、今回は、Swift公式Docker Imageを使った手順をまとめます。
こちらは完璧に動作し、Windowsネイティブよりビルドも早い気がします。

1. WSL(Ubuntu 22.04.3)インストール

↑公式サイトに書いてある通り、管理者モードのターミナルから、次のコマンドを実行する。

> wsl --install

この1コマンドで、仮想マシン機能の有効化も含めて、Ubuntu がインストールされます。

2. Docker Desktop for Windows インストール

↑こちらの案内に従い、Docker Hub から Docker Desktop Installer.exe をダウンロード&インストールしてください。

インストール後、Docker Desktop を起動し、画面左下のEngine runningを確認する。

engine.png

3. Swift Docker 環境構築

↓公式サイトに書いてある手順そのものです。

以降のDockerのコマンドは、すべて Ubuntuのターミナルから実行します。
(Windowsターミナルを起動し、Ubuntu を選択)

3-1 Swift Docker Image取得

docker pull swiftコマンドでイメージを取得
(自分のPCで20分ほどかかった)

$ docker pull swift

Using default tag: latest
latest: Pulling from library/swift
2ec76a50fe7c: Pull complete
031194576cad: Pull complete
7f110500e829: Pull complete
7ddf3aa876b7: Pull complete
Digest: sha256:ded3bdf39b1eb72198a9e0fb6102e98b9c30467fa9e70d5f7d17b4e1d324d29d
Status: Downloaded newer image for swift:latest
docker.io/library/swift:latest

$ docker image ls
REPOSITORY       TAG       IMAGE ID       CREATED       SIZE
swift            latest    dea8a12cbbc1   2 weeks ago   2.63GB

3-2 コンテナの作成と起動

次のdocker runコマンドでコンテナを作成し起動する

$ docker run -it --name swiftfun swift /bin/bash

root@1d4ed170e8f5:/# swift --version
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: x86_64-unknown-linux-gnu

root@1d4ed170e8f5:/# cat >hello.swift
print("Hello Swift!")
^D

root@1d4ed170e8f5:/# cat hello.swift
print("Hello Swift!")

root@1d4ed170e8f5:/# swift hello.swift
Hello Swift!

↑ちゃんと動きました。

↓もちろん、Replも動きます。

root@1d4ed170e8f5:/# swift repl
Welcome to Swift version 5.10.1 (swift-5.10.1-RELEASE).
Type :help for assistance.
  1> let a = 10 + 5
a: Int = 15
  2> print(a)
15
  3> print("Hello!")
Hello!
  4> :quit

3-3 コンテナ停止

exit か Ctrl+D でシェルから抜ける

root@1d4ed170e8f5:/# exit

$ docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS                       PORTS     NAMES
abeb104c0a6d   swift     "/bin/bash"   About an hour ago   Exited (137) 2 minutes ago             swiftfun

3-3 コンテナ(シェル)を再び起動

$ docker start -i swiftfun

root@ad90c6dae1f4:/# mkdir tmp/pj1
root@ad90c6dae1f4:/# cd tmp/pj1
root@ad90c6dae1f4:/tmp/pj1# swift package init --type executable
Creating executable package: pj1
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
root@ad90c6dae1f4:/tmp/pj1# swift run
Building for debugging...
[8/8] Linking pj1
Build complete! (1.93s) ⭐️早い
Hello, world!
root@ad90c6dae1f4:/tmp/pj1#

VSCodeからSwift Dockerを使う方法は、別の記事でまとめます。

2024.6.25追記

0
1
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
0
1