Getting started with Swift 3 on Windows Subsystem for Linux
Posted on April 19, 2017 by Paul
Recently, I’ve played a bit with Swift 3 on WSL (Windows Subsystem for Linux). In this article, I will show you how to install the latest stable version of Swift under WSL. Combining the open source Swift compiler with a text editor like Visual Studio Code is quite a pleasant coding experience, as you can see in the next screenshot:
I assume that you have a working and updated WSL installation on your Windows 10 machine. If this is not the case, check my previous article.
Next, install Clang which is required if you want to be able to use the Swift compiler:
You can download the official Swift binary for WSL, which uses an Ubuntu 16.04 image, from swift.org. Chose the latest stable release (currently this is version 3.1.1). If you see a new version of Swift, update the URL from the next instructions accordingly:
I recommend that you add Swift to your path:
Let’s check the version of the Swift compiler installed. This is what I see on my machine:
At the time of this writing, the Swift REPL swiftc doesn’t seem to work on WSL. You need to use the swiftc compiler. Basically, the typical workflow for a compiled language is how you will do Swift development on WSL:
- run the executable
Assuming that you have a file with some Swift code, named Hello.swift, here is how you will compile and run the code:
For editing Swift code, you can use any text editor you prefer. Personally, I found that Visual Studio Code works really well, has nice syntax highlighting and code suggestions. In order to enable Swift support in Visual Studio Code, install the Swift language extension.
Other advantage of using Visual Studio Code is that you can open a Terminal in the folder where your code is located and run commands. In order to open a Terminal in Visual Studio Code, press CTRL+' and write bash, as you will do in a normal Windows Command Prompt, to start WSL. You can see an example of using Visual Studio Code at the beginning of this article.
If you are interested to learn more about the latest Swift syntax, I would recommend reading Swift Programming: The Big Nerd Ranch Guide (2nd Edition) by M Mathias, J Gallagher:
If you want to learn more about the Linux command-line applications, I would recommend reading The Linux Command Line by William E.Shotts Jr.