Learn how to install Swift 3.0 on your Raspberry Pi. Create and run your first Swift Project “Hello Swifty World!”
Swift is now Open Source!
What does this mean? It means that now thanks to the hard work of many people, we can now use Swift on many different platforms and devices. From Mac OSX machines to Linux Servers and many other SBC and IOT devices including Raspberry Pi's, BeagleBones and Chips.
Swift 3.0 on the Raspberry Pi.
In this project we will learn how to install Swift 3.0 on the Raspberry Pi. The version of Swift we will be using has been built to be compatible with all versions of the Raspberry Pi running the latest Raspbian “Jessie” operating system and is called Swift-Lite. Swift-Lite is a full version of Swift 3.0 but does not include the “Package Manger”, “REPL” or “LLDB”. Swift-Lite includes a special build pre-processor called “swift-lite-build” to allow the easy building of multi-file swift projects. For more info go to www.swift-lite.org.
Step One – Prepare the Raspberry Pi for Swift.
Before we install Swift we need to prepare our Raspberry Pi and install some dependancies.
NOTE: It is always a good idea to make a backup of your SD card before starting a new project.
- Open a new Terminal Window.
- Check you have the latest Raspian Jessie installed.
- (see SPECIAL NOTE if you have Raspbian Stretch installed)
- Run update
sudo apt-get update
- Swift requires the installation “Clang”. Clang is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, OpenMP, OpenCL, and CUDA. It uses LLVM as its back end.
- Install clang.
sudo apt-get install clang
Step Two – Install Swift 3.0
- Download the Swift install file.
- Unzip (tar) Swift.
sudo tar -xzpf swift-lite-3.0-raspberrypi-universal.tar.gz -C/
- Remove install file.
$ rm swift-lite-3.0-raspberrypi-universal.tar.gz
Thats it Swift 3.0 is installed!
Hello Swifty World – Your first project.
- First lets create a directory for our Swift projects.
- Change to the new directory.
- Create a new .swift project file.
- This will open the text editor so we can add some code. You can use any plain text editor to create and edit swift code.
- Add the following code – print(“Hello, swifty world!”)
GNU nano 2.2.6 File: helloworld.swift print("Hello, swifty world!")
- control – x to close the text editor. Answer yes to save the file.
- To build the swift project we will use “swift-lite-build” followed by the project file name.
- “swift-lite-build” will build the swift project and create an executable file with a .swapp extension. In the terminal we should see the following result.
swift-lite-build helloworld.swift Processing Files Building! Build Finished
- List the directory to see the new build file.
ls helloworld.swapp helloworld.swift
- To run your Swift swapp enter the following command, press enter and we should see “Hello, swifty world! “.
./helloworld.swapp Hello, swifty world!
Congratulations you have just created, built and run your first Swift project on a Raspberry Pi!
SPECIAL NOTE FOR RASPBIAN STRETCH
If you have installed Raspbian Stretch you may get the following error.
swift-lite-build: error while loading shared libraries: libicuuc.so.52: cannot open shared object file: No such file or directory
This is because Swift is looking for libicuuc.so.52 and Raspbian Stretch only includes libicuuc.so.57.
To fix this we need to install libicuuc.so.52 by following these instructions.
Step 1 – download the package.
Step 2 – unpack the package.
sudo dpkg -i libicu52_52.1-8_armhf.deb
Step 3 – Cleanup by removing package.
rm -rf libicu52_52.1-8_armhf.deb
That should fix the error.
In the next tutorial we will expand helloworld to include a Swift Module file and start using “Foundation”.