A Complete Guide to Swift Development on Linux
In this tutorial you’ll discover everything you need to start developing Swift on Linux. You’ll learn about LLDB, using SourceKit-LSP, syntax highlighting and the power of autocomplete. By Jari Koopman.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
A Complete Guide to Swift Development on Linux
15 mins
Using Autocomplete
Time to write some code, and only some because your new LSP autocomplete will handle the rest :]
Add a new function called create
between the index
and delete
functions:
/// Saves a decoded `Todo` to the database.
func create(_ req: Request) throws -> Future<Todo> {
return try req.content.decode(Todo.self).flatMap { todo in
return todo.save(on: req)
}
}
This creates a new method which creates a new Todo
based on the information in the request.
Sure, you could copy-paste this code in. To fully enjoy your new autocomplete, however, try typing it yourself.
To register your new route, open Sources/App/routes.swift and add the following line between router.get
and router.delete
:
router.post("todos", use: todoController.create)
This registers the function you created as a POST endpoint on the API. To test it, run your program and execute the following curl command:
curl -X POST -H "Content-Type: application/json" -d '{"title": "Debug a swift app on Linux"}' http://localhost:8080/todos
This will respond with:
{
"id":1,
"title":"Debug a swift app on Linux"
}
If you see the following JSON
returned you know everything is working!
Debugging Swift on Linux
Being able to create, build and run your Swift apps on Linux is great. No doubt! But what if some nasty bug wiggles its way into your code? Well, then you’ll need to do some debugging.
It’s time to dive into how you can set up LLDB on Linux. You can use LLDB to debug in your terminal or in VSCode.
LLDB is the default debugger used by Xcode. It can be used to debug C, Objective-C, C++ and Swift programs. LLDB comes packaged with the Swift toolchain, meaning you already have it installed and ready to go.
From here on, it’s simple to set up some breakpoints. First, you’ll set up these breakpoints using the LLDB command line. Afterward, you’ll install a VSCode Extension to enable breakpoints inside VSCode.
Again in your Todos/Starter folder, build and run:
lldb .build/debug/Run
This command starts the LLDB session. Note that your program is not started yet. This way, you can set breakpoints beforehand.
In your LLDB session, set a breakpoint in your routes.swift on line 48 like this:
breakpoint set -f routes.swift -l 48
LLDB will tell you where it set the breakpoint.
Next, type r
to run your program. You should immediately hit the breakpoint you set. This allows you to inspect the surrounding variables and execution state.
Running the following:
po router
Will result in out similar to below:
<EngineRouter: 0x5555562a0e70>
You’ve printed the object description for your router. Awesome :]
To continue your application, simply type c
and press enter. You’ll see the normal project output telling you the migrations are complete and the server has started.
Debugging within VSCode
That’s is cool and all, but now you’ll add an extension that allows you to do all this from within VSCode. The extension is called CodeLLDB.
To install it, navigate to the VSCode extensions pane by pressing Control+Shift+X. Search for CodeLLDB and click install.
To configure VSCode to use LLDB as your Swift debugger, create a new folder in your project root called .vscode. Create two files inside this folder called launch.json and tasks.json.
Put the following contents in your launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb", // 1
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/.build/debug/Run", // 2
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "swift-build" // 3
}
]
}
This JSON tells VSCode the following:
- The type of process this is.
lldb
means VSCode will use CodeLLDB. - The program to use. This is the debug target you built.
- Pre-launch tasks to run before your main debug script is executed—in this case build your app.
In your launch.json, we tell VSCode to execute a swift-build
before launching the debug session. You’ll need to define this task. Open tasks.json and add the following JSON:
{
"version": "2.0.0",
"tasks": [
{
"label": "swift-build", // 1
"type": "shell", // 2
"command": "swift build" // 3
}
]
}
This task consists of three fields:
-
label
is the identifier from launch.json. -
type
is shell, meaning you’ll run a plain shell command. -
command
is the command you’ll run to ensure you don’t miss changes because you forgot to recompile.
Now open routes.swift in VSCod,e and click to the left of line 48. A red dot will appear indicating you placed a breakpoint. Press F5 to run your app. VSCode will first compile and then debug it.
You’ll hit your breakpoint again, but this time you can use the built-in VSCode debug navigator to continue.
From left to right, these buttons mean the following:
- Continue will continue the execution of the program until the next breakpoint is hit.
- Step over will move the debugger to the next line.
- Step in will go a level deeper — e.g., go into the function body if your breakpoint hits the invocation.
- Step out is the opposite of step in.
- Restart will re-run your debug session.
- Stop will stop the debug session.
Great work!
Where to Go From Here
You can download the final Swift project with VSCode setup using the Download Materials button at the top or bottom of this page.
To learn more about the SourceKit-LSP project or learn how to set it up with other editors, check out the GitHub repo. It’s open source :]
For more information on how to use LLDB, you can follow the LLDB Tutorial or explore CodeLLDB on GitHub.
And now that you’ve set up your Linux environment, you can continue learning with Getting Started with Server-Side Swift with Vapor.
If you have any comments or questions, please join the forums below!