UbuntuUbuntu Based

How To Install Swift Programming Language on Ubuntu 24.04 LTS

Install Swift Programming Language on Ubuntu 24.04

Swift is a powerful and intuitive programming language developed by Apple, primarily used for iOS and macOS application development. However, its versatility allows developers to use it on various platforms, including Linux. This guide will provide a comprehensive, step-by-step approach to installing Swift on Ubuntu 24.04 LTS, ensuring you have everything set up for an efficient development environment.

Understanding the System Requirements

Before diving into the installation process, it’s essential to understand the system requirements for running Swift on Ubuntu 24.04 LTS:

  • Operating System: Ubuntu 24.04 LTS (Noble)
  • Architecture: x86_64 (64-bit)
  • Disk Space: At least 1 GB of free space for installation
  • Memory: Minimum of 2 GB RAM recommended

Additionally, ensure your system is up-to-date with the latest security updates and packages.

Preparing Your Ubuntu System

Updating the System

Start by updating your package list to ensure you have the most recent information about available software. Open your terminal and run:

sudo apt update && sudo apt upgrade -y

Installing Required Dependencies

Swift requires specific dependencies to function correctly. Install these packages using the following command:

sudo apt install binutils git gnupg2 libc6-dev libcurl4-openssl-dev libedit2 libgcc-13-dev libncurses-dev libpython3-dev libsqlite3-0 libstdc++-13-dev libxml2-dev libz3-dev pkg-config tzdata unzip zlib1g-dev -y

This command installs essential libraries and tools needed for Swift development.

Downloading Swift

Finding the Latest Version

The latest version of Swift can be found on the official Swift website. At the time of writing, Swift 6.0.3 is available for download. You can check for newer versions as needed.

Downloading Swift Using Terminal

You can download the Swift tarball directly using the wget command. Execute the following command in your terminal:

wget https://download.swift.org/swift-6.0.3-release/ubuntu2404/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu24.04.tar.gz

This command fetches the latest Swift release suitable for Ubuntu 24.04.

Verifying the Download

Importing GPG Key

To verify the integrity of the downloaded file, import the GPG key using these commands:

wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
gpg --keyserver hkp://keyserver.ubuntu.com --refresh-keys Swift

Verifying the Downloaded File

You will also need to verify the signature of the downloaded file:

wget https://download.swift.org/swift-6.0.3-release/ubuntu2404/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu24.04.tar.gz.sig
gpg --verify swift-6.0.3-RELEASE-ubuntu24.04.tar.gz.sig swift-6.0.3-RELEASE-ubuntu24.04.tar.gz

This step ensures that your download is authentic and has not been tampered with.

Installing Swift

Extracting the Tarball

Once verified, extract the downloaded tarball using the following command:

tar xzf swift-6.0.3-RELEASE-ubuntu24.04.tar.gz

Moving Files to /opt Directory

The next step is to move the extracted files to a designated directory for better organization:

sudo mv swift-* /opt/swift

Configuring Environment Variables

Updating PATH Variable

Add Swift to your system’s PATH variable so that you can run it from any terminal session:

echo "export PATH=\$PATH:/opt/swift/usr/bin" >> ~/.profile

This command appends the necessary path to your profile configuration file.

Sourcing .profile File

To apply changes immediately, run:

source ~/.profile

Verifying Installation

Checking Swift Version

You can confirm that Swift was installed successfully by checking its version with this command:

swift --version

If installed correctly, this command should return the version number of Swift you just installed.

Getting Started with Swift

Running the REPL (Read-Eval-Print Loop)

The REPL allows you to interactively run Swift code in a terminal environment:

swift

You will see a welcome message indicating that you’re now in the Swift environment.

Your First Swift Program

Create a simple “Hello, World!” program by entering:

print("Hello, World!")

This command should display “Hello, World!” in your terminal.

Troubleshooting Common Issues

  • Error: Command not found: If you receive an error stating that ‘swift’ is not found, ensure that you’ve correctly updated your PATH variable and sourced your profile file.
  • Error: Missing dependencies: If there are missing libraries or dependencies when trying to run Swift, double-check that all required packages were installed successfully.
  • Error: Version mismatch: If you encounter issues related to version compatibility, ensure you’re using a version of Swift that supports Ubuntu 24.04 LTS.
  • Error: Permission denied: If you face permission issues while moving files or executing commands, ensure you’re using ‘sudo’ where necessary or adjust permissions accordingly.
  • Error: GPG verification failed: If verification fails, re-download both the tarball and its signature file and repeat the verification process.

Congratulations! You have successfully installed Swift. Thanks for using this tutorial for installing the Swift Programming Language on the Ubuntu system. For additional help or useful information, we recommend you check the official Swift website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button