Getting started with Vapor and Swift On Ubuntu 16.04

I am not an expert I myself am still learning the inner workings of vapor and swift. I wrote this in hopes to help others get setup and started with Vapor and Swift as well, instead of having to look all over the web trying to connect the dot.

Since I have found out about server side swift I have been quite excited. So I wanted to get everything setup and try to break somethings. I decided to work with Ubuntu 16.04 and use Swift 3.0.1 GM Candidate was released on October 25, 2016.

Apple Swift Logo

Get Swift

Swift needs some dependencies, lets get them then:

sudo apt-get install clang libicu-dev binutils

Download Swift:

wget https://swift.org/builds/swift-3.0.1-GM-CANDIDATE/ubuntu1604/swift-3.0.1-GM-CANDIDATE/swift-3.0.1-GM-CANDIDATE-ubuntu16.04.tar.gz

Extract and move Swift:

tar -xvf swift-3.0.1-GM-CANDIDATE/swift-3.0.1-GM-CANDIDATE-ubuntu16.04.tar.gz
mv swift-3.0.1-GM-CANDIDATE/swift-3.0.1-GM-CANDIDATE-ubuntu16.04 swift-3.0.1
sudo mv swift-3.0.1 /opt/

Next we need to make swift global:

echo 'export SWIFT_ROOT="/opt/swift-3.0.1"' >> ~/.bashrc
echo 'export PATH="$SWIFT_ROOT/usr/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Update:

sudo apt-get update

Check to make sure Swift works:

swift --version
Swift Version

Install swiftenv

Next we install swiftenv so that we can have different versions of swift. GitHub location is github.com/kyef/swiftenv

Install git if needed:

sudo apt-get install git

Clone the swiftenv repository:

git clone https://github.com/kylef/swiftenv.git ~/.swiftenv

Make swiftenv global:

echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bashrc
echo 'export PATH="$SWIFTENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(swiftenv init -)"' >> ~/.bashrc
source ~/.bashrc

Make sure everything worked by checking the Version.

swiftenv --version
swiftenv Version

To see what versions are available try:

swiftenv install --list
swiftenv Version list

To install another version just type:

swiftenv install 2.2.1

Vapor (Web Framework for Swift)

Vapor Logo

Vapor is a we framework written in Swift that can be used to build websites or APIs. Vapor has really good documentation, vapor.github.io/documentation

Before we get started we need to make sure that everything is ready for Vapor, run:

curl -sL check.vapor.sh | bash
Vapor pre-install

If you got the message above we can go ahead and install Vapor Toolbox.

curl -sL toolbox.vapor.sh | bash

When I ran the script above I got some errors. I found that running everything manually fixed the issues. You can view the script by running:

curl -sL toolbox.vapor.sh

To manually install, run:

git clone https://github.com/vapor/toolbox vapor-toolbox
cd vapor-toolbox
swift build -c release
./.build/release/Executable self install

After complete install with no errors, verify the Vapor install with :

vapor --help
vapor help menu

We are now ready to create a new project:

vapor new helloworld
New Vapor project

At this point if you want to have an older version of swift for this project you can used swiftenv to set the version needed.

Note: I did not change the version for the project in the example. This is just for education.

cd helloworld
swiftenv local <version you want to use>
swift package init --type executable

We are now ready to build and run the base project. The First build of the project may take couple minutes or so depending on the dependency's that it needs. For example if you add MongoKitten to the project it might take longer than normal to build.

Note: The vapor build command runs the swift build command in the background.

cd helloworld
vapor build && vapor run
Vapor build && vapor run

Once the projects is running open as shown above. Open up your browser and visit locahost:8080.

localhost:8080

Depending on the globalization depends on the language. For some reason in my virtual machine its in German.

ATOM for Development and Debug

Since we are on Ubuntu we do not have and IDE like Xcode for Swift. After looking around I came across Ankit Agarwal Post on Hacking Atom to create a Swift IDE that runs on Linux and Mac so I decided to use Atom.

If your interested in installing the Swift Debugger please follow the tutorial provided by Ankit Agarwal, He did a fantastic job on the swift debugger atom package.

I love Sublime and I have only messed around with Atom so this really was an interesting experiment for me. To install Atom on Ubuntu:

wget https://github.com/atom/atom/releases/download/v1.12.3/atom-amd64.deb
sudo dpkg --install atom-amd64.deb

If your interested in looking more at Atom take a look at there GitHub page github.com/atom/atom

To open Atom you can just type atom in the terminal.

atom

To open our project in Atom:

atom helloworld
Atom opening project

Once Atom is loaded you will have the Welcome Screen and the list of the projects folders.

Atom First Open

Open up the Sources > App > main.swift

main.swfit

Let play around by adding in:

import Vapor
let drop = Droplet()
drop.get { req in
return try drop.view.make("welcome", [
"message": drop.localization[req.lang, "welcome", "title"]
])
}
drop.get("hello") { request in
return "Hello Vapor!!"
}
drop.resource("posts", PostController())
drop.run()

Save the file and go back to the terminal and type:

cd helloworld
vapor build && vapor run

Navigate to localhost:8080/hello

Get Hello Vapor

Moving Forward

I hope I helped getting vapor and swift up and running in Ubuntu.

Finding tutorial for Vapor at this moment in time are hard to find. Here is what I have come across as of yet:

Ray Wenderlich has some really good screencasts.

The Vapor University is also a really good place to start. They also have the screencasts I added above.

There is a lot more to learn. I am personal excited to see where server side swift goes. Who knows it might become a go to in a year or so.

Thank you for reading.