Skip to content

Use a curlbash script for installing swiftly #1133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions _data/new-data/install/linux/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@ latest-release:
pre-code-text: |
The Swiftly installer manages Swift and its dependencies. It supports switching between different versions and downloading updates.
headline: Swiftly
tabs:
- label: Bash
code: |-
curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz && \
tar zxf swiftly-$(uname -m).tar.gz && \
./swiftly init --quiet-shell-followup && \
. "${SWIFTLY_HOME_DIR:-$HOME/.local/share/swiftly}/env.sh" && \
hash -r
- label: Fish
code: |-
curl -O https://download.swift.org/swiftly/linux/swiftly-(uname -m).tar.gz && \
tar zxf swiftly-(uname -m).tar.gz && \
./swiftly init --quiet-shell-followup && \
set -q SWIFTLY_HOME_DIR && source "$SWIFTLY_HOME_DIR/env.fish" || source ~/.local/share/swiftly/env.fish
code: curl -fsSL https://swift.org/swiftly-install | sh
links:
- href: 'https://raw.githubusercontent.com/swiftlang/swiftly/refs/heads/main/LICENSE.txt'
copy: 'License: Apache-2.0'
Expand Down
15 changes: 1 addition & 14 deletions _data/new-data/install/macos/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@ latest-release:
pre-code-text: |
To download toolchains from Swift.org, use the Swiftly toolchain installer. Swift.org toolchains support Static Linux SDK, include experimental features like Embedded Swift and support for WebAssembly.
headline: Swiftly
tabs:
- label: Bash
code: |
curl -O https://download.swift.org/swiftly/darwin/swiftly.pkg && \
installer -pkg swiftly.pkg -target CurrentUserHomeDirectory && \
~/.swiftly/bin/swiftly init --quiet-shell-followup && \
. "${SWIFTLY_HOME_DIR:-$HOME/.swiftly}/env.sh" && \
hash -r
- label: Fish
code: |
curl -O https://download.swift.org/swiftly/darwin/swiftly.pkg && \
installer -pkg swiftly.pkg -target CurrentUserHomeDirectory && \
~/.swiftly/bin/swiftly init --quiet-shell-followup && \
set -q SWIFTLY_HOME_DIR && source "$SWIFTLY_HOME_DIR/env.fish" || source ~/.swiftly/env.fish
code: curl -fsSL https://swift.org/swiftly-install | sh
links:
- href: 'https://raw.githubusercontent.com/swiftlang/swiftly/refs/heads/main/LICENSE.txt'
copy: 'License: Apache-2.0'
Expand Down
2 changes: 1 addition & 1 deletion install/linux/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ title: Install Swift - Linux
<div class="content">
<div class="release-box section">
<div class="content">
{% include new-includes/components/code-box.html with-tabs = true content = site.data.new-data.install.linux.releases.latest-release.swiftly %}
{% include new-includes/components/code-box.html content = site.data.new-data.install.linux.releases.latest-release.swiftly %}
</div>
</div>
<div class="release-box section">
Expand Down
2 changes: 1 addition & 1 deletion install/macos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ title: Install Swift - macOS
<div class="content">
<div class="release-box section">
<div class="content">
{% include new-includes/components/code-box.html with-tabs = true content = site.data.new-data.install.macos.releases.latest-release.swiftly%}
{% include new-includes/components/code-box.html content = site.data.new-data.install.macos.releases.latest-release.swiftly%}
</div>
</div>
<div class="release-box section">
Expand Down
52 changes: 52 additions & 0 deletions swiftly-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #!/bin/sh for maximum portability? shellcheck can help validate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

soundness.sh seems to prefer bash

##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift.org open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift.org project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of Swift.org project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

# Install script for Swiftly
# Usage: curl -fsSL https://swift.org/swiftly-install | sh
OS_NAME=$(uname -s)
OS_ARCH=$(uname -m)
TMPDIR=$(mktemp -d)

cd ${TMPDIR}

case "$OS_NAME" in
"Linux")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, almost no command line tool is guaranteed to be installed. It's worth checking if all of the programs the script needs are installed.

  • ps (from a package usually called procps) is not always present.
  • tar is not always present.
  • Neither is curl (yes the swift.org instructions propose fetching the script with curl in the first place, but someone might've still manually used wget or something other way to get the script onto their machine)

mktemp and uname come from coreutils, but those are a bit too fundamental to bother checking for IMO. cd is a shell builtin.

Or, you could detect apt-get and dnf and try to install the relevant packages to ensure they'll be there (without -y, so the user is prompted if configured).

something like this:

if command -v apt-get >/dev/null 2>&1 ; then # debian, ubuntu
    apt-get update
    apt-get install curl procps tar
elif command -v dnf >/dev/null 2>&1 ; then # rhel
    dnf update
    dnf install curl procps tar
elif command -v yum >/dev/null 2>&1 ; then # amazonlinux2
    yum update
    yum install curl procps tar
elif ! command -v curl >/dev/null 2>&1 || ! command -v ps >/dev/null 2>&1 || ! command -v tar >/dev/null 2>&1 ; then
    echo "Missing one of more of the following programs: curl, ps, tar" >&2
    exit 1
fi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I think that automatically running their package manager to install the missing utilities is a bit heavy-handed, so I just went with the fail-fast exit when one of the necessary commands is not present.

curl -fsSLO https://download.swift.org/swiftly/linux/swiftly-${OS_ARCH}.tar.gz
tar zxf swiftly-${OS_ARCH}.tar.gz
./swiftly init
SWIFTLY_HOME_DIR=${SWIFTLY_HOME_DIR:-$HOME/.swiftly}
;;
"Darwin")
curl -fsSLO https://download.swift.org/swiftly/darwin/swiftly.pkg
installer -pkg swiftly.pkg -target CurrentUserHomeDirectory
~/.swiftly/bin/swiftly init
SWIFTLY_HOME_DIR=${SWIFTLY_HOME_DIR:-$HOME/.local/share/swiftly}
;;
*)
echo "Unknown platform: $OS_NAME"
echo "This script supports Linux and Darwin/macOS only"
exit 1
;;
esac

echo "Swiftly installed! Run the following command to set up your environment:"
PARENT_SHELL=$(ps -o comm= -p "$PPID")
if [ "$PARENT_SHELL" = "fish" ]; then
echo "source ${SWIFTLY_HOME_DIR}/env.fish"
else
# Not fish: assume sh-compatible (bash/zsh)
echo "source ${SWIFTLY_HOME_DIR}/env.sh"
fi