Zero to Project: Swift on Linux

As I’m doing Advent of Code 2020, I wanted to try out server-side Swift on Ubuntu, knowing it would be limited without XCode. This turned out to take many searches to get right.

Visual Studio Code is the best option for a pseudo-IDE since IntelliJ AppCode is still limited ot Mac OSX only.

Installing

  • Installing Swift is easy enough: install the listed dependencies and download a release here, untar wherever you want and add it to PATH through /etc/environment (or put it in /usr/bin) and make sure swift --version works. For reference I get 5.3.1 as of writing this.

  • In VSCode, install this extension (the only one with some level of maintaining)

  • For autocompletion, the Swift toolchain also comes with its language server under swift-5.3.1-RELEASE-ubuntu18.04/usr/bin/sourcekit-lsp. In VSCode, set

    1
    2
    "sde.languageServerMode": "sourcekit-lsp",
    "sourcekit-lsp.serverPath": "[path-to-swift]/usr/bin/sourcekit-lsp"

    If you’re using the settings UI, these are under Extensions > Swift Development Environment Configuration.

    Note that while autocompletion is excellent, jumping to documentation or implementation is unavailable.

  • To set up debugging with LLDB, follow this article. It’s fairly limited however, as you currently cannot see variables, but breakpoints and step-by-step debugging work just fine.

  • SwiftLint is available here but difficult to setup on Linux.

Creating a project

Basic setup (longer version here):

1
2
3
4
5
6
7
mkdir myproject
cd myproject
swift package init --type executable
swift build
swift test
swift run myproject
.build/x86_64-unknown-linux-gnu/debug/swift #prints "Hello, world!"

My resulting project is here. This includes some unit tests so it makes a good starter project as well.

I’m new to Swift, what’s next?

Now you can:

I hope this helps!