Writing and Running Swift Code without Xcode on Mac and Windows

Kurt Frey - Dec 31 '23 - - Dev Community

Swift has quickly become my favourite programming language. Its sleek syntax and fail safe features (guard and if let) are beautiful. My first steps with Swift were a few years back when I made my first iOS app. I knew that Objective-C was too different from what I knew at that time and I heard good things about Swift's learning curve.

But now I wanted something more light-weight than Xcode for quick, un-sandboxed scripts. And so I looked into Visual Studio Code and Swift.

Turns out, writing a command line swift App can easily be done on macOS, Linux and Windows with VS Code, some extensions and just a few dependencies. Follow along to see how it's done.

macOS

Firstly, install Xcode or the Xcode command line tools. I recommend installing the package manager brew (homebrew). It installs Xcode command line tools for you and comes in handy when you install software that's not in the App Store (and even if it is!).

Now, install Visual Studio Code (if you haven't already) and the executable SwiftLint (which is used by the extension we'll install in a second) with i.e. brew install visual-studio-code swiftlint.

In VS Code, install the following extensions (via GUI or command line):

  1. Swift
  2. CodeLLDB
  3. SwiftLint
  4. Swift Project Creation

After that open a folder, and create a new swift project in VS Code. That's easy, because you installed the Swift Project Creation extension. In the Sources directory, there's a main.swift. Happy Coding. Once your code is ready to run, run it with a touch on the play button (or the play button with the critter for debugging) in your Touch Bar or with Run > Start Debugging (F5).

Windows

On Windows, things are a bit more involved but still straight forward once you know the culprits.

First, you'll need Visual Studio Community (at the time of writing this blogpost, Visual Studio 2022 is the most recent version). Get it via winget install --id=Microsoft.VisualStudio.2022.Community -e , or directly via Visual Studio Community if you don't have winget installed. winget (winget) is a package manager for Windows like chocolatey and scoop. They have been compared numerous times.

Don't worry, you'll still be using Visual Studio Code but Visual Studio must be installed!

If you hadn't have the chance to install the "Desktop Development with C++" component with the IDE itself, install it in Visual Studio Community, install as described here.

Next, install Swift for Windows. Also, you'll need Python 3.9 for the Swift Toolchain to work. LLDB is very picky about the Python version. Install both by using winget install Swift.Toolchain Python.Python.3.9.

Lastly, you'll need the following Visual Studio Code extensions (via GUI or command line):

  1. Swift
  2. CodeLLDB
  3. Swift Project Creation

Note: SwiftLint - the executable required by the extension - is not available on Windows. So we omit the extension as well.

In Windows, you have to check Swift > Debugger: Use Debug Adapter From Toolchain in the Settings of the Swift VS Code Extension.

After that open a folder, and create a new Swift project in VS Code. That's easy, because you installed the Swift Project Creation extension. In the Sources directory, there's a main.swift. Happy Coding. Once your code is ready to run, run it with Run > Start Debugging (F5).

Linux

Help me, make this blog post complete: What are your instructions for making Swift and VS Code work on Linux?

Target Platform

For targeting the most recent macOS you have to add or change the code in Package.swift to include this snippet:

platforms: [
    .macOS(.v14)
],
Enter fullscreen mode Exit fullscreen mode

Otherwise there will be errors if you use a language feature that is not available in the macOS version set in Package.swift.

Remarks

Swift Versions on macOS

One disadvantage of Swift is how Apple decided to deploy new versions. They only come bundled with new versions of their operating systems. However, with swiftenv (kylef/swiftenv) other versions than the bundled one can be installed. So maybe that adds a possibility to run more modern swift versions on older hardware?

VS Code Sync

VS Code can sync settings and installed extensions. If you're running this setup on both, a macOS and a Windows machine, you'll run into troubles:

  1. CodeLLDB has a setting that points to the lldb library. Don't sync this setting.
  2. SwiftLint is not available on Windows. Don't sync this extension.
. . . . . . . . . .