Setting up a Swift development environment on Raspberry Pi by@piotr.gorzelany

Setting up a Swift development environment on Raspberry Pi

Piotr Gorzelany Hacker Noon profile picture

Piotr Gorzelany

Senior iOS Developer

Image taken from

In my previous post, I gave an overview of the current state of Swift on the Raspberry Pi platform. In this post, I will try highlight how to set up a pleasant development environment to write Swift code on the Pi.

One of the biggest pains right now when writing Swift code on any Linux platform is the lack of a real IDE. Sure, you may hate Xcode but go start typing your program in Nano on the command line and you will see how real misery feels.

You get a lot of these since you do not have compiler warnings at write time

The first step for a good development experience is a good IDE and the best Swift IDE for Linux right now is…. Xcode.

What!? But Xcode does not work on Linux. How do I install it on the Pi? Well, you don’t, and frankly, you would not want to write code on the Pi. It is a slow computer and running it with a monitor and keyboard is unnecessary. So where do I write my code? On your Mac of course, where else dummy.

The first thing you need to do is setup a headless Pi. There is a really nice tutorial on how to do that on the Raspberry forums. The tutorial is written for Raspian and I recommend using Ubuntu for the Pi since the SPM works there. I first ran my Pi with a keyboard and monitor and did the initial setup and connected it to my WiFi network. Once you have the Pi connected to your router you can start controlling it by SSH from your mac. First, you need to find the IP address of your Pi on the network. I do that using nmap which scans devices in your network that listed on port 22 (ssh port).

To find your Pi, you need to replace the part with your local address space. Once you have it, connect your mac through ssh.

ssh {your-pi-username}@{you-pi-ip-address}

And there you go, the sky is the limit now. Try to build and run your first Swift script remotely on the Pi.

// Create the script
echo "print(\"hello from swift\")" >> hello.swift
// Run it
swift hello.swift

This is all fine and dandy but what are you still doing on the command line? Go fire up Xcode and begin some serious development!

We will create a program that will switch on and off an LED connected to the Pi if you press the return key on your Mac. To switch nicely between the two environments we will use the Swift Package Manager, which is a system-agnostic dependency manager for Swift. It runs fine both on OS X and on Linux and it will be a vital part that will enable us to develop code on the Mac and run it on the Pi.

// create the project folder
mkdir led-blink
// go to the folder
cd led-blink
// initialize the swift project using SPM
swift package init --type executable

You should now have the directory structure and main.swift file generated by SPM. Edit the Package.swift to include the GPIO swift library.

import PackageDescription
let package = Package(
name: "led-blink",
dependencies: [
.Package(url: "", majorVersion: 0)

Now fetch the library using the command:

swift package update

You can now use SPM to generate an Xcode project for you! Do this using the command:

swift package generate-xcodeproj

Open the generated Xcode project and build it (cmd+B), so the compiler knows about the SwiftyGPIO library. Voila, you can now use Xcode to develop your program, having access to all those sweet tools like code completion, syntax highlighting, compiler errors, documentation and more.

Open the main.swift file and add the following code:

import SwiftyGPIO
import Foundation
// Get a dictionary of all the gpio pins
let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2)
// Get the pin that you connected the LED to. Remember to set the right pin number, for me it was 27
guard let ledGpio = gpios[GPIOName.P27] else {
fatalError("Could not initialize the gpio")
// Set the pin direction to .OUT and turn it off
ledGpio.direction = .OUT
ledGpio.value = 0
// Read user input from keyboard and switch the LED on/off each time the user presses the return key
print("Press return to switch the LED on/off. To quit type exit")
while let userInput = readLine(strippingNewline: true), userInput != "exit" {
print("Switching LED")
ledGpio.value = ledGpio.value == 0 ? 1 : 0

Ok, the program is completed. The only thing left to do is to build and run it on the Pi. To do this, you will need to copy the project you created on your mac to the Pi. There are a couple of solutions how you might do it. For one, you could create a GIT repository and host it on Github. Then you would push the changes from your mac, and pull them on the Pi. Another option is to use SCP to copy the folder.

scp -r {path-to-project-folder-on-mac} {your-pi-username}@{you-pi-ip-address}:{path-to-folder-on-pi}

Once you copied the project, you can ssh to the Pi and go to the project folder. The folder has all the files there but you still have to build it using the compiler on the Raspberry.

swift build

Should create an executable which you can then run and marvel at your work! Here is the masterpiece I’ve created in action.

Raspberry Pi controlled by a MacBook Pro


Developing for the Pi is not that painful once you can use your familiar environment on the Mac. Using this technique you can develop your programs faster using Xcode.

The workflow looks roughly like this: Write code on Mac -> Copy to Pi -> Build and run on Pi

This process can easily be completely automated but I just didn’t have time to do it.

Using this setup I plan to use Xcode to develop a server application, run it on the Pi, and control LEDs with my iPhone. Fingers crossed.


Join Hacker Noon

Create your free account to unlock your custom reading experience.