Swift
Installing Swift on Ubuntu in TIDE
Swift
Swift is a modern, fast, and type-safe programming language developed by Apple. While widely known for iOS and macOS app development, Swift is open-source and officially supports server-side and command-line application development on Linux.
Installation
You can install Swift on Ubuntu using swiftly (the official Swift toolchain manager) or via system package repositories.
Option 1: Using System Packages (APT)
On supported Ubuntu distributions, Swift is available directly via APT:
apt update
apt install -y swiftlangWarning: Avoid running
apt install swiftdirectly, as that package installs OpenStack Swift (an object storage service), not the Swift programming language compiler. Useswiftlang.
Option 2: Using Swiftly (Official Toolchain Manager)
swiftly is the recommended toolchain manager for installing and managing multiple Swift versions on Linux.
- Install required system build dependencies:
apt update
apt install -y binutils git gnupg2 libc6-dev libcurl4-openssl-dev libedit2 libgcc-13-dev libpython3-dev libsqlite3-0 libstdc++-13-dev libxml2-dev libz3-dev pkg-config tzdata unzip zlib1g-dev curl- Download and initialize
swiftly:
curl -O https://download.swift.org/swiftly/linux/swiftly-1.0.1-$(uname -m).tar.gz
tar -zxf swiftly-1.0.1-$(uname -m).tar.gz
./swiftly init
source ~/.local/share/swiftly/env.sh- Install the latest stable Swift compiler release:
swiftly install latestVerification
Verify that the Swift compiler and toolchain are available:
swift --versionWriting and Running Code
Interpreting Code Directly
Create a Swift source file named hello.swift:
import Foundation
print("Hello, World from Swift in TIDE!")Run the script directly through the Swift interpreter:
swift hello.swiftCompiling to Native Executable
Compile the source file into an optimized binary using swiftc:
swiftc hello.swift -o hello
./helloUsing Swift Package Manager (SPM)
Initialize, build, and run a structured Swift application:
mkdir HelloApp && cd HelloApp
swift package init --type executable
swift run