Skip to content

Instantly share code, notes, and snippets.

@harrisg
Last active August 10, 2024 22:18
Show Gist options
  • Save harrisg/fba19b74bc310e9b2f3542e614430c07 to your computer and use it in GitHub Desktop.
Save harrisg/fba19b74bc310e9b2f3542e614430c07 to your computer and use it in GitHub Desktop.
Download and install new version of Swift on Ubuntu 22.04
#!/bin/bash
# Things you should know:
# 1. I'm a terrible bash programmer.
# 2. Call with sudo.
# 3. Pass in the url of the swift .tar.gz release from https://www.swift.org/download/ .
# 4. It checks for an existing file of the same name, and will skip the download if it already exists.
# 5. This will install the Swift directory under /opt/swift-x.y.z... and symlink that to /opt/swift .
# 6. Put /opt/swift/usr/bin in your path and you can easily upgrade Swift to a new release with this script.
# export PATH=/opt/swift/usr/bin:"${PATH}"
tmp_dir=$(mktemp -d -t swift-XXXXXXXXXX)
cd $tmp_dir
url=$1
archive_name=$(basename $url)
if [ ! -f "$archive_name" ]; then
curl -s -O $url
fi
tar xzf $archive_name
dir_name=$(basename $archive_name .tar.gz)
if [ -d /opt/$dir_name ]; then rm -Rf /opt/$dir_name; fi
mv $dir_name /opt
ln -sfn /opt/$dir_name /opt/swift
rm -rf $tmp_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment