WARNING This page is outdated. The last time I tested it was around Swift 3.0 and things have moved on since. Be careful with what you run in your computer. Until this is updated, I recommend not running it at all.
Shell Scripts and instructions on how to set up Swift on Linux.
Follow the Steps:
These scripts are intended for use in Ubuntu 16.04. If you're not running 16.04 or a derivative, you've been warned.
Apart from running Ubuntu 16.04, you'll need git
. Open your Terminal (Ctrl + Alt + T
) and run:
sudo apt-get install git
And after that, clone this repo:
git clone https://github.com/felix91gr/swift-linuxSetup.git
With that, you're ready to start with the real setup.
You'll need certain dependencies to install the Swift Toolchain. To have them, run the BasicSwiftDependencies.sh
script and that's it:
./BasicSwiftDependencies.sh
You can compile the toolchain if you want to. But since we'll need Swift 3.1, which is still in development, is hard to reliably compile it. Instead, we're gonna use the precompiled 3.1 toolchain, available at swift.org.
For that, run the next script:
./S_3.1_Toolchain.sh
Now you should be able to use the Swift REPL
(interactive Swift terminal). Test it, closing the current terminal, then opening a new one and running:
swift
You should see the following prompt:
Welcome to Swift version 3.1-dev (LLVM 04d780b9be, Clang 5fd2455004, Swift ccffbcd31a). Type :help for assistance.
1>
If you wanna know how and why to use the REPL, go here. To exit the REPL, press Ctrl + D
.
If all went well, now you're ready to use Swift in Linux!. If you want to setup the SDE (Swift Development Environment), follow the rest of the tutorial.
SDE, or Swift Development Environment, is a great plugin for Swift development in Linux using VSCode. It is made and mantained by the awesome Jin Mingjian. It features:
- Syntax highlighting
- Autocomplete
- Error checking and highlighting
Note: For now, I can only provide the "Docker-based" steps. Sorry about that, when I manage to reliably have a "native" install of SDE, I will post it here. Given that Swift 3.1 is around the corner, it should be getting easier to make it work the native way.
Anyway. Onwards!.
You'll need Docker for this. Docker is a lightweight virtualization tool, which allows us to have a linux VM with high performance and low memory and CPU footprint. We'll use it to have a Docker Container which will run Source Kite, the backend of SDE.
But first, we'll have to install and configure Docker. For that, run:
./DockerSetup.sh
If it worked, you should see the output of the hello-world
docker command. If it did work, you should now log out and log back in to manifest the changes in your user privileges. (If that doesn't work, you might actually have to reboot for it to take effect).
Basically, you won't need the sudo keyword to use docker commands
Source Kite is the backend for SDE. It allows SDE to interface with the SourceKit libraries provided by the Swift toolchain. Here you can look at the sourcecode.
But for this tutorial, we'll use a docker image of Source Kite that Jin made available (Thank you Jin! You're the best ^o^). To download and test it, run:
./SourceKiteFromDocker.sh
We're almost there! All that is left of the scripts is to set up apt-get
to download and update VSCode from the repositories (thanks to these people for making it so easy!). After that, you'll only have to download and configure the SDE plugin in VSCode.
For installing VSCode, run:
./InstallVSCode.sh
After installing it, open it:
Click in extensions:
Search for "sde" and install it:
Then, click Reload to reload the current window and activate SDE.
That's it! Now all that is left is to configure SDE for our setup, and you'll have a working Swift IDE!
We have to tell SDE two things:
- That we are using Source Kite from Docker
- Where is the swift compiler binary located
To do so, in VSCode go to: File
=> Preferences
=> Settings
. This will open up an empty JSON
file:
Inside the brackets, insert the following: (replace as needed for your folders)
"swift.path.sourcekiteDockerMode":true,
"swift.path.swift_driver_bin":"/home/felix/swift-linuxSetup/swift-binaries/usr/bin/swift"
After that, save it (Ctrl + S
) and close it.
Try SDE! You can use your own project, or the example project provided by Jin for SDE.
-
Open the folder in VSCode: File => Open Folder
-
Open one of the sources and make an edit. Save it.
-
Start a build process (Alt + B). It may take a while.
-
After the first build, SDE will understand your project well. Type and enjoy!
I would love to provide a native (non-dockery) setup, but as of right now (March the 2nd, 2017) I haven't been able to reliably get SourceKit binaries, which are needed for the fully native setup. If you can provide some help, feel free to put up an Issue; I'm all ears!
Many, many credits go to the work of the awesome Jin Mingjian. I am but a novice who knows how to use google properly :P
He helped me substantially trying to debug my attempts at compiling and linking the libraries of the Swift 3.1 toolchain. He also explained me many concepts and properties of it, and of his own projects. Thank you very much Jin!
While we're at it: I used the work from his SDE and Source Kite projects, and his demos.
Much love to the Swift Team and to the Swift community, at swift.org and at the Swift repository, because they make Swift possible ❤️
Thanks to the Docker people, who made this special setup possible.
And lastly, thanks to the VSCode community for providing a secure apt-get repository for easy installation and update of it on Debian-based systems.
I made the scripts, and I give them to the public domain. Use them as you wish.