Swift on the Windows Subsystem for Linux

Not long ago Swift was open sourced and made available on Linux, opening up the possibility for server-side Swift. I've always thought Swift was a beautiful language (it looks a lot like Rust), but I never went any further than that thought. I didn't think server-side Swift would be a marketable skill so I never bothered with a test drive. Server-side Swift still probably isn't a marketable skill, but I couldn't help myself, I wanted to see if I could get it working on Windows via WSL and try it out.

The download process isn't as simple as the usual apt-get install <xyz> but luckily the Swift maintainers didn't make it too difficult.

First, lets update:
sudo apt-get update

Then install our dependencies. The download instructions on Swift.org tell us to use the sudo apt-get install clang libicu-dev command, indicating that all we need is clang. But my first attempt failed because I needed libpython2.7 in addition to clang. To install both at the same time, use the following command:
sudo apt-get install clang libicu-dev libpython2.7

Next, download the Swift binary for Ubuntu (as of this writing, the only Linux flavor officially supported is Ubuntu, so if you have one of the other flavor's you may need to take extra steps or get Ubuntu from the Windows Store). We can check our version of Ubuntu with the following command:
lsb_release -a
I'm on 16.04 so that's the version of the download I'll use.

The downloads for the Swift binary and signature are available at Swift.org. You can download them manually by clicking on the links or by using the wget command to download straight to your current directory. I find wget easier, so I copied the link address for the download and pasted it into the console.

Swift binary download:
wget https://swift.org/builds/swift-4.1.3-release/ubuntu1604/swift-4.1.3-RELEASE/swift-4.1.3-RELEASE-ubuntu16.04.tar.gz
Signature download:
wget https://swift.org/builds/swift-4.1.3-release/ubuntu1604/swift-4.1.3-RELEASE/swift-4.1.3-RELEASE-ubuntu16.04.tar.gz.sig

To make sure our binary is intact and unaltered we will import Swift's pgp keys and verify the pgp signature.

Import the PGP keys:
gpg --keyserver hkp://pool.sks-keyservers.net \ --recv-keys \ '7463 A81A 4B2E EA1B 551F FBCF D441 C977 412B 37AD' \ '1BE1 E29A 084C B305 F397 D62A 9F59 7F4D 21A5 6D5F' \ 'A3BA FD35 56A5 9079 C068 94BD 63BC 1CFE 91D3 06C6' \ '5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235' \ '8513 444E 2DA3 6B7C 1659 AF4D 7638 F1FB 2B2B 08C4'

We should get something like the following as the output:

gpg: /home/GHvW/.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: key 91D306C6: public key "Swift 3.x Release Signing Key <swift-infrastructure@swift.org>" imported
gpg: key 71E1B235: public key "Swift 4.x Release Signing Key <swift-infrastructure@swift.org>" imported
gpg: key 2B2B08C4: public key "Swift Automatic Signing Key #2 <swift-infrastructure@swift.org>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 5
gpg:               imported: 5  (RSA: 5)

Refresh the keys:
gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys Swift

Verify the archive is intact:
gpg --verify swift-<VERSION>-<PLATFORM>.tar.gz.sig
On Ubuntu 16.04, downloading Swift 4.1.3, this command will be:
gpg --verify swift-4.1.3-RELEASE-ubuntu16.04.tar.gz.sig

The output should look something like this:

gpg: assuming signed data in `swift-4.1.3-RELEASE-ubuntu16.04.tar.gz'
gpg: Signature made Fri 27 Jul 2018 09:01:24 PM DST using RSA key ID 71E1B235
gpg: Good signature from "Swift 4.x Release Signing Key <swift-infrastructure@swift.org>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 5E4D F843 FB06 5D7F 7E24  FBA2 EF54 30F0 71E1 B235

The warning here is fine according to the downloads page. If you get a different warning, you should re-download and re-verify the download.

Lastly, we'll extract the archive and add the Swift toolchain to our path:

Extract:
tar xzf swift-<VERSION>-<PLATFORM>.tar.gz
Using the same 16.04 Ubuntu and Swift 4.1.3 as before, the command is:
tar xzf swift-4.1.3-RELEASE-ubuntu16.04.tar.gz

Add the Swift toolchain to our Path:
export PATH=/path/to/usr/bin:"${PATH}" which will end up looking something like:
export PATH=/path/to/swift-4.1.3-RELEASE-ubuntu16.04/usr/bin:"${PATH}"
Then we'll add it to our ~/.bashrc file so that we can use the swift command across sessions.
To open and edit ~/.bashrc using nano, use the following command:
nano ~/.bashrc

Unfortunately one of the coolest features, the Swift REPL, doesn't work on WSL, and likely never will. You can read more about why here.

Despite the sad state of the REPL, we can still compile and run Swift programs. To test it out, create a .swift file (something like test.swift). Open the file and type an iconic print("Hello World!"). In the console, type swift test.swift, and you should see Hello World! printed to the console.

Apple's Swift language on the Windows Subsystem for Linux!

Update 8/13/2018: in order to use the Swift package manager I needed to download libcurl3. If you cant use the swift package command, you may need to download it as well.
sudo apt-get update
sudo apt-get install libcurl3

Reference:
Swift.org, Install Swift on Digital Ocean, No REPL support in WSL,