LoginSignup
9

More than 5 years have passed since last update.

Ubuntu 16.04にSwift4をインストールしてVaporでハローワールドするまで #love_swift

Last updated at Posted at 2018-01-20

Swift愛好会合宿 2018/1/20-1/21で記事書いてます。 #love_swift

筆者スペック

  • 普段はNode.jsを書いている
  • SwiftはServer Side Swiftを1年くらい前にさわった程度です

ということでSwift書いてるというより環境構築復習くらいですがメモしておきます。

環境

  • Ubuntu 16.04
  • Swift 4.0.3
  • Vapor 2.4.0

現在の最新バージョンを確認

どこの言語でもそうですけど最新バージョンしりたいですよね。

2018年1月20日現在の最新は 4.0.3らしい。

Ubuntuにインストールする

Ubuntuのバージョンを確認

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"

16.04でした。

ツールのインストール

Ubuntu16.04 に Swift4 をインストールして、swift package を簡単に使ってみるを参考にインストール

$ sudo apt-get install clang libpython2.7 libxml2

Swiftのインストール

4.0.3をDL

$ wget https://swift.org/builds/swift-4.0.3-release/ubuntu1604/swift-4.0.3-RELEASE/swift-4.0.3-RELEASE-ubuntu16.04.tar.gz
$ tar zxfv swift-4.0.3-RELEASE-ubuntu16.04.tar.gz
$ sudo mv swift-4.0.3-RELEASE-ubuntu16.04 /usr/local/swift

パスを通す。

確認

$ swift --version
Swift version 4.0.3 (swift-4.0.3-RELEASE)
Target: x86_64-unknown-linux-gnu

無事に

Swift Packageでエラー

$ swift package init --type executable
/usr/local/swift/usr/bin/swift-package: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

Electron apps can't find libcurl.so.4で解決しました。

$ sudo apt-get install curl libcurl3

改めて Swift Package

myswiftというフォルダを作成して作業します。

$ mkdir myswift
$ cd myswift
$ swift package init --type executable
Creating executable package: myswift
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/myswift/main.swift
Creating Tests/

これでHello World的なプロジェクトができます。

ビルドしてみます。

$ swift build
Compile Swift Module 'myswift' (1 sources)
Linking ./.build/x86_64-unknown-linux/debug/myswift

./.build/x86_64-unknown-linux/debug/myswiftという実行ファイルができました。

実行

$ ./.build/x86_64-unknown-linux/debug/myswift
Hello, world!

ここまでそこまで詰まらずにいけました。

さて...

VaporでWebサーバーを立ててみる

Webフレームワーク何個かあるけど、なんとなく使ってみます。

$ mkdir webapp
$ cd webapp
$ swift package init --type executable

Manual Quickstartを参考に進めます。

Sources/webapp/main.swiftを編集

main.swift

import Vapor

let drop = try Droplet()

drop.get("hello") { req in
  return "Hello Vapor"
}

try drop.run()

Package.swiftを編集。

Package.swift
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "webapp",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/vapor/vapor.git", from: "2.4.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "webapp",
            dependencies: ["Vapor"]),
    ]
)

ビルド

$ swift build

結構時間かかります。

Compile CHTTP http_parser.c
Compile Swift Module 'libc' (1 sources)
Compile Swift Module 'Debugging' (1 sources)
Compile Swift Module 'Bits' (19 sources)
Compile Swift Module 'PathIndexable' (2 sources)
Compile Swift Module 'Core' (23 sources)
Compile Swift Module 'Transport' (10 sources)
Compile Swift Module 'Random' (6 sources)
Compile Swift Module 'Node' (38 sources)
Compile Swift Module 'Console' (36 sources)
Compile Swift Module 'URI' (6 sources)
Compile Swift Module 'Sockets' (22 sources)
Compile Swift Module 'Crypto' (13 sources)
Compile Swift Module 'BCrypt' (9 sources)
Compile Swift Module 'Cache' (2 sources)
Compile Swift Module 'Branches' (3 sources)
Compile Swift Module 'JSON' (8 sources)
Compile Swift Module 'TLS' (12 sources)
Compile Swift Module 'SMTP' (21 sources)
Compile Swift Module 'Configs' (13 sources)
Compile Swift Module 'HTTP' (45 sources)
Compile Swift Module 'WebSockets' (14 sources)
Compile Swift Module 'Cookies' (11 sources)
Compile Swift Module 'Multipart' (6 sources)
Compile Swift Module 'Routing' (10 sources)
Compile Swift Module 'Sessions' (7 sources)
Compile Swift Module 'FormData' (4 sources)
Compile Swift Module 'Vapor' (100 sources)
Compile Swift Module 'webapp' (1 sources)
Linking ./.build/x86_64-unknown-linux/debug/webapp

WebSocketとかSessionとか色々と落として来てますね。

無事にビルドできました。実行します。

$ ./.build/x86_64-unknown-linux/debug/webapp

Could not load config files from: /home/n0bisuke/myswift/webapp/Config/
Try using the configDir flag
ex: .build/debug/Run --configDir=/absolute/path/to/configs
The default hash should be replaced before using in production.
The default cipher should be replaced before using in production.
No command supplied, defaulting to serve...
Starting server on 0.0.0.0:8080

これでサーバーが起動します。

:8080/helloにアクセスします。

これでWebサーバーが立てられました。

もうちょい色々試してみたかったけど一旦ここまでで...

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
9