Pinterest Stumbleupon Whatsapp
Ads by Google

Apple recently released their newest programming language How Does Apple's New Programming Language Affect Me? How Does Apple's New Programming Language Affect Me? From the get-go, developers knew Apple's new Swift was going to be big. But why should you care? Read More , Swift, as open source. That means that anyone can now use this trending programming language, on any system. Apple has made it easy to get Swift going on Ubuntu, so we’ll show you how!

What is Swift?

If you haven’t heard of it before, Swift is Apple’s newest programming language intended to replace Objective-C and become the primary language for building Mac OS X and iOS applications. It’s very easy to learn the language as it’s clean and has a similar syntax to other popular programming languages.

However, Swift was only available for Apple devices — it could not be run on Windows, Linux, or other systems. With Swift’s increasing popularity, more people were asking that Apple open-source Swift or at least make it available on more platforms. After some time, Apple has finally released Swift as open source Apple's Swift Is Going Open Source: So What? Apple's Swift Is Going Open Source: So What? Even if you aren't a programmer yourself, Apple's decision to open source Swift will impact you. Want to know why and how? Read More . I think this is a good move for Apple, as it allows more people to become exposed to Swift, which in turn allows for more people to write Mac OS X and iOS applications in the future.

In any case, if you’re interested in using or learning Swift The Best Places to Learn Swift, Apple's Programming Language The Best Places to Learn Swift, Apple's Programming Language If you want to learn Swift, now is the time to dive in. The language has a bright future and the faster you learn it, the sooner you'll be able to reap the rewards. Read More , it’s now available for Linux! Right now Apple only has released snapshots made for Ubuntu 14.04 and 15.10, but opportunities to install Swift support on other distributions will certainly come in the near future. For now, here’s how to get it going on Ubuntu.

How to Install It

ubuntu-swift-download
First, you’ll need to visit the Swift download page and grab the latest version (at time of writing, there are only development snapshots available — stable releases will come soon, so it’s your choice which of the two you’d like to use). Once it’s downloaded, open the .tar file by double-clicking on it and then extract the folder that lies within to any location of your preference. This will become the location of your Swift installation.

ubuntu-swift-dependencies
Next, you’ll need to install some dependencies that Swift needs to run. You can easily install them by opening a Terminal and running

Ads by Google
sudo apt-get install clang libicu-dev

Finally, before you close the terminal window, type

gedit .profile

This will open up a text editor gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] When you think of plain text editors, the first thing that may pop into your head is Windows' Notepad application. It does exactly what its job description states - plain features for a plain text... Read More . Scroll all the way to the bottom and make two new lines. The first one should be empty, and on the second one put

export PATH=/path/to/usr/bin:"${PATH}"

where

/path/to/usr/bin

is the path to the bin folder inside the usr folder inside the Swift folder that you extracted out of the .tar file. So if you just extracted the Swift folder to your Downloads folder, the path would be something like

/home/username_here/Downloads/swift_folder_name_here/usr/bin

The export command will allow you to simply call

swift

in a Terminal and it will know where to look. Putting this line into the .profile text file will make this command permanent — otherwise its effect would be lost when you log out, shutdown, or restart your computer.

ubuntu-swift-ready
To make sure that Swift works, you can type

swift --version

into a Terminal and it should display some version information. Congrats! You now have Swift working on your Ubuntu system!

How to Run Swift Code

ubuntu-swift-run-example
To run a Swift file, all you need to do is run

swift /path/to/file.swift

It will compile and run automatically. You can also simply just run

swift

which will bring up an interactive shell with which you can run Swift code line by line.

Finally, you can also make executables from your Swift code. All you need to do is the following:

  1. Create a project folder with any name you like, then inside there create a folder with the name “sources”.
  2. Place all of your code files inside the source folder.
  3. Create a text file named “Package.swift” in the project folder (but outside the sources folder) and put at least the following in it:
import PackageDescription

let package = Package(
    name: "package_name_here_and_keep_quote_marks"
)

Finally, run

swift build

while the project folder is the working directory. You will find the executable under

.build/debug/package_name

Swift At Your Fingertips

Congrats! You’re now able to get started coding in Swift! Of course, this is a relatively simply guide meant to get anyone going with a quick and easy setup. If you need a bit more, such as key signing, you’ll need to visit the Swift homepage for more information. But other than that, feel free to start coding! Just note that (at least for the time being) you’ll still need Mac OS X and Xcode in order to write Mac OS X and iOS applications So You Want To Make iPhone Apps? 6 Projects For Beginners So You Want To Make iPhone Apps? 6 Projects For Beginners Why not try learning Swift, the language that will let you create iPhone and iPad apps? Read More , but having Swift in Ubuntu will help you get accustomed to the language.

What do you think about Swift? Does it have a future as a common, universal language like C/C++ and Java? Let us know in the comments!

  1. Bitwalker
    June 2, 2016 at 12:50 pm

    If Swift could be used to write apps for Ubuntu Touch that could help to make Touch more attractive as it needs more native apps.

  2. David
    May 14, 2016 at 3:40 am

    Riddle me this? I am trying to install Swift on Ubuntu, and from an admin world, you always use the most stable release.

    Why are the dev releases ONLY in the untested snapshots and not in the fully tested releases?

    I am finding this process to be very infuriating.

  3. ush
    April 12, 2016 at 12:00 pm

    I get this when I edit the path and restart the shell:

    The program 'swift' can be found in the following packages:
    * python-swiftclient
    * python3-swiftclient
    Try: sudo apt install

    Any suggestions appreciated.

    • NIraj Subedi
      June 21, 2016 at 10:53 am

      install the below packages :

      sudo apt install python-swiftclient
      sudo apt install python3-swiftclient

      once done check for the version.

      It will show you the version of Python Swift installed.

  4. Derek Beda
    April 8, 2016 at 1:29 am

    Long Hui:

    Go to your home directory: (You can check you are home with: pwd )

    cd ~/

    Then edit profile as listed above:

    gedit .profile

    Add the line:

    export PATH=/path/to/usr/bin:"${PATH}"

    And save it. It should work every time you login.

  5. graeme
    April 6, 2016 at 1:04 pm

    You must restart you shell after you edit the PATH. Follow those directions above, and it should work.

    But.

    My installation of Swift 2.2 doesn't include "build". The package management stuff doesn't work. REPL works. Debugger works. Compiler works. Package building: nope.

  6. Long Hui
    March 14, 2016 at 11:07 am

    For some reasons, I have to type the
    PATH=something/something everytime I use swift, any fix?

  7. Avatan
    February 25, 2016 at 10:05 am

    Thanks for the manual. It's better than the official one.

Leave a Reply

Your email address will not be published. Required fields are marked *