# Found some solutions when troubleshooting installing swift on Windows

**URL:** <https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467>\
**Category:** Using Swift\
**Tags:** windows\
**Created:** [February 5, 2021, 3:55am UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467 "2021-02-05T03:55:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhymu8354](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rhymu8354/32/15230_2.png) [@rhymu8354](https://forums.swift.org/u/rhymu8354)\
**Post date:** [February 5, 2021, 3:55am UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/1 "2021-02-05T03:55:16Z")

</div>

I ran into a few issues installing and using the Swift 5.3.3 toolchain on Windows 10, some of which are mentioned in other posts but never fully explained. I did some troubleshooting and learned a few things that helped me get it all working. Some of the issues were caused by errors in the documentation. I thought I would share my notes here in case anyone else runs into the same or similar issues. Here are some things to look at or try.

## Problems with %SWIFTFLAGS%

The [Getting Started - On Windows](https://swift.org/getting-started/#on-windows) documentation says extra parameters are needed on the command line for REPL, and recommends defining a `SWIFTFLAGS` environment variable:

```sh
set SWIFTFLAGS=-sdk %SDKROOT% -I %SDKROOT%/usr/lib/swift -L SDKROOT%/usr/lib/swift/windows

```

The intent is to add `%SWIFTFLAGS%` on the end of any command line. For example:

```sh
> swiftc hello.swift -o hello.exe %SWIFTFLAGS%
> swift repl %SWIFTFLAGS%

```

First of all, notice there is a '%' character missing in front of `SDKROOT` in the "-L" option. This ends up passing an invalid path for the option. I corrected this right away, although I'm not sure it had an effect at all (see below).

Next, I found out the "-sdk" option doesn't seem to be always necessary for REPL (it seems to run fine without it); however, you do need it to run the compiler (`swiftc`), otherwise you get errors like this:

```sh
> swiftc hello.swift -o hello.exe
clang: error: no such file or directory: 'C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\lib\swift\windows\x86_64\swiftrt.obj'
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)

```

Now I could be wrong about REPL; perhaps you _do_ need the "-sdk" option in some use cases of REPL. So it doesn't hurt to include `%SWIFTFLAGS%` on the REPL command line, granted. Anyway, since you definitely need the "-sdk" option when running the compiler, I think it would help if this was mentioned earlier (and more prominently) in the documentation, rather than being buried all the way down the section about REPL.

Another weird thing you might run into is "redefinition of module" errors trying to use `swiftc` to compile a simple one-line "hello world" program. This happened to me initially, and while experimenting trying to fix it, I found that shortening the `%SWIFTFLAGS%` environment variable down to just the "-sdk" option made the errors go away:

```swift
set SWIFTFLAGS=-sdk %SDKROOT%

```

Interestingly, I was unable to reproduce the problem later by adding the "-I" and "-L" options back. So I'm not sure if these options are really needed. Perhaps they're needed for more use cases more advanced than simply running REPL or compiling a "hello world" program? If someone could explain if/why they're still necessary, I'd like to know.

## Problems with Python version

The [Getting Started - On Windows](https://swift.org/getting-started/#on-windows) documentation lists `Python 3 64-bit (3.7.8)` as a "recommended additional component". I found that actually Python 3.9 is _required_, because the `lldb.exe` bundled with Swift 5.3.3 depends on `python39.dll` directly. If you have even a slightly older version of Python, such as 3.8, some tools won't work (notably, REPL). I think closer attention to this should be given when building the tools and documenting how to install and use them. The dependency is much more strict and seems to change from one build of the toolchain to the next, whereas the documentation seems out of date. Alternatively, consider either bundling a copy of Python with the toolchain, or loosen the dependency by dynamically trying several candidate versions of the Python DLL or having the toolchain installer detect what version of Python you have and customize `lldb.exe` accordingly.

The main reason I think the LLDB-Python version dependency should be more clearly documented and/or improved upon is it leads to a problem that is more difficult to track down. There is no error message printed/displayed if you run `swift.exe` without the correct version of Python in the path. Tracking the activity of `swift.exe` with something like `Process Monitor` doesn't reveal the issue either. What I found actually happens is `swift.exe` launches `lldb.exe` as a child process, but that process (rather silently!) dies with the Windows error code `STATUS_DLL_NOT_FOUND` (0xc0000135). To detect if this happens, you have to inspect the process exit code which is available through the variable `%ERRORLEVEL%` immediately after running the command. `0xc0000135` will be interpreted as a signed integer `-1073741515`:

```sh
> swift.exe
> echo %ERRORLEVEL%
-1073741515

```

To find the missing DLL I first had to figure out that `swift.exe` was launching `lldb.exe`, and then I tried running `lldb.exe` directly:

```sh
> lldb.exe

```

In Windows 10 this causes a pop-up window to appear which says:

```swift
lldb.exe - System Error

The code execution cannot proceed because python39.dll was
not found. Reinstalling the program may fix this problem.

```

I verified that `lldb.exe` has been linked to depend directly on `python39.dll` by opening `lldb.exe` in [Dependency Walker](https://dependencywalker.com/):

 ![image](https://global.discourse-cdn.com/swift/original/2X/4/482be4d369acbe8cf476c9f1aa2c388199d4bb0d.png)

## No Package Manager

I found out the hard way that there's no Swift Package Manager yet on Windows. You get a rather cryptic (for a Swift newbie) error if you try to run `swift package` for example:

```sh
> swift package
error: unable to invoke subcommand: C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swift-package ()

```

I think the documentation should include a disclaimer in the "Using the Package Manager" section that it's not available at all on Windows yet. Alternatively, perhaps deploy a "dummy" swift-package.exe that simply prints a message to the console like this:

```sh
> swift package
Sorry, the Swift Package Manager is not yet available for the Windows platform.

```

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [February 5, 2021, 6:14am UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/2 "2021-02-05T06:14:11Z")

</div>

> [@rhymu8354](#):
>
> First of all, notice there is a '%' character missing in front of `SDKROOT` in the "-L" option.

> [@rhymu8354](#):
>
> I found that actually Python 3.9 is _required_ , because the `lldb.exe` bundled with Swift 5.3.3 depends on `python39.dll` directly.

These need fixing. I’m not sure where that page is generated from. @mishal_shah, are you the one to ask about this?

If you find other such mistakes, you can bring them up either in [bugs.swift.org](http://bugs.swift.org) or the [Site Feedback](https://forums.swift.org/c/site-feedback/3) part of the forum, whichever seems a better match for the issue.

> [@rhymu8354](#):
>
> I'm not sure if these options are really needed.

None of these are supposed to be necessary in the long run. Windows introduced a split between the toolchain and the SDK that hadn’t existed on earlier platforms, and 5.3 was released before the toolchain was properly taught to find the external SDK, so for now you need to redirect a few pieces manually.

The number of extra flags needed has been steadily shrinking over time as Windows catches up. Which of the flags you actually need could vary from one patch release to another, and the page you were reading isn’t version‐specific. Redundantly specifying unneeded flags should not cause any problems, but you can leave off any flag once you discover it is no longer needed.

To some degree, which flags are actually needed can depend on which features you actually touch. Importing C modules and using XCTest are two such examples from the past.

> [@rhymu8354](#):
>
> If someone could explain if/why they're still necessary, I'd like to know.

- `-sdk` directs Swift to use an SDK outside the toolchain. Windows is the only platform where the host SDK lives outside the toolchain by default. (The SDK is the stuff specific to the target platform; you use the same toolchain, but point at a different SDK when you cross‐compile, such as when building for Android from Windows.)
- `-I` and `-L` instruct the compiler to look in additional locations for libraries and headers. The locations listed here point inside the SDK because some features haven’t been taught to derive them automatically from the `-sdk` flag and still look in the wrong place.

> [@rhymu8354](#):
>
> I found out the hard way that there's no Swift Package Manager yet on Windows.

Information about this sort of thing is under [Platform Support](https://swift.org/platform-support/).

I _think_ the package manager is available in the snapshots and is on track for inclusion in Swift 5.4.

In the meantime some of us [cross‐compile out of WSL](https://github.com/SDGGiesbrecht/Workspace/blob/5003aa1ce0c834c888e34d9da1ff4606dee5bee5/.github/workflows/Workspace%20Validation.yaml#L48-L186) in order to compile for Windows using SwiftPM with Swift 5.3. Others [use CMake instead](https://github.com/compnerd/swift-cmake-examples#cmake-examples-for-swift).

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [February 8, 2021, 5:20pm UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/3 "2021-02-08T17:20:23Z")

</div>

Thanks for the information @rhymu8354! Could you please file a SR as @SDGGiesbrecht mentions for the documentation issue? I can't believe that the missing `%` has gone unnoticed for so long.

Ah, I think that the 5.3 builds missed the fix to ensure that we used the same python version as VS. The only reason that this is done is to avoid the need to manually install Python. Unfortunately, that was missed in the 5.3 release :-(. This should be fixed in the 5.4 release.

As to SPM, 5.4 will hopefully include that. Unfortunately, it seems that the downloads page does not list the 5.4 snapshot (CC: @mishal_shah).

---

<div class="post-metadata">

**Author:** ![mishal\_shah](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mishal_shah/32/247_2.png) [@mishal\_shah](https://forums.swift.org/u/mishal_shah)\
**Post date:** [February 11, 2021, 6:57pm UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/4 "2021-02-11T18:57:35Z")

</div>

Nightly **development** Swift 5.4 toolchain for Windows 10 is now available on [Swift.org - Download Swift](https://swift.org/download/#snapshots)

---

<div class="post-metadata">

**Author:** ![liudonghua123](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/liudonghua123/32/21387_2.png) [@liudonghua123](https://forums.swift.org/u/liudonghua123)\
**Post date:** [January 20, 2022, 8:06am UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/5 "2022-01-20T08:06:05Z")

</div>

After I installed python3.7 for swift 5.5.2 on windows. I can start `swift repl`, but I got crashed when I tried to execute any expression. More details I filed an issue on [swift repl crashed on windows. · Issue #53311 · llvm/llvm-project · GitHub](https://github.com/llvm/llvm-project/issues/53311).

```swift

C:\Users\Liu.D.H>swift repl
Welcome to compnerd.org Swift version 5.5.2 (swift-5.5.2-RELEASE).
Type :help for assistance.
1> 1+2
Assertion failed: false && "called into swift language runtime stub", file D:\a\1\s\llvm-project\lldb\source\Plugins\LanguageRuntime\Swift\SwiftLanguageRuntime.cpp, line 368
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
 #0 0x00007ff7d0fde755 (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\lldb.exe+0x1e755)
 #1 0x00007ffd4a52cd84 (C:\Windows\System32\ucrtbase.dll+0x7cd84)
 #2 0x00007ffd4a52dd61 (C:\Windows\System32\ucrtbase.dll+0x7dd61)
 #3 0x00007ffd4a52f7ea (C:\Windows\System32\ucrtbase.dll+0x7f7ea)
 #4 0x00007ffd4a52f6e1 (C:\Windows\System32\ucrtbase.dll+0x7f6e1)
 #5 0x00007ffd4a52f981 (C:\Windows\System32\ucrtbase.dll+0x7f981)
 #6 0x00007ffcc9384d9c PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x824d9c)
 #7 0x00007ffccdf99774 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x5439774)
 #8 0x00007ffccdf9eff3 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x543eff3)
 #9 0x00007ffccdf9c6e8 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x543c6e8)
#10 0x00007ffccdf96b36 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x5436b36)
#11 0x00007ffcc90ae4a3 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x54e4a3)
#12 0x00007ffcc90bca9b PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x55ca9b)
#13 0x00007ffcc908ac46 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x52ac46)
#14 0x00007ffcc903960a PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x4d960a)
#15 0x00007ffcc903832e PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x4d832e)
#16 0x00007ffd4a4d6c0c (C:\Windows\System32\ucrtbase.dll+0x26c0c)
#17 0x00007ffd4c7854e0 (C:\Windows\System32\KERNEL32.DLL+0x154e0)
#18 0x00007ffd4cfc485b (C:\Windows\SYSTEM32\ntdll.dll+0x485b)

C:\Users\Liu.D.H>
C:\Users\Liu.D.H>echo %SWIFTFLAGS%
-sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L SDKROOT%/usr/lib/swift/windows

C:\Users\Liu.D.H>swift repl %SWIFTFLAGS%
Welcome to compnerd.org Swift version 5.5.2 (swift-5.5.2-RELEASE).
Type :help for assistance.
1> 1+2
Assertion failed: false && "called into swift language runtime stub", file D:\a\1\s\llvm-project\lldb\source\Plugins\LanguageRuntime\Swift\SwiftLanguageRuntime.cpp, line 368
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
 #0 0x00007ff7d0fde755 (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\lldb.exe+0x1e755)
 #1 0x00007ffd4a52cd84 (C:\Windows\System32\ucrtbase.dll+0x7cd84)
 #2 0x00007ffd4a52dd61 (C:\Windows\System32\ucrtbase.dll+0x7dd61)
 #3 0x00007ffd4a52f7ea (C:\Windows\System32\ucrtbase.dll+0x7f7ea)
 #4 0x00007ffd4a52f6e1 (C:\Windows\System32\ucrtbase.dll+0x7f6e1)
 #5 0x00007ffd4a52f981 (C:\Windows\System32\ucrtbase.dll+0x7f981)
 #6 0x00007ffcc9384d9c PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x824d9c)
 #7 0x00007ffccdf99774 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x5439774)
 #8 0x00007ffccdf9eff3 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x543eff3)
 #9 0x00007ffccdf9c6e8 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x543c6e8)
#10 0x00007ffccdf96b36 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x5436b36)
#11 0x00007ffcc90ae4a3 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x54e4a3)
#12 0x00007ffcc90bca9b PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x55ca9b)
#13 0x00007ffcc908ac46 PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x52ac46)
#14 0x00007ffcc903960a PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x4d960a)
#15 0x00007ffcc903832e PyInit__lldb (C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\liblldb.dll+0x4d832e)
#16 0x00007ffd4a4d6c0c (C:\Windows\System32\ucrtbase.dll+0x26c0c)
#17 0x00007ffd4c7854e0 (C:\Windows\System32\KERNEL32.DLL+0x154e0)
#18 0x00007ffd4cfc485b (C:\Windows\SYSTEM32\ntdll.dll+0x485b)

C:\Users\Liu.D.H>

```

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [January 20, 2022, 5:21pm UTC](https://forums.swift.org/t/found-some-solutions-when-troubleshooting-installing-swift-on-windows/44467/6 "2022-01-20T17:21:57Z")

</div>

I've posted multiple times about this on the forums before ([Error starting the swift repl on windows - #5 by compnerd](https://forums.swift.org/t/error-starting-the-swift-repl-on-windows/40692/5)), please search first. Reporting errors should go through the Swift bug tracking system, not the forums nor LLVM's issues.

While the REPL works (particularly when cajoled to do the right thing), it doesn't work out of the box due to some regression that has crept in a long while back in LLDB. You're welcome to investigate and fix that, there are some pointers for what is going on at [Can't interpret .swift file on Windows - #7 by compnerd](https://forums.swift.org/t/cant-interpret-swift-file-on-windows/47656/7). I've done some work to improve the lldb implementation for library loading, but that still doesn't handle the library search paths properly. This might be best fixed in the Swift local lldb changes.

In the mean time, I recommend that you just compile the code instead of executing it in the REPL.
