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 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 --versionworks. 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
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):
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:
- Launch the REPL (just run
swiftin the terminal)
- Take the language tour to get a basic idea of how things work
- Read on the package manager: declaring and importing dependencies, building configurations…
- From there on start using the basic reference as needed.
I hope this helps!