Compile Swift for Raspberry Pi by Visual Studio Code

In order to keep this tutorial short, I assume that you have had Raspbian OS, Swift and Swish in place, otherwise please check out these following posts:
* Set up Raspbian Strech Lite OS
* Set up Swift 4.1 environment
* Setup Swish and compile source in XCode

Visual Studio Code is free, and available on Window, Mac and Linux. But this tutorial  limits for the guys who use Mac or Linux. Because I’m not sure if there are rsync, ssh on Window or not. If available, not sure they function as same as on Mac and Linux.

The mechansim which compiles swift sources for Raspberry Pi on Visual Studio Code is as same as XCode. Visual Studio Code will sync up source code to Raspberry Pi, then send compilation command over ssh to have them compiled on Raspberry Pi.

The benefit here is you take advantage of Visual Studio Code instead of using a boring command-line editor.

Visual Studio Code compiling for raspberry

Create project

Ok, let get started by create a new project named “Raspberry_Pi_In_Swift” by these following commands:

Create a new folder

mkdir Raspberry_Pi_In_Swift

Jump in the folder

cd Raspberry_Pi_In_Swift

Create a new project

swift package init --type executable

Create a project by swiftPM

After all, open the project in Visual Studio Code in menu File > Open or Cmd + O and choose the Raspberry_Pi_In_Swift folder.

You will see the Swift project opened like the following picture.


Config build task

In visual studio code, open menu item Terminal > Configure Tasks, then select “Create tasks.json from template” and choose “Others – example to run an arbitrary external command

This slideshow requires JavaScript.

Now the task is created, set a label for it. It can be anything but choose an appropriate label, for example “Remotely build on Raspberry Pi”.

In the command, put swish command in there with the raspberry pi’s user and ip address. Then save and close it.


In the menu, choose Terminal > Configure Default Build Task, and choose the task which you have created earlier.

Before building, make sure in Package.swift, swift-tools-version version must be appropriate to the swift on the Raspberry Pi. It should be 4.1 for Swift 4.1.x

Here We go. Choose Terminal > Run Build Task or Shift + Cmd + B to build the project. You will see the following console output.

Build swift project for raspberry Pi on xcode

On the Raspberry Pi end, the project is located at home/pi/swish/Raspberry_Pi_In_Swift

Jump in the project folder by

cd home/pi/swish/Raspberry_Pi_In_Swift

and run the compiled program

swift run --skip-build

What’s next

Visual Studio Code is free, simple and lightweight editor. It’s litterally friendly to beginner. However, in term of features, functionalities, XCode is many times better than Visual Studio Code.

The XCode has better IntelliSense for Swift. It also supports running and debugging Swift.

In order take advantages of XCode, I will share you some useful stragegies to help write code for Raspberry Pi which you can run and debug straight on MAC/XCode, so stay tuned

2 thoughts on “Compile Swift for Raspberry Pi by Visual Studio Code

  1. I blog often and I seriously thank you for your content.

    The article has really peaked my interest. I’m going to bookmark your site and keep checking for new information about once per week.
    I subscribed to your RSS feed too.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s