kodeco.com Forums

Introduction to Open Source Swift on Linux

Learn how to use open source Swift to run a Hello, World app on Linux - and how to use the Swift package manager!

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1448-introduction-to-open-source-swift-on-linux

The link “https://cloud-images.ubuntu.com/vagrant/trusty/20151201/trusty-server-cloudimg-amd64-vagrant-disk1.box
returns 404.
It seems, the correct version would be config.vm.box = “https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

You can also use
config.vm.box = "ubuntu/trusty64"
or pick another box from the Atlas box catalog.

I have installed Swift on Fedora 23, and, after juggling file locations a bit, have the REPL working. But I get an error the Mac community experienced back in June 2015, that they cleared by flushing a file.
I don’t have the exact error message at hand, but the “flushing a file” strategy doesn’t sound quite right to me. I’ll do some more fiddling in the next week or so, and see if I can’t be more helpful.
The other interesting thing is the version: Swift version 3.0 dev. I was expecting version 2.1.
Now, for my real “issue”: I would like to see some production code, complete with error checking, asserts, etc, for Swift, preferably done outside XCode 7. All I can find are the usual “tutorial” level stuff.
Can anyone point me to some genuine production code that I can study, please?

Sorry, I wasn’t quite clear about my installation “problem”.
REPL works correctly.
When I try to use swiftc to compile a “file.swift” at the command prompt it almost works. Swift on Fedora 23.

When I try this on Linux (Ubuntu 16.04):

example-package-dealer git:(master) swift build
Cloning Packages/example-package-deckofplayingcards
Using version 1.0.3 of package example-package-deckofplayingcards
Cloning Packages/example-package-fisheryates
Using version 1.0.3 of package example-package-fisheryates
Cloning Packages/example-package-playingcard
Using version 1.0.1 of package example-package-playingcard
Compiling Swift Module 'FisherYates' (1 sources)
/home/bjorne/swift/example-package-dealer/Packages/example-package-fisheryates-1.0.3/Sources/Fisher-Yates_Shuffle.swift:17:18: error: use of undeclared type 'Collection'
public extension Collection {
/home/bjorne/swift/example-package-dealer/Packages/example-package-fisheryates-1.0.3/Sources/Fisher-Yates_Shuffle.swift:26:18: error: use of undeclared type 'MutableCollection'
public extension MutableCollection where Index == Int {
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/home/bjorne/clion/swift-2.2-SNAPSHOT-2015-12-22-a-ubuntu15.10/usr/bin/swift-build-tool", "-f", "/home/bjorne/swift/example-package-dealer/.build/debug/FisherYates.o/llbuild.yaml"]


Seems like Canonical is not hosting that ubuntu image any more, so the existing tutorial instructions don’t work. I will update them.

In the meantime, here is an updated Vagrantfile which pulls the latest version of Ubuntu Trusty, and the 2016-04-12 snapshot of Swift. It also includes a tweak to adjust permissions.

# Vagrantfile for installing:
# - latest release of Ubuntu trusty
# - Swift dev snapshot as of 2016-04-12 (which includes the Swift package manager)
Vagrant.configure(2) do |config|
  ## 1
  config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
config.vm.provision "shell", inline: <<-SHELL
    ## 2
    sudo apt-get --assume-yes install clang libicu-dev
    ## 3
    curl -O https://swift.org/builds/development/ubuntu1404/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz
    ## 4
    tar zxf swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz
    ## 4b
    sudo chown -R vagrant:vagrant swift-*
    ## 5
    echo "export PATH=/home/vagrant/swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04/usr/bin:\"${PATH}\"" >> .profile
    echo "Swift has successfully installed on Linux"

(You need to use the Swift dev snapshot, instead of the release build, in order to get the Swift Package Manager as well.)

I have Error is build

Hello$ swift build
error: unable to invoke subcommand: /home/Develop/Swift/usr/bin/swift-build (No such file or directory)

This is helpful. Thank you.

Now… where can I find documentation for the standard library or the core library as implemented on linux? Like… how do I open/read/write/close a file?

Thanks so much Alexis, very informative.

For anyone who is working through the examples months behind everyone else and wants to build Apple’s sample project, it will fail to build one of the dependencies (Fisher Yates). I updated to a more recent swift development snapshot:


and now it builds without error, although I have not gone back to repeat all of the previous examples to make sure they still work. (left as an exercise for the reader :wink:

Very exciting stuff, thanks again to Alexis for putting this out.

I see this people having this error occasionally, but I have never seen anyone offer a response as to how to fix it. I have downloaded both release and development versions of Swift to my Linux box, and I have that issue on every one. Is there a solution?


Forgive the newb questions… I have been doing tutorials and coding using Xcode for sometime. I would like to try my hand at doing a server side application on Linux. I’d still like to use Xcode, but I want my app to run on Linux. The trouble I am having is I don’t know what functionality (classes, types, etc) are specific to the Apple platform and what is available in Swift Foundation, Core Libraries, and libdispatch. I would hate to get into a situation where I build an application that uses some feature that is specific to the Apple Platform and prevents me from easily building and running on Linux.

Q. When coding in Xcode, how do I distinguish between Apple specific libraries and open Swift libraries and frameworks?

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]