Swift 3.0 on Raspberry Pi 2 and 3

raspberry-pi-logo

Swift 3.0 Swift 3.0 Swift 3.0

There are a number of people working to bring Swift 3.0 to as many ARM-based systems as possible. This article is specifically about getting Swift 3.0 for your Raspberry Pi 2 or Raspberry Pi 3 that is running Ubuntu 16 (Xenial Xerus). It has not been tested for Raspbian variants (and likely doesn’t work).

A fair warning: support for Swift 3.0 on the Raspberry Pi (and all ARM devices) is still beta. Use it for prototyping and proof-on-concept work rather than building a product with it. Also, if you’re interested in joining the community of folks working on getting Swift 3.0 for ARM devices, come join us on Slack!

Xenial on a Raspberry Pi

You might not have even known Xenial for the Raspberry Pi existed; I didn’t! To grab it head over to the Ubuntu Wiki and get the distribution for your Pi. You will want to use at least an 8G SD card.

Install Swift 3.0

The team working on Swift for ARM is using Jenkins to build the Raspberry Pi binaries natively on a Raspberry Pi 3. As in, this is the build machine!

Swift 3.0 Build Machine
Swift 3.0 Build Machine

For the curious check out the Jenkins build project. Quite frankly, I’m amazed that it only took 6 hours to compile Swift.

Now, on your Raspberry Pi, grab and extract the Swift 3.0 build artifact into a directory and set your PATH with something like:

Note: You aren’t tied to putting Swift 3.0 in $HOME, I typically use /opt/swift/swift-3.0.

Now, let’s give it a spin!

Create a file called helloWorld.swift:

You can use swift helloWorld.swift to execute the file like a script:

# swift helloWorld.swift
Hello, world!

Or, you can compile the file into an executable with swiftc if you have clang installed and configured properly:

# swiftc helloWorld.swift
# ./helloWorld
Hello world!

If swiftc failed with error: link command failed with exit code 127 there’s a good chance you don’t have clang installed and configured properly:

sudo apt-get update
sudo apt-get install -y libicu-dev
sudo apt-get install -y clang-3.6
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100

Let’s look at a few other tidbits:

Importing Glibc works!

swiftcat.swift:

Compile (swiftc swiftcat.swift) and run (swiftcat)!

Bridging to C routines

Linking against compiled object files works!

escapetext.c:

escapetext.h:

escapeswift.swift:

Compile and link everything together:

# clang -c escapetext.c
# swiftc -c escapeswift.swift -import-objc-header escapetext.h
# swiftc escapeswift.o escapetext.o -o escapeswift -lcurl

And run:

# ./escapeswift "foo > bar"
Escaped text:  foo%20%3E%20bar

Swift Package Manager

Unless you enjoy writing makefiles and build scripts (and trust me, some do), the Swift Package Manager is here to help manage software package dependencies. We’ll be writing more about the SwiftPM available in Swift 3.0, but are excited to provide a version that works on armv7 devices. Try this out:

# mkdir finalCountdown && cd finalCountdown
# swift package init --type executable
Creating executable package: finalCountdown
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/

Replace the contents of Sources/main.swift with

Now, run swift build to build your finalCountdown application:

# swift build
Compile Swift Module 'finalCountdown' (1 sources)
Linking .build/debug/finalCountdown
# .build/debug/finalCountdown
Entering thread
10...9...8...7...6...5...4...3...2...1...
Exiting thread
Done

moreswift

For a random smattering of Swift 3.0 applications that have been run on both x86 and armv7 systems, check out the moreswift swift-3.0 branch.

What Version Is This?

The current builds available are not tracking the Swift 3.0 preview builds. To determine the Git hashes associated with the swift binary, type swift --version:

# swift --version
Swift version 3.0-dev (LLVM eb140647a6, Clang a9f2183da4, Swift bb43874ba1)

You can then, if you so choose go directly to the Swift repository commit with something like https://github.com/apple/swift/tree/bb43874ba1 to see the history beginning with the last commit incorporated into the build.

Acknowledgements

A lot of people have been apart of the effort to bring Swift to Linux ARM devices. This list is just a few of those individuals. Please do visit their blogs; there is a wealth of information and learnings therein.

30 comments

  1. David GReply

    Hi,

    I followed your instructions and I get the following error at trying “Hello, world!”

    pi@RPI3B-1:~/swift-3.0 $ swift hellowWorld.swift 
    swift: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version 'GLIBCXX_3.4.21' not found (required by swift)
    swift: /lib/arm-linux-gnueabihf/libtinfo.so.5: no version information available (required by swift)
    

    I have tried the sudo apt-get install libstdc++6 and it said that I had the newest version.

    Not sure where to go from here. Any help would be appreciated.

    Thanks for the article and your work.

    Dave

    • Joe Post authorReply

      David: were you using Ubuntu Xenial or Raspbian Jessie (lsb_release -a will tell you)? The binary is only compatible with Ubuntu Xenial.

    • AleyRoboticsReply

      To get it work you need replace “jessie” by “stretch” at /etc/apt/sources.list file.
      then “sudo apt-get update”
      and the last step is: “sudo apt-get install gcc-5”

  2. David GReply

    Hi Joe,

    In my exuberance, I somehow totally missed that minor detail! That’s the problem.

    Thanks.

  3. javacomReply

    Thank you for porting swift 3.0 to Raspberry Pi. Swift PM works. However, it seems that the “Dispatch” module is missing in the package.

    • Joe Post authorReply

      That’s correct; as of June 21, 2016 libdispatch has not been delivered as a part of the Swift 3.0 release on Linux (it is present on OS X). This is irrespective of architecture. If you take a look at the libdispatch note on Swift.org there is a reference to the porting project on Github. It appears the Linux port of GCD is moving a little slower than the rest of the components.

      For what it’s worth myself and some other individuals are about to start looking more into this to see how we can help move GCD on Linux along.

    • Joe Post authorReply

      Matt, thanks for bringing that to our attention, we’ve been moving stuff around and the link was out of date. Try again!

  4. MichaelReply

    Great guide, thanks a lot!

    In the bridging example, before compiling, don’t forget to
    sudo apt-get install libcurl4-gnutls-dev

    For the package manager example: NSThread is now just called Thread

    • Joe Post authorReply

      You’re welcome Michael! Yes, the Great Renaming effort is in full swing for Swift and a number of APIs are changing. I expect it to continue to churn daily as they rush to meet the goals of releasing Swift 3.0 later this year.

  5. FabioReply

    I’m currently getting “use of unresolved identifier ‘Process'” trying to get process arguments, anyone has any idea?

    • Joe Post authorReply

      Yes! Swift 3 has changed this to CommandLine. I need to update the tutorials to reflect this!

  6. AleyRoboticsReply

    Does somebody use OperationQueue?
    When i add Operation object declaration to code i get linker error

    How to fix it?

  7. AleyRoboticsReply

    to build libdispath try

    it will install swiftCore missing libs

  8. Eugenio BaglieriReply

    Is it possible download swift3 stable release for raspberry pi 2?

  9. AndrewReply

    Tried to build swift on my own, failed after several hours. Followed your instructions, had swift running in minutes. Thanks very much!

  10. Scott James RemnantReply

    lldb and the Swift REPL seem to fail with Illegal Instruction with the current build/current Xenial

  11. AleyRoboticsReply

    swift package init –type executable
    /usr/bin/swift-package: error while loading shared libraries: libicuuc.so.55: cannot open shared object file: No such file or directory
    Where to get libicuuc.so.55 file?

Leave a Reply

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