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). If you see a new version of Swift, update the URL from the next instructions accordingly:
1 wget https://swift.org/builds/swift-3.1-release/ubuntu1604/swift-3.1-RELEASE/swift-3.1-RELEASE-ubuntu16.04.tar.gz 2 tar xzf swift-3.1-RELEASE-ubuntu16.04.tar.gz 3 sudo mv swift-3.1-RELEASE-ubuntu16.04 /usr/local 4 rm swift-3.1-RELEASE-ubuntu16.04.tar.gz
I recommend that you add Swift to your path:
1 cd ~ 2 echo 'export PATH=/usr/local/swift-3.1-RELEASE-ubuntu16.04/usr/bin:$PATH' >> .bashrc 3 source .bashrc
Let’s check the version of the Swift compiler installed. This is what I see on my machine:
1 ~ $ swiftc --version 2 Swift version 3.1 (swift-3.1-RELEASE) 3 Target: x86_64-unknown-linux-gnu 4 ~ $
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:
1 swiftc Hello.swift 2 ./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.
If you are interested to learn more about the Swift 3 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.comments powered by Disqus