How to Install Swift Programming Language on Ubuntu 20.04

Channel: Linux
Abstract: import the Swift’s PGP key using the following commandverify the version of Swift using the following command
How to Install Swift Programming Language on Ubuntu 20.04

Swift is a general purpose, compiled and high-performing programming language with a focus on safety. It was developed by Apple as a replacement for the older Objective-C language. It is very useful for those who want to develop applications for macOS or iOS from Linux. It is a intuitive and friendly programming language to new programmers. It is is optimized for development and performance without compromising on either.

In this post, we will show you how to install the Swift programming language on Ubuntu 20.04.

Prerequisites
  • A server running Ubuntu 20.04.
  • A root password is configured the server.
Getting Started

First, you will need to update your system packages to the latest version. You can update them using the following command:

apt-get update -y
Swift all required some dependencies need to be install in your system. You can install all of them using the following command:
apt-get 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

Once all the dependencies are installed, you can proceed to the next step.

Download Swift

First, visit the Swift official download page and download the latest version of Swift 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

Next, download the PGP signature to verify the integrity of the download 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 the Swift’s PGP key using the following command:

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'

Next, verify the integrity of downloaded file using the following command:

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

You should get the following output:

gpg: Signature made Tue 26 Jan 2021 10:32:56 PM UTC
gpg:                using RSA key 925CC1CCED3D1561
gpg: Good signature from "Swift 5.x Release Signing Key <[email protected]>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: A62A E125 BBBF BB96 A6E0  42EC 925C C1CC ED3D 1561
Install Swift

Next, extract the downloaded file using the following command:

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

Once the file is extracted, move the extracted directory to /opt with the following command:

mv swift-5.3.3-RELEASE-ubuntu20.04 /opt/swift

Next, export the path of the Swift to the .bashrc file:

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

Next, activate the .bashrc file with the following command:

source ~/.bashrc

Finally, verify the version of Swift using the following command:

swift --version

You should see the following output:

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

Now, you can connect to the Swift shell called REPL using the following command:

swift

Once connected, you should see the following output:

Welcome to Swift version 5.3.3 (swift-5.3.3-RELEASE).
Type :help for assistance.

Now, print your name using the following command:

  1> let name = "Hitesh Jethva"
name: String = "Hitesh Jethva"
  2> import Glibc // imports GNU C Library
  3> var ln = random() % 100
ln: Int = 83
  4> print("hello,",name,"your lucky number is", ln)
hello, Hitesh Jethva your lucky number is 83

Next, exit from the Swift shell using the following command:

6> :q
Conclusion

Congratulations! you have successfully installed Swift on Ubuntu 20.04. You can now start developing you first iOS app using Swift.

Ref From: howtoforge

Related articles