Writing and Running Swift Code without Xcode on Mac and Windows
Tags: swift, windows, vscode, mac
Published: 2023-12-31 08:14:22

---
title: Writing and Running Swift Code without Xcode on Mac and Windows
published: true
description: Swift is multiplatform. Here's how to code with Swift without Xcode.
tags: swift, windows,vscode, Mac
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-12-31 08:12 +0000
---
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][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][vscode-cmd]):

1. [Swift][ext-swift]
2. [CodeLLDB][ext-codelldb]
3. [SwiftLint][ext-swiftlint]
4. [Swift Project Creation][ext-swiftprojectcreation]

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][visualstudio] if you don't have `winget` installed. `winget` ([winget][winget]) is a package manager for Windows like chocolatey and scoop. They have been [compared][winget-comp] 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][desktop-development].

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][vscode-cmd]):

1. [Swift][ext-swift]
2. [CodeLLDB][ext-codelldb]
4. [Swift Project Creation][ext-swiftprojectcreation]

*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:

```swift
platforms: [
    .macOS(.v14)
],
```

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][swift-env]) 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.

[swift-env]: https://github.com/kylef/swiftenv
[winget]: https://learn.microsoft.com/en-us/windows/package-manager/winget/
[homebrew]: https://brew.sh/
[desktop-development]: https://learn.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=msvc-170
[visualstudio]: https://visualstudio.microsoft.com/vs/community/
[vscode-cmd]: https://stackoverflow.com/questions/34286515/how-to-install-visual-studio-code-extensions-from-command-line

[winget-comp]: https://dev.to/bowmanjd/chocolatey-vs-scoop-package-managers-for-windows-2kik?comments_sort=top

[ext-swift]: https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang
[ext-codelldb]: https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb
[ext-swiftlint]: https://marketplace.visualstudio.com/items?itemName=vknabel.vscode-swiftlint
[ext-swiftprojectcreation]: https://marketplace.visualstudio.com/items?itemName=cgrevisse.swift-project-creation