HOME -> Swift on Linux

First Impressions: Swift on Ubuntu 14.04 and 15.10

December 8, 2015

Finally the long-awaited open source Swift has been released! See Apple's documentation on swift.org. A couple of days after the release date I discovered a question on Stack Overflow complaining about a problem running Swift on Ubuntu 14.04, see http://stackoverflow.com/questions/34077880/swift-on-linux-make-very-first-step-work. So, I decided to try it for myself and installed Swift, available from https://swift.org/download/, on a few virtual machines in Microsoft Azure, Amazon Web Services, and VirtualBox on my Mac. I haven't been able to reproduce the error reported in the aforementioned Stack Overflow question, but did find that not all Ubuntu installations are equal.

Please see https://swift.org/getting-started/#installing-swift on how to install Swift. As it turns out, you can do some things even without installing Clang.

Swift Interpreter, REPL, and Build

Four ways of using Swift were tested on the different installations:
  1. One of the simplest things to do is use the swift command as an interpreter. Suppose we have a file hi.swift with the following content: print("Hi from Swift!") If you type swift hi.swift the output will be Hi from Swift! You don't even need to install Clang for this to work.
  2. You can also play with Swift read-eval-print loop (REPL) as described at https://swift.org/getting-started/#using-the-repl. This doesn't require Clang, either.
  3. You can build a native executable using the Swift compiler, swiftc: swiftc hi.swift which will create a binary named "hi" in the current directory. For this to work, Clang must be installed on the system. You can name your output file differently using the "-o" option.
  4. Use the Swift build system, see https://swift.org/getting-started/#using-the-build-system. This also requires Clang to be installed.

Experiment: Using Swift with GCC Instead of Clang

As an experiment, Swift was also tried with GCC instead of Clang. For this to work, g++ must be available. For example, if g++ is /usr/bin/g++, then ln -s /usr/bin/g++ /usr/bin/clang++ This should allow you to use swiftc and the Swift build system at least for trivial programs. Not sure how far this would go with more complex Swift code. Just an experiment...

Swift on Ubuntu in AWS, Azure, and VirtualBox VMs

Swift worked for me without any issues on the following platforms: Ubuntu was installed in the cloud from images available from Azure and AWS galleries. ISOs available from the official Ubuntu website at the time of this writing were used to install in the VirtualBox VMs.

Problems were encountered while using Ubuntu 14.04 server in VirtualBox. First, the following errors occurred: $ swift error: failed to stop process at REPL breakpoint $ swift hi.swift :0: error: could not load the swift standard library $ swiftc junk.swift clang: error: unable to execute command: Executable "ld" doesn't exist! clang: error: linker command failed with exit code 1 (use -v to see invocation) :0: error: link command failed with exit code 1 (use -v to see invocation) The last error provided a clue: ld is missing! Installing the binutils package cleared this error. However, we are not out of the woods yet: $ swiftc hi.swift /usr/bin/ld: warning: libicuuc.so.52, needed by /home/user/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/linux/libswiftCore.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: libicui18n.so.52, needed by /home/user/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/linux/libswiftCore.so, not found (try using -rpath or -rpath-link) This was cleared and everything worked fine after installing the libicu52 package: $ sudo apt-get install libicu52

Please feel free to contact me with questions or comments.

© 2015 swiftprogrammer.infoAnatoli Peredera