The swift package also comes with Swift REPL tool. We can use the tool to quickly execute swift snippets.
Downloading Swift language package
To setup Swift language on Linux. We need to download the Swift package which we will configure in our environment variable. Then we can easily compile and run Swift programs.
After downloading the package file, simply extract it and rename it to Swift for the convenience.
Set up and configure Swift on your Linux distribution
We need to add the path of this extracted folder to the environment variable. This can be done in two ways.
Method 1: If you want to set up Swift for all the users then move the Swift folder to /opt/ folder. Add the path of this Swift folder in /etc/environment file and save it.
Now just logout and login again and type swift –version in the terminal. This will output the currently configured Swift details.
Method 2: Another way to set up Swift for a particular user is that you can place the Swift folder in the user directory that was extracted and renamed earlier.
In my case it’s here : /home/takshaksh/Swift. Now simply echo the environment folder in /home/takshaksh/.bashrc file.
If this file is not available in your distribution then simply create one and don’t forget to add “.” a dot before the file name as it denotes that it’s a hidden file. This file gets executed every time when you start the terminal.
Add the below line at the end of the file. Here takshaksh is the username so replace it with yours.
Running a swift program using CLI
let name = "Hello World from Ubuntu !!" print(name)
Create a file and place the above swift code in it and save it as HelloSwift.swift
Alternatively, For a step-by-step video guide, check out this tutorial
That is how you can set up and run Swift programs on Linux. Let us know your thoughts in the comment section below. Do you think Swift is the best programming language out there?