Swift Development On Linux: Part One

Introduction 

I’ve been a long-time Linux user. I love the flexibility and openness of the platform. So when I decided to take up iOS development my first question was “Will any of this stuff work on Linux?” 

At first the answer seemed to be ‘not really’ (no support for XCode, claims about Apple’s ecosystem being a walled garden, etc), but I was excited to hear about Apple’s decision to make Swift a cross-platform language. Indeed, frameworks like Vapor for server-side Swift were already coming onto the scene. 

But I didn’t want my work with Swift on Linux to be restricted to writing web backends, so I set out to learn more about building different types of software on Linux using Swift. 

This series of tutorials will detail some of the techniques and technologies I’ve learned along the way and, I hope, help you use Swift for creating amazing software solutions on Linux, in addition to its already potent capabilities in creating beautiful Apple apps. 

What We’ll Cover 

In this tutorial we will cover the basics of writing and executing Swift code on an Ubuntu 18.04 machine. This tutorial assumes a basic knowledge of working in Linux. For example, I assume you are familiar with extracting a downloaded tar file. 

By the end of this tutorial you will be able to create a swift project using Swift Package Manager, develop Swift code from Visual Studio Code, and even do basic syntax validation inside your IDE. 

Installing Swift on Ubuntu 

First you will need to install a few packages before starting with swift. Execute the following command at your terminal: 

sudo apt-get install libatomic1 clang 

You will also want to make sure you have git installed, as Swift Package Manager needs this to be able to resolve packages. 

Next download the Swift language itself by heading over to Swift’s download page: https://swift.org/download/

Download the latest Swift tar file and extract it into a directory of your choice. 

Next, add Swift’s command line tools to your path. To do this first determine the absolute path to the swift executable. cd into the extracted swift download and then cd into sub directory user/bin. Finally type pwd to get the current directory. This is the directory of the swift runtime. 

Now that you have this directory add it to your PATH. For example, to permanently add swift to your user’s bash sessions, edit the .bashrc file and add the line 

export PATH=$PATH:/path/to/swift/usr/bin 

To the end of the file. Note that after saving the file you will either need to start a new terminal session or else update the path in your current session by typing 

source ~/.bashrc 

Once swift is on your path you should validate the installation by typing 

swift -version 

You should get output that looks like this: 

Setting up Visual Studio Code 

Now that you have validated the installation let’s get your IDE configured. 

As I mentioned at the beginning of this tutorial, we’ll be using Visual Studio Code. If you haven’t done so already, please download and install the app. 

Next open VS Code. For my Swift development on Linux, I prefer the Swift Language and Sourcekit-LSP plugins. 

Let’s install the “Swift Language” plugin, by Martin Kase. Next, install Sourcekit-LSP.

Once you’ve installed Sourcekit-LSP you should configure it with the path to your language server protocol installation. First, open File -> Preferences -> Settings. Next open up the “Extensions” section. 

Click on the “SourceKit-LSP” item. 

Let’s fill in the Toolchain Path. The toolchain path will be the same as the path of the swift executable you configured earlier. So if swift is installed at /home/youruser/bin/swift then the toolchain path will be: 

/home/youruser/bin/swift/usr/bin

Writing your First Swift Program on Ubuntu 

Now let’s try writing our first Swift program.  Let’s start with a simple executable program. 

First choose or create a directory you would like to save your code in. For this example I’ll use ~/swiftCode

Go into that directory and create a new directory called helloWorld 

CD into helloWorld and then type 

swift package init —type executable 

Swift (more specifically, Swift Package Manager) has created a bunch of files for you, including a file called Package.swift.  For our project we’ll create a simple hello world but with a twist. Instead of printing hello world using Swift’s built-in print statement we’ll use Apple’s swift-log library. 

First let’s update the Package.swift file to include the swift-log dependency. Open up the file Package.swift. We won’t cover the syntax of Package.swift files in this part of the tutorial, but adding a new dependency is quite simple. 

Go to the “dependencies” section of the file and add a new .package() line (as in the example from the comment already provided for you): 

.package(url: “https://github.com/apple/swift-log.git”, from: “1.2.0”),

Next we need to make sure our project depends on the swift logging package. To do this go to the helloWorld target and add “Logging” as a dependency. 

You should now have something that looks like this: 

Now open the main.swift file under Sources. This file will be a simple hello world program. We’re going to modify it to print hello world using the logger type. 

First import the ‘Logging’ module. Then declare a logger. Finally, use the logger.info() method to print your message. Your code should look like this: 

import Logging
let logger = Logger.init(label: “HelloTest”) 
logger.info(“Hello, World!”)

Now from the terminal type: 

swift run

You should now see a logged message with the date/time and “Hello, World!” 

Auto-completion and API Browsing 

By running your program above you have triggered Swift Package Manager to resolve the dependencies (including their source code) of your project. Now that you’ve done this sourcekit-lsp will be able to help you autocomplete code and also view the sources of dependencies etc.. 

You will need to reload the window first, in order to get sourcekit-lsp to pick up the resolved dependencies. You can do this by opening the command pallet (SHIFT + CTRL+ P) and then typing reload window and selecting “Developer: Reload Window” from the drop down. 

You can now CTRL+Click on items such as the Logger class and view their source. Furthermore, hovering over items such as the Logger class will let you see their swift doc in a pop up. 

This concludes the first part of the tutorial. In the next tutorial we’ll be covering writing unit tests of your projects. 

See you then!

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like