How to Install Swift on Fedora

Swift is Apple’s open-source programming language designed for building applications across iOS, macOS, watchOS, tvOS, and Linux. On Fedora, Swift enables server-side development, command-line tools, and cross-platform applications without requiring macOS hardware. This guide walks you through installing Swift using Fedora’s DNF package manager or Apple’s Swiftly toolchain manager, then demonstrates how to compile and run Swift code. For larger projects using the Swift Package Manager, you will also want Git installed on Fedora for dependency management.

Choose Your Swift Installation Method

Fedora offers Swift through its default repositories. However, Apple provides the Swiftly toolchain manager for installing the latest releases directly. While both methods work well, they serve different needs.

MethodChannelVersionUpdatesBest For
DNF (Fedora Repos)Fedora PackageDistribution stableAutomatic via dnf upgradeMost users who want simple, system-integrated installation
Swiftly (Apple)Swift.orgLatest releaseManual via swiftly updateDevelopers needing newest Swift features or multiple versions

For most users, the DNF method is recommended because it integrates with Fedora’s package management, receives automatic security updates, and requires no additional configuration. Choose Swiftly if you need the absolute latest Swift release or want to switch between multiple Swift versions for testing.

Install Swift via DNF (Recommended)

Step 1: Update Your System

First, refresh your package database and upgrade existing packages to ensure compatibility with the Swift installation:

sudo dnf upgrade --refresh

Step 2: Install Swift

Next, install the Swift programming language package from Fedora’s repositories:

sudo dnf install swift-lang

Specifically, DNF automatically installs required dependencies including GCC, the GNU C Library development files, and Python. The installation downloads approximately 650 MB and requires about 3 GB of disk space.

Step 3: Verify the Installation

After installation completes, verify that Swift is accessible by checking the installed version:

swift --version

Expected output:

Swift version 6.2 (swift-6.2-RELEASE)
Target: x86_64-unknown-linux-gnu
Build config: +assertions

Consequently, if you see this output, Swift is ready to use. Fedora’s packaged version may trail Apple’s latest release by a few weeks, but you receive automatic security updates through DNF.

Install Swift via Swiftly (Alternative)

Alternatively, Swiftly is Apple’s official toolchain manager for Linux that downloads Swift directly from swift.org. This method provides the latest Swift releases and supports installing multiple versions side by side.

Step 1: Download and Initialize Swiftly

First, download the Swiftly binary for your architecture and initialize it:

curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz
tar zxf swiftly-$(uname -m).tar.gz
./swiftly init

During initialization, Swiftly creates directories in ~/.local/share/swiftly/ for configuration and toolchains. Additionally, it modifies your shell profile to add Swift to your PATH. Follow the on-screen prompts to complete setup.

Step 2: Load the Environment

After initialization, load the Swiftly environment in your current session:

source "${SWIFTLY_HOME_DIR:-$HOME/.local/share/swiftly}/env.sh"

Alternatively, log out and log back in to apply the profile changes automatically.

Step 3: Verify the Installation

Check that Swift is available and view the installed version:

swift --version

Swiftly installs the latest stable release by default. However, you can install additional versions using swiftly install <version> and switch between them with swiftly use <version>.

Swiftly requires specific CPU instructions that may not be available in minimal environments like Docker containers. If initialization fails with “Illegal instruction”, use the DNF method instead or run Swiftly on a full Fedora installation.

Create and Run Your First Swift Program

With Swift installed, you can write and execute Swift code directly from the command line. This section demonstrates both running Swift files and using the interactive REPL.

Run a Swift Script

Create a simple Swift program that prints a message to the terminal:

echo 'print("Hello, World!")' > hello.swift
swift hello.swift

Expected output:

Hello, World!

The swift command interprets and runs Swift source files directly without requiring a separate compilation step. However, for production applications, you would typically compile to an executable using swiftc or the Swift Package Manager.

Use the Swift REPL

The Swift REPL (Read-Eval-Print Loop) provides an interactive environment for experimenting with Swift code. Start it with:

swift repl

After the REPL loads, a numbered prompt appears where you can enter Swift expressions:

Welcome to Swift!
1>

Next, try some basic Swift operations in the REPL:

let greeting = "Hello from Swift on Fedora"
print(greeting)

The REPL evaluates each line immediately and displays results. As a result, you can define variables, create functions, and test logic interactively before adding code to your projects.

To exit the REPL, type:

:q

Compile a Swift Executable

For distribution or performance, compile Swift source files into standalone executables using the Swift compiler:

swiftc hello.swift -o hello
./hello

The swiftc compiler produces a native Linux executable that runs without the Swift interpreter. Consequently, use this approach for server applications, CLI tools, and any software you plan to deploy.

Manage Swift on Fedora

Update Swift

If you installed Swift via DNF, updates arrive automatically with your regular system updates:

sudo dnf upgrade --refresh

For Swiftly installations, update Swiftly itself and check for newer Swift toolchain releases:

swiftly self-update
swiftly update

Remove Swift

To remove the DNF-installed Swift package and its dependencies:

sudo dnf remove swift-lang
sudo dnf autoremove

Additionally, the autoremove command cleans up dependencies that were installed specifically for Swift and are no longer needed.

For Swiftly installations, first remove all installed Swift toolchains, then uninstall Swiftly itself:

swiftly uninstall all
swiftly self-uninstall

The self-uninstall command removes Swiftly’s configuration and binary. However, if it does not clean up your shell profile automatically, remove the Swiftly-related lines from ~/.bashrc, ~/.zshrc, or your shell’s profile file.

Conclusion

You can now write, run, and compile Swift programs on Fedora. Furthermore, for larger projects, explore the Swift Package Manager documentation. Finally, to set up a full development environment, install Visual Studio Code on Fedora and add the Swift extension for code completion and debugging.

Leave a Comment