Skip to content

Commit 486df6d

Browse files
committed
docs: Create development environment setup instructions (including virtual)
1 parent cd04dd1 commit 486df6d

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed

Sources/SwiftCrossUI/SwiftCrossUI.docc/Quick start.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Creating a cross-platform SwiftCrossUI app with Swift Bundler.
66

77
This quick start guide uses [Swift Bundler](https://github.com/stackotter/swift-bundler). Although not strictly required, it simplifies many aspects of cross-platform distribution and provides a platform-agnostic configuration format for specifying things such as app icons and custom URL schemes.
88

9+
> Note: If you're new to Swift, we recommend starting with <doc:Setting-up-a-development-environment>.
10+
911
## Installing Swift Bundler
1012

1113
Follow [the installation instructions in Swift Bundler's readme](https://github.com/stackotter/swift-bundler?tab=readme-ov-file#installation-). `mint` is the preferred installation method on macOS and Linux as of the last update to this quick start guide.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Setting up a virtual development environment
2+
3+
Detailed set up instructions for virtual Linux and Windows development environments.
4+
5+
## Overview
6+
7+
Most developers only have access to one or two of the major operating systems for development. Setting up virtual machines is an important part of developing and testing cross-platform apps, especially for smaller less-specialized teams.
8+
9+
This article covers VM creation, leaving set up to the more general <doc:Setting-up-development-environments> article which applies to both physical and virtual machines.
10+
11+
So far this article only covers macOS hosts but will be expanded to cover other host OSes in future.
12+
13+
## Creating VMs on macOS hosts (UTM)
14+
15+
Before continuing, make sure that you've installed [UTM](https://mac.getutm.app/). A download link is available on their website.
16+
17+
These instructions have been adapted from [UTM's VM creation guides](https://docs.getutm.app/guides/guides/).
18+
19+
### Windows guests
20+
21+
These instructions apply to both Windows 10 and Windows 11. Choose which you want to install before you get started. We recommend Windows 11 because it's better supported by UTM, especially on Apple Silicon.
22+
23+
Here are links to the relevant ISO download pages:
24+
25+
- [Windows 11 x64 ISO](https://www.microsoft.com/en-us/software-download/windows11)
26+
- [Windows 11 arm64 ISO](https://www.microsoft.com/en-us/software-download/windows11arm64)
27+
- [Windows 10 x64 ISO](https://www.microsoft.com/en-au/software-download/windows10iso)
28+
- Unfortunately Windows 10 arm64 doesn't support VM hypervisors. Microsoft also doesn't provide any downloads for it.
29+
30+
Once you've decided on a Windows version, you can begin the VM creation process.
31+
32+
1. Download the installer ISO for your chosen Windows version. Make sure it matches the architecture of your host machine (arm64 for Apple Silicon).
33+
2. Click the `+` button in UTM.
34+
3. Select `Virtualize`.
35+
4. Select `Windows`.
36+
5. Click `Browse...` and select the ISO you downloaded.
37+
6. Ensure that `Install Windows 10 or higher` and `Install drivers and SPICE tools` are both checked. Click `Continue`.
38+
7. Configure the amount of RAM and number of CPU cores to allocate. Then click `Next`.
39+
- Linking `swift-winui` and `swift-uwp` can be pretty RAM intensive. We recommend allocating as much RAM as you can without compromising your macOS experience.
40+
8. Configure the amount of storage to allocate. Click `Next`.
41+
9. Press `Save` to create the VM. Then press `Run` to start the VM.
42+
10. Follow any instructions the Windows installer gives you.
43+
11. Once the installation process has completed, navigate to the `D:` drive in file explorer and run the Spice Guest Tools installer. This enables clipboard sharing and automatic display resizing.
44+
45+
The next few steps are optional, and walk through setting up a shared directory between the host and guest machines.
46+
47+
1. Shutdown the VM.
48+
2. Click the edit button (the icon with 3 sliders) in the top right of UTM and navigate to the `Sharing` tab.
49+
3. Set `Path` to the directory you want to share (probably your top-level projects directory).
50+
4. Ensure that `Directory Share Mode` is set to `SPICE WebDAV`.
51+
5. Boot the VM.
52+
53+
You should now find the shared directory mounted as a drive inside your Windows VM.
54+
55+
You're now ready to begin setting up a SwiftCrossUI development environment inside your Windows VM. Head over to <doc:Setting-up-development-environments> to continue.
56+
57+
### Linux guests
58+
59+
You must choose a Linux distribution before starting VM creation. We recommend Ubuntu 24.04 because it has the best Swift support. Fedora is also great, but the latest version that Swift officially supports is a year and a half old.
60+
61+
Here's a list of ISO download pages for the most common Linux distribution choices:
62+
63+
- [Ubuntu Desktop 24.04 x64](https://ubuntu.com/download/desktop)
64+
- [Ubuntu Server 24.04 arm64](https://ubuntu.com/download/server/arm); Ubuntu doesn't provide stable arm64 desktop iso downloads, but it only takes one extra step to convert to install the Ubuntu desktop environment
65+
- [Ubuntu Desktop 24.04 arm64 (daily build)](https://cdimage.ubuntu.com/daily-live/20240421/); unstable, mileage may vary
66+
67+
Once you've decided on a Linux distribution, you can begin the VM creation process.
68+
69+
1. Download the install ISO for your chosen Linux distribution. Make sure it matches the architecture of your host machine (arm64 for Apple Silicon).
70+
2. Click the `+` button in UTM.
71+
3. Select `Virtualize`.
72+
4. Select `Linux`.
73+
5. Click `Browse...` and select the ISO you downloaded.
74+
6. Click `Continue`.
75+
7. Configure the amount of RAM and number of CPU cores to allocate. Then click `Next`.
76+
8. Configure the amount of storage to allocate. Click `Continue`.
77+
9. [Optional]: Select a directory to share with the VM. Do this if you want to use your preferred code editor on your host machine to edit code as you test it in the VM (recommended). You will have to perform some additional steps post-installation to configure directory sharing in the guest.
78+
10. Click `Continue`.
79+
11. Click `Save` to create the VM. Then press `Run` to start the VM.
80+
12. Follow the Ubuntu installer.
81+
82+
You should now have a working VM. If you used an Ubuntu Server installer, run the following commands to install a graphical environment:
83+
84+
```sh
85+
sudo apt update
86+
sudo apt install ubuntu-desktop
87+
sudo reboot
88+
```
89+
90+
Run the following command to enable clipboard sharing and dynamic display resizing:
91+
92+
```sh
93+
# Ubuntu, Debian, apt-based
94+
sudo apt install spice-vdagent
95+
# Fedora, CentOS, RPM-based
96+
sudo yum install spice-vdagent
97+
```
98+
99+
If you selected a directory to share with the VM, set up your VM to automatically mount the shared directory on boot. To do so, follow these steps, replacing `USER` with your VM username.
100+
101+
1. Edit `/etc/fstab` to add the following two lines:
102+
103+
```
104+
share /mnt/utm 9p trans=virtio,version=9p2000.L,rw,_netdev,nofail,auto 0 0
105+
/mnt/utm /home/USER/utm fuse.bindfs map=501/1000:@20/@1000,x-systemd.requires=/mnt/utm,_netdev,nofail,auto 0 0
106+
```
107+
108+
2. Now run the following commands to install required dependencies, create your mount destinations, and reboot so that your changes take effect:
109+
110+
```sh
111+
sudo apt install bindfs
112+
sudo mkdir /mnt/utm
113+
mkdir /home/USER/utm
114+
sudo reboot -h now
115+
```
116+
117+
3. You'll find your directory mounted at `~/utm`. If you run into file ownership related issues, check your macOS user id (with the `id -u` command). If it isn't `501`, update the `bindfs` fstab rule accordingly and reboot again.
118+
119+
You're now ready to begin setting up a SwiftCrossUI development environment inside your Linux VM. Head over to <doc:Setting-up-development-environments> to continue.
120+
121+
<!-- ## Creating VMs on Linux hosts (Gnome Boxes) -->
122+
123+
<!-- ### Windows guests -->
124+
125+
<!-- ### Linux guests -->
126+
127+
<!-- Linux virtual machines can be useful when you need to test on multiple Linux distributions. -->

Sources/SwiftCrossUI/SwiftCrossUI.docc/SwiftCrossUI.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ SwiftCrossUI takes inspiration from SwiftUI, allowing you to use the basic conce
1111
### Getting Started
1212

1313
- <doc:Quick-start>
14+
- <doc:Setting-up-a-development-environment>
15+
- <doc:Setting-up-a-virtual-development-environment>
1416
- <doc:Examples>
1517
- <doc:Hot-reloading>
1618

0 commit comments

Comments
 (0)