Swift on Windows — Part 1 (Native)

Shalini
VMware 360
Published in
3 min readJan 20, 2023

This tutorial series is to help you run your Swift code on Windows.

Swift on Windows

In this tutorial we will :

  • Understand the purpose of Swift on Windows
  • Install prerequisite software on Windows
  • Install Swift on Windows
  • Install WSL on windows

Why?

While Swift-based projects are usually built and run on Mac machines, it can also be helpful to install Swift on Windows.

Infrastructure as Code (IaC) can be written in any language. In our case, our solution is written in Swift which manages and provisions our CI/CD configuration through code. This enables us to require peer reviews for any change to CI configuration before it is introduced in CI/CD process. CI/CD may run on any Apple, Linux, and Windows-based machine depending on the project requirement, which makes it important for us to provide support for all these platforms

Prerequisites

Suggested versions of Visual Studio, Python and Git have been 
refered from Swift's Official Documentation (link attached below).
Please check the correct version on Swift's official documentation
before you install them.
  1. Git

Using Windows Powershell in admin mode, run the following command to install Git.

winget install Git.Git

2. Python

Using Windows Powershell in admin mode, run the following command to install Python.

winget install Python.Python.3.9

If `No package found matching criteria` error is received, search available versions using command `winget search <app_name>` and install available version

In a new Windows command prompt (non-admin), run the following command to install Python libraries.

pip install six

3. VS Studio

Using Windows command prompt in normal mode, run the following command to download Microsoft Visual Studio.

curl -sOL https://aka.ms/vs/16/release/vs_community.exe

Then proceed with the following to install it.

start /w vs_community.exe --passive --wait --norestart --nocache ^

--installPath "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community" ^

--add Microsoft.VisualStudio.Component.Windows10SDK.19041 ^

--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64

Once Visual Studio has been installed, you may delete its installer by running the following command:

del /q vs_community.exe

After installing, open Microsoft Visual Studio Installer from Settings>Apps in Windows Machine. In Visual Studio Installer, click on the Modify tab and choose the Individual Components tab. You will then install the following components

  1. Windows 10 SDK(10.0.17763.0)
  2. Python 3 64-bit (3.7.8)
  3. MSVC v142 - VS 2019 C++ x64/x86 build tools (Latest)
  4. Git for Windows

Install Swift on Windows

Swift can be installed through the official installer directly or by using the Windows Package Manager. Notice that the Windows Package Manager release may be behind the official release.

  1. When using the official installer which can be downloaded from the latest package release, run the package installer and follow the steps.
  2. When using Windows Powershell in admin mode, run the following command:

winget install Swift.Toolchain

Swift ToolChain Installation Location

A Swift toolchain will be installed at %SystemDrive%\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain.

A compatible Swift SDK will be installed at %SystemDrive%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk.

Once installed, you can run

swift --version

to validate Swift installation on the machine. Now your Windows machine is ready to compile and run your swift libraries.

Additionally, you may need to run Linux-style commands on Windows. You can install WSL(Windows Subsystem for Linux).

WSL

Using the Windows command prompt in normal mode, run the following command to view a list of available WSL distros.

wsl -l -o

List Linux Distribution

Using Windows command prompt in normal mode, run the following command to install the WSL with a specific distro (Ubuntu in my case).

wsl --install -d <DISTRO-NAME>

For e.g. : wsl --install -d Ubuntu

  1. Restart your computer to finish the WSL installation on Windows.
  2. Search for the installed Linux Distribution on the system and double-click to launch it.
Linux Distribution in Windows Machine

3. Configure username and password and it's ready to use.

In Part 2, we will run Swift on Windows Docker Container.

--

--