Swift is now Open Source. Did anyone tried compiling Swift for a Raspberry PI? I started to do, but my 8 GB SD card seems to be too small for it ;) Is it possible to cross compile it from Ubuntu?
Since Raspberry Pi is limited in RAM and CPU power, using a language like Swift is perfect for maximizing the potential of the hardware.
A 8GB SD Card works ok, but you'll need to extend the root volume. I have it working and the used space on /dev/root
partition is around 3.1GB.
The steps below are based on the blog post by Andrew Madsen with a little extra focus on the steps inside fdisk
.
Download an image of Ubuntu 14.04 for Raspberry Pi 2 from finnie.org and copy it onto the SD card. Boot the Raspberry Pi.
Log into the Raspberry Pi and change the partition size. The default size for /dev/root
is 1.7G with 1.1G available. That is not enough.
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 1.7G 540M 1.1G 35% /
devtmpfs 458M 4.0K 458M 1% /dev
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 93M 228K 93M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 462M 0 462M 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/mmcblk0p1 64M 20M 45M 31% /boot/firmware
Run fdisk
sudo fdisk /dev/mmcblk0
At the prompt enter p
for 'print the partition table'. There are two partitions
/dev/mmcblk0p1 * 2048 133119 65536 c W95 FAT32 (LBA)
/dev/mmcblk0p2 133120 3670015 1768448 83 Linux
When prompted, enter d
(for delete), then 2
. Then, recreate the partition by entering n
, then p
, then 2
, then pressing enter at the next two prompts accepting the defaults.
Enter p
again and see the second partition is now bigger, now all space on an 8GB card is used.
Device Boot Start End Blocks Id System
/dev/mmcblk0p1 * 2048 133119 65536 c W95 FAT32 (LBA)
/dev/mmcblk0p2 133120 15523839 7695360 83 Linux
Enter w
to write the changes to disk, then reboot
sudo reboot
After the reboot, resize the partition’s file system by running
sudo resize2fs /dev/mmcblk0p2
Setup a swap file by doing
sudo apt-get install dphys-swapfile
sudo apt-get install libicu-dev clang-3.6
Use update-alternatives to provide /usr/bin links for clang and clang++:
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
Then, add @iachievedit’s repository key:
wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -
Add the appropriate repository to sources.list:
echo "deb [arch=armhf] http://iachievedit-repos.s3.amazonaws.com/ trusty main" | sudo tee --append /etc/apt/sources.list
Run apt-get update:
sudo apt-get update
sudo apt-get install swift-2.2
After the installation is complete, you’re ready to compile Swift programs!
Open your favorite text editor, write a program, and save it (e.g. to 'hello.swift’):
let device = "Raspberry Pi 2!"
print("Hello from Swift on \(device)")
swiftc hello.swift
and run it:
./hello
Hello from Swift on Raspberry Pi 2!
That’s it! Swift running on Raspberry Pi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With