Run Swift on Raspberry Pi

In this tutorial we will explore how to run Swift on Raspberry Pi.

In 2014 Apple announced a new programming language called Swift which was going to slowly replace Objective-C as the main language on iOS and Mac OS devices. The best thing with this announcement was that they were going to make it open-source which means that we can run Swift e.g. on a Raspberry Pi.

You can find more information about Swift here

There are two ways you can install swift on your raspberry pi; you can either compile the necessary libraries yourself or you can get pre-compiled libraries. We will use pre-compiled libraries which is compiled by Swift-arm.

Requirements

If you have not done this yet, check out these tutorials:
Getting started with Raspberry Pi
Setup Wifi on your Raspberry Pi without a monitor

Install Swift Libraries

In order to run swift applications on your Raspberry Pi you will need to install the swift libraries. Before we can install the libraries we need to tell the “apt-get” tool to look for the libraries in a specific repository. Otherwise we will need be able to find the Swift libraries. We do this with the following command:

# curl -s https://packagecloud.io/install/repositories/swift-arm/release/script.deb.sh | sudo bash

We can now install the Swift libraries with the command:

# sudo apt-get install swift5

Hello World application

Swift 5 is now installed and all you need to do now is to create a project and start coding! You can do this by running following commands:

One issue with this technique is that we are compiling everything on the Raspberry Pi. Which means that it takes quite a lot time longer compared to e.g. a desktop computer. The optimal would to have a development environment set up and compile the binaries towards the Raspberry Pi directly on a desktop computer. I will look into that and maybe have a follow up tutorial on that matter.

Where to go from here?

Just to have a Hello World application running on a Raspberry Pi isn’t so much fun. I suggest looking into the library SwiftyGPIO to make your Swift application capable to do more cool things.

Many thanks to https://www.freepik.com/flatart for the Swift Icon.