{"id":8641,"date":"2021-01-19T22:11:38","date_gmt":"2021-01-19T22:11:38","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8641"},"modified":"2022-03-14T20:44:23","modified_gmt":"2022-03-14T20:44:23","slug":"setting-up-swift-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/setting-up-swift-on-ubuntu-18-04\/","title":{"rendered":"Setting up Swift on Ubuntu 18.04"},"content":{"rendered":"<p>Let&#8217;s setup a swift development environment on Ubuntu 18.04. &#8220;Why?&#8221;, you might ask, as swift was written by Apple for Mac and iOS development and I do happen to have a Apple Mac with everything installed there, my answer is &#8220;why not&#8221;, let&#8217;s give it a try.<\/p>\n<ul>\n<li>Go to <a href=\"https:\/\/swift.org\/download\/#releases\" rel=\"noopener\" target=\"_blank\">https:\/\/swift.org\/download\/#releases<\/a> and locate the version of Swift you want to download, I picked Swift 5.3.2, Ubuntu 18.04. Download this to your machine.<\/li>\n<li>From ~.\/Downloads run\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntar -xvzf swift-5.3.2-RELEASE-ubuntu18.04.tar.gz \r\n<\/pre>\n<p>Obviously replace the .tar.gz with whatever version you download.\n<\/li>\n<li>\nNow would probably be a good time to move the resultant decompressed folder to where you want it to be kept, mine&#8217;s in a ~\/Home\/swift directory.\n<\/li>\n<li>Open .bashrc and add the following line (or just export the path if you want it temporary without adding to .bashrc)\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nexport PATH=$PATH:$HOME\/swift\/swift-5.3.2-RELEASE-ubuntu18.04\/usr\/bin\r\n<\/pre>\n<p>Don&#8217;t forget to save the .bashrc file if you&#8217;ve gone that route and ensure the path to swift usr\/bin matches where you moved your files to\n<\/li>\n<li>I&#8217;m going to use VSCode, as my editor, and there&#8217;s several Swift extensions, I installed Swift Language 0.2.0. This has the largest number of downloads, I&#8217;ve no idea how good this is compared to others, but that&#8217;s the one I&#8217;ve installed for now.<\/li>\n<li>If all went well, open a terminal window in VSCode or just use the a bash terminal and type\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nswift --version\r\n<\/pre>\n<p>If all went well you&#8217;ll see the Swift version and Target listed\n<\/li>\n<\/ul>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Now we have swift installed (hopefully), let&#8217;s look at the sort of &#8220;Getting Started&#8221; type of things you&#8217;ll want to try.<\/p>\n<p>Let&#8217;s use the swift command to create a simple executable application. So run the following for your terminal<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift package init --type executable\r\n<\/pre>\n<p>This will use swift&#8217;s package manager to create a new executable application with good old &#8220;Hello World&#8221;.<\/p>\n<p>To build this, simple execute the following command<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift build\r\n<\/pre>\n<p>and to run this we simply execute the command<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift run\r\n<\/pre>\n<p>When making changes to your code you can actually just save the file(s) and use the <em>run<\/em> command which will build and run the application.<\/p>\n<p>The command which generated this source created a Package.swift file which is where we add dependencies etc. Source code is stored in the Sources folder, and here the file is named main.swift which simply contains<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprint(&quot;Hello World!&quot;)\r\n<\/pre>\n<p>In the Tests folder we have the tests for the application. We&#8217;re not going to go into those now except to say, you can run the following command to run the tests<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nswift test\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s setup a swift development environment on Ubuntu 18.04. &#8220;Why?&#8221;, you might ask, as swift was written by Apple for Mac and iOS development and I do happen to have a Apple Mac with everything installed there, my answer is &#8220;why not&#8221;, let&#8217;s give it a try. Go to https:\/\/swift.org\/download\/#releases and locate the version of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286,312],"tags":[],"class_list":["post-8641","post","type-post","status-publish","format-standard","hentry","category-swift","category-ubuntu-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8641"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8641\/revisions"}],"predecessor-version":[{"id":9266,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8641\/revisions\/9266"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}