{"id":1975,"date":"2015-12-05T11:35:51","date_gmt":"2015-12-05T17:35:51","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=1975"},"modified":"2015-12-26T12:55:29","modified_gmt":"2015-12-26T18:55:29","slug":"swift-on-linux","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/swift-on-linux\/","title":{"rendered":"Swift on Linux!"},"content":{"rendered":"<p><b>Update!<\/b>  If you have the Swift compiler on your Linux box already, head on over to our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/more-swift-on-linux\/\">tutorial<\/a> on building Swift applications to run on Linux.  If you don&#8217;t have one, come and grab it with <code>apt-get<\/code> from <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/ubuntu-packages-for-open-source-swift\/\">here<\/a>.<\/p>\n<p>On December 3, 2015 Apple made good on its promise to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Open_source\">open source<\/a> <a href=\"https:\/\/developer.apple.com\/swift\/\">Swift<\/a>, their new language for development on Apple platforms.  Swift has quickly risen in popularity and folks are developing Mac, iOS, WatchOS, and now tvOS apps with it.<\/p>\n<p>While I enjoy developing in the Apple ecosystem and writing iOS apps, my first love will always be writing software for Unix systems (please don&#8217;t post a comment reminding me that OS X is a Unix).  So while I&#8217;m excited about the fact that Swift is now open source, I&#8217;m more excited about the ramifications of that fact, primarily inroads Swift can make into the server room, operations environments, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_of_Things\">IoT<\/a> devices.<\/p>\n<h2>Swift on Ubuntu<\/h2>\n<p>Apple has made available all of the Swift source code available on <a href=\"https:\/\/github.com\/apple\/swift\">Github<\/a>, and even included instructions for building it on Ubuntu 14.04 LTS.  I tried it, and while I did get it to compile, it isn&#8217;t quite polished yet in terms of &#8220;easy installation.&#8221;  Fortunately if you search around you will eventually find <a href=\"https:\/\/swift.org\/download\/#linux\">this page<\/a> for easier installation.<\/p>\n<p>So, here we go, the iAchieved.it quick start guide for Swift on Linux, though its fairer to call it Swift on Ubuntu for now.<\/p>\n<p><b>Step 1.  Get the Package<\/b><\/p>\n<p>Go to <a href=\"https:\/\/swift.org\/download\/\">the download page<\/a> and grab the version appropriate for your Ubuntu version, either 14.04 or 15.10.  I&#8217;m going to get the 14.04 version and save it to my <code>\/usr\/local\/archive<\/code> directory where I keep tarballs.<\/p>\n<pre class=\"crayon:false\">\r\nsudo mkdir \/usr\/local\/archive\r\ncd \/usr\/local\/archive\r\nsudo wget https:\/\/swift.org\/builds\/ubuntu1404\/swift-2.2-SNAPSHOT-2015-12-01-b\/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz\r\n<\/pre>\n<p><b>December 13 Update:<\/b>  Copy\/paste is great, but there are new builds coming out every other week from Apple.  Double-check the <a href=\"https:\/\/swift.org\/download\/#latest-development-snapshots\">latest development snapshots<\/a> before your <code>wget<\/code>.<\/p>\n<p><b>Step 2.  Unpack<\/b><\/p>\n<p>Like <code>\/usr\/local\/archive<\/code> is a convention I use, I am also picky about where vendor software is installed and typically use the <code>\/opt\/<i>provider<\/i><\/code> convention, which I&#8217;ll do here.<\/p>\n<pre class=\"crayon:false\">\r\ncd \/opt\r\nsudo mkdir apple\r\ncd apple\r\nsudo tar -xzf \/usr\/local\/archive\/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz\r\n<\/pre>\n<p>This will leave us with a directory named <code>swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04<\/code> in <code>\/opt\/apple<\/code>.  <\/p>\n<p><b>Step 3.  Add it to your PATH<\/b><\/p>\n<p>The directory structure Apple chose within is a typical <code>usr<\/code> structure with the Swift compiler in <code>usr\/bin<\/code>.  Let&#8217;s update our path now to pick up the compiler.  In your shell&#8217;s rc-file update your <code>PATH<\/code> environment variable to include <code>\/opt\/apple\/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04<\/code>, like this:<\/p>\n<pre>\r\n# User configuration\r\n  export PATH=\"\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games:\/opt\/apple\/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04\/usr\/bin\"\r\n<\/pre>\n<p>Reload your rc file (I use <code>. ~\/.zshrc<\/code>) and type <code>which swift<\/code>.  You should see something like this:<\/p>\n<pre class=\"crayon:false\">\r\n# which swift\r\n\/opt\/apple\/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04\/usr\/bin\/swift\r\n<\/pre>\n<p><b>Step 4.  Optional!  Emacs Goodness<\/b><\/p>\n<p>When developing on the Mac I like Xcode.  On a Linux box, <a href=\"https:\/\/www.gnu.org\/software\/emacs\/\">emacs<\/a> is the only editor for me.  Since there is <a href=\"https:\/\/github.com\/chrisbarrett\/swift-mode\">swift-mode<\/a> available out there I naturally wanted to use it, but it turns out it only supports Emacs 24.4 and higher, so we need to install it on Trusty (14.04).  Luckily someone out there has a <a href=\"https:\/\/launchpad.net\/~andreas-h\/+archive\/ubuntu\/emacs\">PPA<\/a> available for it:<\/p>\n<pre>\r\nsudo apt-get remove -y emacs24\r\nsudo apt-add-repository ppa:andreas-h\/emacs\r\nsudo apt-get update\r\nsudo apt-get install -y emacs24\r\n<\/pre>\n<p>Edit your <code>~\/.emacs.d\/init.el<\/code> Emacs configuration file and add something like<\/p>\n<pre>\r\n(require 'package)\r\n(package-initialize)\r\n(add-to-list 'package-archives '(\"melpa\" . \"https:\/\/melpa.org\/packages\/\"))\r\n<\/pre>\n<p>Launch emacs and using <code>M-x package-refresh-contents<\/code> to pull in the packages from the MELPA repository, and then <code>M-x package-install<\/code> to install the <code>swift-mode<\/code> package.  <\/p>\n<p><b>Step 5.  Try it out!<\/b><\/p>\n<p>We&#8217;re not going to use the REPL function right now, but will compile everything together.  First, you may need to install some dependencies if you don&#8217;t already have them:<\/p>\n<pre class=\"crayon:false\">\r\nsudo apt-get install clang libicu-dev\r\n<\/pre>\n<p><b>Warning!<\/b>  After an hour or two browsing around I discovered that the current version of <b>Foundation<\/b> released with Swift for Linux doesn&#8217;t have a lot of items implemented.  For example, the <a href=\"https:\/\/github.com\/apple\/swift-corelibs-foundation\/blob\/master\/Docs\/Status.md\">status page<\/a> currently states (as of December 5, 2015) that <code>NSURLSession<\/code> isn&#8217;t implemented and that <code>NSURL<\/code> is &#8220;mostly&#8221; implemented.  Mostly doesn&#8217;t include using <code>init(URL:)<\/code> apparently!<\/p>\n<pre class=\"crayon:false\">\r\nfatal error: init(URL:) is not yet implemented: file Foundation\/NSObjCRuntime.swift, line 64\r\n<\/pre>\n<p>If you take a look at the new <a href=\"https:\/\/github.com\/apple\/swift-package-manager\">Swift Package Manager<\/a> being developed, you can see in the source code where low-level code for routines like <code>popen<\/code> is being developed to communicate with Github for downloading repositories.  Even <code>NSTask<\/code> isn&#8217;t implemented!<\/p>\n<p>Until these Foundation classes are implemented it might be worth waiting a bit before trying to really do much with Swift on Linux.  Our sample code just shows a basic example of using <code>main.swift<\/code> and a class file to calculate the area of a rectangle.  Boring stuff!<\/p>\n<p>In a file called <code>main.swift<\/code> (this name is important as it defines to Swift the entry point of your application, like <code>main()<\/code> in C):<\/p>\n<pre class=\"lang:swift\">\r\nlet s = Rectangle(base:5, height:6)\r\nprint(\"The area of the rectangle is \\(s.area()).\")\r\n<\/pre>\n<p>Now in a file called <code>rectangle.swift<\/code>:<\/p>\n<pre class=\"lang:swift\">\r\nclass Rectangle {\r\n    var base:Int\r\n    var height:Int\r\n    \r\n    init(base:Int, height:Int) {\r\n        self.base = base\r\n        self.height = height\r\n    }\r\n\r\n    func area() -> Int {\r\n        return self.base * self.height\r\n    }\r\n}\r\n<\/pre>\n<figure id=\"attachment_1992\" aria-describedby=\"caption-attachment-1992\" style=\"width: 764px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/12\/Selection_001.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2015\/12\/Selection_001.png\" alt=\"Swift on Linux!\" width=\"764\" height=\"438\" class=\"size-full wp-image-1992\" \/><\/a><figcaption id=\"caption-attachment-1992\" class=\"wp-caption-text\">Swift on Linux!<\/figcaption><\/figure>\n<p>Compile it with the <code>swiftc<\/code> compiler, which should now be in your path.<\/p>\n<pre class=\"crayon:false\">\r\nswiftc main.swift rectangle.swift\r\n<\/pre>\n<p>And of course, run it!<\/p>\n<pre class=\"crayon:false\">\r\n# .\/main\r\nThe area of the rectangle is 30.\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>There&#8217;s not a lot one can do right now with Swift on a Linux box.  This will rapidly change as Foundation gets built out and others come in and begin developing packages.  One should continue to monitor the <a href=\"https:\/\/swift.org\/download\/\">download page<\/a> to keep track of updates and keep an eye on the status of Foundation.  As classes such as <code>NSTask<\/code> and <code>NSURLSession<\/code> are added we should see folks exploring using Swift as a &#8220;server language&#8221;.  For me, I can&#8217;t wait!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update! If you have the Swift compiler on your Linux box already, head on over to our tutorial on building Swift applications to run on Linux. If you don&#8217;t have one, come and grab it with apt-get from here. On December 3, 2015 Apple made good on its promise to open source Swift, their new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[],"class_list":["post-1975","post","type-post","status-publish","format-standard","hentry","category-linux","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1975"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=1975"}],"version-history":[{"count":25,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1975\/revisions"}],"predecessor-version":[{"id":2033,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/1975\/revisions\/2033"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=1975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=1975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=1975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}