How to Install Swift Programming Language on Ubuntu 20.04

Swift, often referred to as “Objective-C, without the C,” is an open-source programming language developed and maintained by Apple. Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns. The Swift project aims to create the best available language for uses ranging from systems programming to mobile and desktop apps, scaling up to cloud services.

In the following tutorial, you will learn how to install Swift on Ubuntu 20.04 and 21.04.

Prerequisites

  • Recommended OS: Ubuntu 20.04 – optional (Ubuntu 21.04 and Linux Mint 20)
  • User account: A user account with sudo or root access.

Updating Operating System

First, before anything, update your Ubuntu operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

Required Dependencies Installation

To install Swift programming language on your Ubuntu operating system, you will require a few dependencies.

Proceed to install the dependencies, use the following command:

sudo apt install binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev pkg-config tzdata zlib1g-dev -y

Advertisement


Download Swift on Ubuntu 20.04

To download Swift, visit the official download page that has the latest version listed. In time, these will change, but the tutorial will use an example of how to download Swift version build 5.4.2 using the wget command as follows:

wget https://swift.org/builds/swift-5.4.2-release/ubuntu2004/swift-5.4.2-RELEASE/swift-5.4.2-RELEASE-ubuntu20.04.tar.gz

Next, you will need to download the PGP signature to verify the integrity of the download. To do this, use the following :

wget https://swift.org/builds/swift-5.4.2-release/ubuntu2004/swift-5.4.2-RELEASE/swift-5.4.2-RELEASE-ubuntu20.04.tar.gz.sig

Now you will need to import Swift’s PGP key using the following terminal command:

sudo gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys '7463 A81A 4B2E EA1B 551F FBCF D441 C977 412B 37AD' '1BE1 E29A 084C B305 F397 D62A 9F59 7F4D 21A5 6D5F' 'A3BA FD35 56A5 9079 C068 94BD 63BC 1CFE 91D3 06C6' '5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235' '8513 444E 2DA3 6B7C 1659 AF4D 7638 F1FB 2B2B 08C4' 'A62A E125 BBBF BB96 A6E0 42EC 925C C1CC ED3D 1561' '8A74 9566 2C3C D4AE 18D9 5637 FAF6 989E 1BC1 6FEA'

Lastly, you need to verify the integrity of the downloaded file using the following command:

sudo gpg --verify swift-5.4.2-RELEASE-ubuntu20.04.tar.gz.sig

Install Swift on Ubuntu 20.04

The installation process is quite quick and easy. First, you will need to extract the archive as follows:

tar -xvzf swift-5.4.2-RELEASE-ubuntu20.04.tar.gz

Next, move the extracted directory to /opt location using the following command:

sudo mv swift-5.4.2-RELEASE-ubuntu20.04 /opt/swift

Now you need to export the path of the Swift to the .bashrc file as follows:

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

Once the path is exported, you can activate the .bashrc file with the following command:

source ~/.bashrc

To round off the installation, verify the version and build:

swift --version

You should get a similar output:


Advertisement


Connect to Swift REPL on Ubuntu 20.04

To connect to the Swift REPLL (Read Eval Print Loop) can be achieved using the following command:

swift

You can write and valid Swift statements in this terminal and see them evaluated, and you can even use standard C Libraries by importing the GNU C Libary.

Below are some basic examples of Swift in action:

To exit the Swift terminal, use the following command:

:q

Comments and Conclusion

In the tutorial, you have learned how to install Swift programming language by learning how to download and import the PGP key, extract the bash and path required for it, and basic commands to get a feel for the Swift terminal.

Overall, Swift is fast, safe, modern, and enables a level of interactivity in development. It contains several features such as closures, generics, and type inference, making it easier to use, simplifying common patterns used in Objective-C. Swift combines features of both C and Objective-C without having direct built-in C compatibility and all the constraints that come along with it.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x