How to Install Swift on Elementary OS

Swift is a new programming language developed by Apple to build iOS, OS X, watch OS, and tvOS applications. Its goal is to replace the current Objective-C language in future, not for now or in near few years. Apple Inc. has made it open-source so community around the world can help to make it available everywhere. At the time of this writing, it is only available for Apple Platform and Linux (Ubuntu 14.04 and Ubuntu 15.10 64-bit). Windows and other Linux distributions such as RedHat, CentOS, OpenSUSE will be supported later. This article will guide you how to install Swift on Elementary OS Freya 0.3.2 or previous release which is based-on Ubuntu 14.04.

Download Swift Programming Language package

You can download the latest release of Swift directly from https://swift.org/download/ Select the platform based-on your Linux. Elementary OS Freya users should choose the Ubuntu 14.04 version.

Install required dependency packages

Swift requires libicu-dev (Development files for International Components for Unicode package) and clang (a compiler that can parse and analyze source codes in C language family (such as C, C++, and Objective-C)). Let’s install both using apt-get tool.

$ sudo apt-get install clang libicu-dev

Import PGP keys if Swift package downloaded for the first time.

You can import the PGP keys as follows:

$ wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
gpg: directory `/home/computechtips/.gnupg' created
gpg: new configuration file `/home/computechtips/.gnupg/gpg.conf' created
gpg: WARNING: options in `/home/computechtips/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/home/computechtips/.gnupg/secring.gpg' created
gpg: keyring `/home/computechtips/.gnupg/pubring.gpg' created
gpg: /home/computechtips/.gnupg/trustdb.gpg: trustdb created
gpg: key 412B37AD: public key "Swift Automatic Signing Key #1 <swift-infrastructure@swift.org>" imported
gpg: key 21A56D5F: public key "Swift 2.2 Release Signing Key <swift-infrastructure@swift.org>" imported
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)

Extract / Install Swift package and set the PATH

Make sure you are in the correct working directory. For example I save the file in my $HOME folder (/home/computechtips). Change accordingly to meet with yours.

$ tar zxf swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04.tar.gz
$ mv swift-2.2-SNAPSHOT-2015-12-18-a-ubuntu14.04 swift
$ export PATH=$PATH:/home/computechtips/swift/usr/bin

Type swift to start playing with Swift programming language

$ swift
Welcome to Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 0ddf238ad7). Type :help for assistance.
1> let message = "Hello Swift on Elementary OS";
message: String = "Hello Swift on Elementary OS"
2> print(message)
Hello Swift on Elementary OS
3> func Calc() {
4.     let height = 6;
5.     let width = 7;
6.     let area = height * width;
7.
8.     print("Area is (area)")
9. }
10>
11> Calc()
Area is 42
12> :q

Below is the screenshot of Swift running on Elementary OS.

Swift Programming Language on Elementary OS

Hopefully you can find this brief post helpful.