|
| 1 | +# Setting up a development environment |
| 2 | + |
| 3 | +Detailed set up instructions for macOS, Linux, and Windows based development environments. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Building a cross-platform app requires testing your app in a variety of environments. Setting up development environments can come with many platform-specific challenges. This article attempts to give an overview of the steps involved and touch on the most common pain-points for each platform. |
| 8 | + |
| 9 | +In this article we'll focus on developing SwiftCrossUI apps with Swift Bundler, but much of the information should still apply if you choose to use a different build system. |
| 10 | + |
| 11 | +> Note: This article covers setting up development environments on pre-existing machines. To develop in a virtual machine, first follow <doc:Setting-up-virtual-development-environments> then return to complete the set up process. |
| 12 | +
|
| 13 | +## Apple platforms |
| 14 | + |
| 15 | +As you might expect, setting up a development environment for Apple platforms comes with the least challenges; as long as you have a Mac. |
| 16 | + |
| 17 | +This article doesn't cover Apple development from Linux or Windows machines. However, Swift Bundler will likely support cross-compilation in the future. And in the mean time [xtool](https://xtool.sh) is a great option for targeting iOS from Linux and Windows; as long as you're ok with mixing and matching build systems to target various platforms. |
| 18 | + |
| 19 | +We'll install the following tools: |
| 20 | + |
| 21 | +- [Xcode](https://developer.apple.com/xcode/); even if you aren't using the editor, you'll still need the SDKs that come with it |
| 22 | +- [Swift Bundler](https://swiftbundler.dev); this unifies your cross platform development experience with a consistent CLI interface and configuration format |
| 23 | + |
| 24 | +### Installing Xcode |
| 25 | + |
| 26 | +You have two main options: |
| 27 | + |
| 28 | +- Install Xcode from the macOS App Store |
| 29 | +- Install Xcode manually |
| 30 | + |
| 31 | +Installing from the App Store is simple: |
| 32 | + |
| 33 | +1. Just visit [Xcode's App Store page](https://apps.apple.com/us/app/xcode/id497799835?mt=12/) |
| 34 | +2. Click GET |
| 35 | + |
| 36 | +Installing manually gives you more flexibility. It also lets you install directly to an external storage device, which comes in handy if you're running low on storage. It goes like so: |
| 37 | + |
| 38 | +1. Locate your desired Xcode release on [https://xcodereleases.com] |
| 39 | +2. Click Download; this is take you to the requested Xcode release download in the Apple Developer portal, and requires an Apple Developer login |
| 40 | +3. Unzip the `.xip` file. I recommend using [unxip](https://github.com/saagarjha/unxip); it's faster and it doesn't use temporary storage on your internal drive, which causes issue when low on storage and installing to an external drive. |
| 41 | +4. Move `Xcode.app` to wherever you want it, whether that be `/Applications` or some external location. |
| 42 | +5. Open Xcode and select the SDKs you need when it prompts you to install optional components. |
| 43 | +6. Run `xcode-select --switch /path/to/Xcode.app` in terminal if installing Xcode at a non-standard location or alongside an existing version of Xcode. |
| 44 | + |
| 45 | +### Installing Swift Bundler |
| 46 | + |
| 47 | +Swift Bundler's [documentation](https://swiftbundler.dev/documentation/swift-bundler/installation) lists a wide variety of installation methods, but if you have Mint installed it's as easy as running `mint install stackotter/swift-bundler@main`. |
| 48 | + |
| 49 | +Once installed, run `swift bundler` in terminal as a quick sanity check. |
| 50 | + |
| 51 | +### Further steps |
| 52 | + |
| 53 | +If you're only using <doc:AppKitBackend> and <doc:UIKitBackend> when targeting Apple platforms, then you're good to go. |
| 54 | + |
| 55 | +If you'd like to test your app with <doc:GtkBackend> or <doc:Gtk3Backend> without leaving your Mac, visit the backends' respective documentation pages for detailed dependency installation instructions. |
| 56 | + |
| 57 | +## Linux |
| 58 | + |
| 59 | +We'll install the following: |
| 60 | + |
| 61 | +- The latest stable [Swift toolchain](https://www.swift.org/install/linux/) |
| 62 | +- [Swift Bundler](https://swiftbundler.dev) |
| 63 | +- [Gtk](https://www.gtk.org/) (3 or 4 depending on which is available) |
| 64 | + |
| 65 | +### Installing a Swift toolchain |
| 66 | + |
| 67 | +With the release of [swiftly](https://github.com/swiftlang/swiftly), the official Swift toolchain manager, installing Swift toolchains on Linux has become much easier. That said, there are still a few things to watch out for when using less mainstream Linux distributions. If Swiftly doesn't work for you, feel free to use an alternative installation method as found on the [Swift installation instructions page](https://www.swift.org/install/linux). |
| 68 | + |
| 69 | +1. Follow the [Swiftly-based installation instructions](https://www.swift.org/install/linux). |
| 70 | +2. Make sure that `swift` is not only installed but also on your path by opening a new terminal window and running `swift --version`. |
| 71 | + |
| 72 | +> Tip: If your Linux distribution isn't officially supported, check out [`xtremekforever`'s post on the Swift Forums](https://forums.swift.org/t/running-swift-on-unsupported-distributions/71741). It contains a table that tells you which distribution to select in Swiftly, or in the swfit.org downloads section, in order to maximise the chance of things working on your chosen distribution. The best distribution to download for isn't always the most intuitive. |
| 73 | +
|
| 74 | +### Installing Swift Bundler |
| 75 | + |
| 76 | +Swift Bundler's [documentation](https://swiftbundler.dev/documentation/swift-bundler/installation) lists a wide variety of installation methods. If you have Mint installed, then installing Swift Bundler is as easy as running `mint install stackotter/swift-bundler@main`. A good alternative method is the manual installation method. |
| 77 | + |
| 78 | +Once installed, run `swift-bundler` in terminal as a quick sanity check. |
| 79 | + |
| 80 | +> Warning: If you're coming from macOS, you'll have to adjust your habits because `swift bundler` with a space instead of a hyphen doesn't work on Linux. |
| 81 | +
|
| 82 | +### Installing Gtk (3 or 4) |
| 83 | + |
| 84 | +You're almost ready to go. The final step is to install the dependencies for the backend you'll be using. On Linux you're limited to <doc:GtkBackend> and <doc:Gtk3Backend>. We recommend using <doc:GtkBackend> unless you have a really good reason not to. <doc:Gtk3Backend> purely exists to support older Linux distributions, and doesn't support as many features. |
| 85 | + |
| 86 | +> Tip: some distributions such as Ubuntu 22.04 allow you to install both Gtk 3 and Gtk 4. This can come in handy when you have to target both <doc:GtkBackend> and <doc:Gtk3Backend>. |
| 87 | +
|
| 88 | +Click through to your chosen backend for detailed dependency installation instructions: |
| 89 | + |
| 90 | +- <doc:GtkBackend> |
| 91 | +- <doc:Gtk3Backend> |
| 92 | + |
| 93 | +## Windows |
| 94 | + |
| 95 | +We'll install the following: |
| 96 | + |
| 97 | +- The latest stable [Swift toolchain](https://www.swift.org/install/windows/) |
| 98 | +- [Swift Bundler](https://swiftbundler.dev) |
| 99 | +- <doc:WinUIBackend> related dependencies |
| 100 | + |
| 101 | +### Installing a Swift toolchain |
| 102 | + |
| 103 | +The [Swift installation instructions page](https://www.swift.org/install/windows/) lists various installation methods. The easiest is the WinGet-based method: |
| 104 | + |
| 105 | +1. Visit the [Swift installation instructions page](https://www.swift.org/install/windows/). |
| 106 | +2. Click the Instructions button in the WinGet installation method card. |
| 107 | +3. Follow the installation instructions. |
| 108 | +4. Run `swift --version` as a quick sanity check. |
| 109 | + |
| 110 | +We don't include the instructions here because the Visual Studio version they depend on may change in the future. |
| 111 | + |
| 112 | +### Installing Swift Bundler |
| 113 | + |
| 114 | +Swift Bundler's [documentation](https://swiftbundler.dev/documentation/swift-bundler/installation) lists a wide variety of installation methods. The only one applicable to Windows at the moment is the manual installation method. |
| 115 | + |
| 116 | +First we'll clone and build Swift Bundler locally. Make sure to do this in a location that you're ok with keeping around. |
| 117 | + |
| 118 | +```sh |
| 119 | +git clone https://github.com/stackotter/swift-bundler |
| 120 | +cd swift-bundler |
| 121 | +swift build -c release |
| 122 | +``` |
| 123 | + |
| 124 | +Next we'll add Swift Bundler to the system Path so that you can run it from any directory. These instructions have been adapted from [a eukhost article](https://www.eukhost.com/kb/how-to-add-to-the-path-on-windows-10-and-windows-11/) which includes additional images that some may find useful. |
| 125 | + |
| 126 | +1. Search for 'Edit the system environment variables' in the start menu. Select the matching option. |
| 127 | +2. Click the `Environment Variables...` button. |
| 128 | +3. Navigate to the `System Variables` tab. |
| 129 | +4. Select the row corresponding to the `Path` variable and click `Edit...`. |
| 130 | +5. Click `New`, and enter `absolute\path\to\swift-bundler\.build\release` (replacing with the appropriate path). |
| 131 | +6. Save your changes by selecting `OK` a bunch of times. |
| 132 | + |
| 133 | +Open a new command prompt window and run `swift bundler` as a quick sanity check. |
| 134 | + |
| 135 | +### Installing WinUIBackend related dependencies |
| 136 | + |
| 137 | +You're almost ready to start developing. That final step is to install the dependencies required by <doc:WinUIBackend>. Click through to <doc:WinUIBackend>'s documentation page for detailed installation instructions. |
| 138 | + |
| 139 | +> Warning: You may run into strange Swift issues if you use Command Prompt for Swift development. We recommend always using the Native Tools Command Prompt for VS 2022, which you can find in the start menu. |
0 commit comments