How to install Swift in Ubuntu?


Swift is a modern, open-source, and powerful programing language developed by Apple Inc in 2014. It is developed to replace the Object-C language which was the primary language for building iOS, iPadOS, macOS, tvOS, and watchOS applications. This language can also be used for other purposes like server-side programming.

In this article, I will discuss how to install and use the Swift programming language in Ubuntu.

Features of Swift programming language

Some of the key features of the Swift programming language are –

  • Designed for safety
  • Supports closure (or lambdas in other languages ), strings, etc
  • Tuples and multiple return values
  • Protocol oriented language
  • Fast and concise iteration over a range or collection
  • Structs that support methods, extensions, and protocols
  • Functional programming patterns, e.g., map and filter
  • Powerful built-in error handling

Installing Swift in Ubuntu

Follow the given steps to install Swift programming language in Ubuntu.

Update and upgrade your system

Run the following command to update the local package index and upgrade packages to their latest version –

sudo apt update && sudo apt upgrade -y

Install required dependencies

Next, use the following command to install the required dependencies

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

Go to the download page of Swift and download the latest binaries for installing Swift on your Ubuntu system.

OR use the following command to download the Swift tarball –

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

Installing Swift

Once the download is completed you need to extract and install it on your system.

Use the following command to extract the downloaded package –

sudo tar -xvzf swift-5.4.3-RELEASE-ubuntu20.04.tar.gz -C ~

Add Swift executable to our PATH environment variable –

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

Use the following command to update the PATH variable in currently running shell –

. ~/.bashrc

Verify you installation

Now you can verify the installation of swift on your system by checking its version. Run the following command to check the Swift version –

swift --version

check swift version

Writing your first Swift program

Swift provides REPL (Read Eval Print Loop) which is an interactive Swift shell. Here you can write and execute any valid Swift statement.

To access this shell run the following command in your terminal –

swift

You can type the given statement to print the Hello World! –

print("Hello World!")

Now you can see the output in the image below.

hello world

There are several online resources you can learn and write your code in the Swift programming language.

Conclusion

You have successfully set up Swift programming in your Ubuntu system. For any query please write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.