How to install Swift on Ubuntu 20.04

Swift is a modern open source high-performing programming language with a focus on safety. It was developed by Apple and released in 2014. Swift was designed as a replacement for the older Objective-C language. Although, the language was originally proprietary, in 2015 Apple open-sourced the language and made it available for GNU/Linux systems. Although Swift is most well known for being the language used in iOS app development, there is an uptick in using it for server-side programming on Linux. Additionally, the fact that it is a young open source general-purpose programming language may lead to increased use in other domains over time.

In this article, we will see how to install Swift on Ubuntu 20.04 LTS. Due to the version of Ubuntu that we are focusing on, before you follow this tutorial, it is recommended that you first install Ubuntu 20.04 or upgrade to Ubuntu 20.04 if you are on an earlier version.

In this tutorial you will learn:

  • How to download, verify, and install Swift on Ubuntu 20.04 LTS
  • How to invoke REPL or Read Eval Print Loop (the interactive Swift shell)
How to install Swift on Ubuntu 20.04

How to install Swift on Ubuntu 20.04

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 20.04
Software Swift
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Dependencies

First ensure that your package index and installed packages are up to date with the following command.

$ sudo apt update && sudo apt upgrade

Next, install the dependencies with 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

Download Swift

Download the swift tarball for Ubuntu 20.04 from the swift download page with the following command.

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

Verifying the Download



Next, we are going to verify the integrity of the download to ensure that it is intact and untampered with. To do so, download the PGP signature from the same download page using the following command.

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

Next, Import Swift’s PGP keys using the following command.
Note: This might take a few minutes to complete.

$ 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'
Importing Keys

Importing Keys

Finally, use the signature that you downloaded to verify the integrity of the tarball that you downloaded with the following command.

$ gpg --verify swift-5.3.3-RELEASE-ubuntu20.04.tar.gz{.sig,}

If the tarball was downloaded without any issues and is safe to be used then you should see the following line in the output gpg: Good signature from "Swift 5.x Release Signing Key <swift-infrastructure@swift.org>".

Verifying the archive

Verifying the archive

Install and setup

For the purposes of this tutorial, we will install swift to our home directory.

To extract the tarball to your home directory enter the following command.

$ tar -xvzf swift-5.3.3-RELEASE-ubuntu20.04.tar.gz -C ~

Next, we must add the swift executables to our PATH and update the PATH environment variable in the currently running instance of our shell using the following commands.

$ echo "PATH=~/swift-5.3.3-RELEASE-ubuntu20.04/usr/bin:$PATH" >> ~/.bashrc
$ . ~/.bashrc

To verify that this went through correctly enter $ swift --version and if you receive output similar to

Swift version 5.3.3 (swift-5.3.3-RELEASE)
Target: x86_64-unknown-linux-gnu

then you are good to go!

Now you should be able to enter the swift command on the command line anytime that you want.



REPL

Entering the swift command into you terminal will launch an interactive swift shell called REPL or Read Eval Print Loop. Here you can write any valid swift statements and see them evaluated. You can even import the GNU C Library to use the standard C libraries as you would when programming in C on Linux.

To get a feel for using REPL, go ahead and enter the following, pressing enter after each line.

let name = "LinuxConfig"
import Glibc // imports GNU C Library
var ln = random() % 100
print("hello,",name,"your lucky number is", ln)
using REPL

using REPL

To quit REPL enter :q

Conclusion

Now that you have Swift installed on Ubuntu you can start exploring the possibilities. Swift is still a very young language and the open-source community is expanding upon it’s uses all the time. You may choose to just take the opportunity to familiarize yourself with the language out of curiosity or you may even want to experiment with some of the server-side Swift frameworks out there. Whatever the reason, now you have a working Swift installation to use as a basis.