My programming ramblings

Getting started with Swift 3 on Windows Subsystem for Linux

Posted on April 19, 2017 by Sol

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:

1 sudo apt install clang libicu-dev

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:

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:

edit

compile

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:

1 swiftc Hello.swift2 ./Hello

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.

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.