How to Install Swift 5 on Ubuntu 20

First, we need Clang, it is the compiler for C, C++, Objective-C, Objective-C++ и OpenCL C based on LLVM.

Open the Linux terminal and run:

sudo apt install clang

Then we need to install additional packages for Ubuntu 20.

sudo apt install libcurl4 libpython2.7 libpython2.7-dev

Now let's download Swift from the official website. I use wget:

cd ~/Downloads/
wget https://swift.org/builds/swift-5.3-release/ubuntu2004/swift-5.3-RELEASE/swift-5.3-RELEASE-ubuntu20.04.tar.gz

Then use tar to unpack the archive:

tar xzf swift-5.3-RELEASE-ubuntu20.04.tar.gz

Move to the folder "share"

sudo mv swift-5.3-RELEASE-ubuntu20.04 /usr/share/swift

We need to set the environment variable "Path"

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

Now we can test:

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

And now let's create our first Swift program "Hello World". Create a file hello.swift

print("Hello world")

Then compile it with the command:

swiftc hello.swift

And we can run our program:

./hello
Hello world

If everything is correct you should see "Hello world" in the output

Login to post a comment.