Swift is a programming language developed by Apple. Swift is often used to develop applications for iOS, iPadOS, macOS, tvOS, and watchOS.
This tutorial shows how to install Swift on Ubuntu 20.04.
Install Swift
Get the latest version tag of Swift release from GitHub. Assign version tag to variable.
SWIFT_VERSION=$(curl -s "https://api.github.com/repos/apple/swift/releases/latest" | grep -Po '"tag_name": "swift-\K[0-9.]+')
Download the latest version of Swift from official page:
curl -o swift.tar.gz "https://swift.org/builds/swift-${SWIFT_VERSION}-release/ubuntu2004/swift-${SWIFT_VERSION}-RELEASE/swift-${SWIFT_VERSION}-RELEASE-ubuntu20.04.tar.gz"
Create a new directory to store Swift and extract the tar.gz
file:
sudo mkdir /opt/swift
sudo tar xf swift.tar.gz --strip-components=1 -C /opt/swift
Add /opt/swift/usr/bin
directory to the PATH environment variable. This variable can be set in /etc/profile
file. In this case, Swift will be available for all users as a system-wide command. It can be done by executing the following command:
echo 'export PATH=$PATH:/opt/swift/usr/bin' | sudo tee -a /etc/profile
Logout and login to your machine to make changes to take effect or run the following command to apply the changes immediately:
source /etc/profile
We can check Swift version:
swift -version
The tar.gz
file is no longer needed, remove it:
rm -rf swift.tar.gz
Testing Swift
Create a main.swift
file:
nano main.swift
Once file is opened, add a line of code:
print("Hello world")
Use swift
command to run script:
swift main.swift
Uninstall Swift
If you decided to completely remove Swift, just delete the installation directory:
sudo rm -rf /opt/swift
Remove entry from /etc/profile
file:
sudo sed -i '/export PATH=\$PATH:\/opt\/swift/d' /etc/profile
source /etc/profile
Leave a Comment
Cancel reply