How to install swift and vapor on Ubuntu 16.04

Source: Internet
Author: User
Tags curl gpg

From: Install Swift and Vapor on Ubuntu 16.04 Introduction

Swift is a programming language developed by Apple, characterized by fast, secure and modern, and it has a large community that supports language. Swift is primarily used to develop iOS and Mac OS applications, but you can also use it for server-side application development starting with Swift 3.

Vapor is a popular server-side Swift Web framework, and like Swift, Vapor has a quick and modern feature that supports many of the features in the web framework of other programming languages.

In this guide, you will install swift and vapor on Ubuntu 16.04, and then use the vapor template to create a simple Web application to test your settings. preparatory work

To learn this tutorial, you need to prepare the following:

1. An Ubuntu 16.04 server with sudo permissions for a non-root user. You can set it up by learning how to use our Initialization server Settings guide.

2. The server needs to install Git. Git should have been installed on Ubuntu 16.04, but if not, run sudo apt-get install git to install the first step-install Swift

To build and run vapor Web applications, you first need to install Swift.

First, make sure that you have the latest list of packages on your system:

$ sudo apt-get update

Then install Swift's required packages, including clang and some Python 2.7 components

$ sudo apt-get install clang libicu-dev libpython2.7

Then, to download the latest Swift binaries, you can manually download (download the address) from the swift download page, or through wget:

$ wget https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz

Next to verify that the download is not corrupted or tampered with, first import Swift's PGP key into your key ring, and the key will be used to verify the signature file

$ gpg--keyserver hkp://pool.sks-keyservers.net \
      --recv-keys \
      7463 a81a 4b2e ea1b 551F fbcf D441 C977  412B 37AD ' \
      ' 1be1 e29a 084C B305 F397 d62a 9f59 7f4d 21a5 6d5f ' \ a3ba FD35 56a5 9079  94BD 63BC 1CFE 91D 3 06c6 '
      5e4d F843 FB06 5d7f 7E24 FBA2 EF54  30f0 71E1 B235 '

Output results:

...
Gpg:key 412b37ad:public Key "Swift Automatic signing key #1 <swift-infrastructure@swift.org>" imported
Gpg:ke Y 21a56d5f:public Key "Swift 2.2 release signing key <swift-infrastructure@swift.org>" imported
gpg:key 91d306 C6:public Key "Swift 3.x release signing key <swift-infrastructure@swift.org>" imported
gpg:key 71e1b235:publ  IC Key "Swift 4.x release signing key <swift-infrastructure@swift.org>" imported
gpg:no ultimately trusted keys Found
gpg:total number processed:4
gpg:               imported:4  (rsa:4)

After the key is imported, download the signature file corresponding to the Swift version:

$ wget Https://swift.org/builds/swift-4.0-release/ubuntu1604/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu16.04.tar.gz.sig

To verify this signature file:

$ GPG--verify Swift-4.0-release-ubuntu16.04.tar.gz.sig

Validation results:

Gpg:assuming signed data in ' swift-4.0-release-ubuntu16.04.tar.gz '
gpg:signature made Wed Sep 2017 01:13:38 AM UT C using RSA key ID 71e1b235
Gpg:good signature from ' Swift 4.x release signing key <swift-infrastructure@swift.org > "
Primary key fingerprint:5e4d F843 FB06 5d7f 7E24  FBA2 EF54 30f0 71E1 B235

You may see a warning like this:

Gpg:WARNING:This key isn't certified with a trusted signature!
GPG:          There is no indication this signature belongs to the owner.

This means that the swift key you import is not yet trusted, or that you or any other key that you have installed in your key ring is clear. This warning can be ignored, but if there are some different errors (error), you should download the swift binaries again.

With the key and signature files installed, we can install Swift. Execute the following command to extract the binaries you previously downloaded:

$ tar xzf swift-4.0-release-ubuntu16.04.tar.gz

Then add the swift tool chain to the environment variable to run the Swift command

$ export Path=swift-4.0-release-ubuntu16.04/usr/bin: "${path}"

This command will only add the swift command to the current shell session path, and if you want to make sure that it is added automatically in a future session, add it to the. bashrc file.

Open the. bashrc file:

$ nano ~/.BASHRC

Add at end of file:

$ export Path=swift-4.0-release-ubuntu16.04/usr/bin: "${path}"

Save and exit.

To verify that Swift is installed correctly, run the SWIFT command:

$ swift

See is welcome to ..., which means Swift has been properly installed

Welcome to Swift version 4.0 (swift-4.0-release). Type:help for assistance.
  1>  

To further examine some of Swift's simple features, enter a program that sums all the integers from 1 to 5 (separated by a carriage return between each row):

var x = 0 for
i in 1...5 { 
    x = = i 
} 
x

REPL will calculate the result:

$R 0:int = 15

Use Ctrl+d to exit Swift REPL. Next we are ready to install vapor Step Two-install vapor

To install vapor, you will download and execute scripts from the vapor developer, add the vapor official package repository to your server's package list, and then use apt to install the latest version of vapor.

It is often not a good security measure to perform scripts that are downloaded from somewhere else without first checking them out. First, use the Curl command with the-o switch to download the installation script to the server to specify the local file name.

$ CURL-SL Apt.vapor.sh-o apt.vapor.sh

Use the less command to check this script:

$ less apt.vapor.sh

After checking the contents of the installation script, execute the script to add the repository

$ bash./apt.vapor.sh

At this point, you will be prompted to enter a sudo password. Once the password is entered, you can install the vapor package and its dependencies once the script is completed.

$ sudo apt-get install vapor

You can verify that vapor was installed successfully by using other scripts provided by the vapor developer. Similarly, download the script, check it, and then execute it

$ CURL-SL check.vapor.sh-o check.vapor.sh
$ less check.vapor.sh
$ bash./check.vapor.sh

If you see the following results, vapor has been installed successfully

✅compatible with Vapor 2

Now that swift and vapor are installed, you can create your first vapor application. Step Three-Create the vapor application

To create our application, here we will use the template provided by the vapor default, and we can create a user-oriented Web application from the Web template provided by vapor.

This template is based on Git and has been configured with your name and email address, and if you do not have git or are not configured to do so, you may see an error message that lets you configure Git. You can ignore this message, or execute these commands to provide your details:

$ git config--global user.email "your_email@example.com"
$ git config--global User.Name "Your name"

To create a Web application from this template, execute the following command

$ Vapor New Demo--template=web

This script uses the name you specified to generate a new application in the new directory

Execution results:

Cloning Template [done]
updating Package Name [do]
initializing git repository [done]
...
               _       __    ___   ___   ___
              \ \  /  / /\  | |_) / / \ | |_)
               \_\/  /_/--\ |_|   \_\_/ |_| \
                 A web framework for Swift

              Project "Demo" has been created.
       Type ' CD demo ' to enter the project directory.
   Use the ' vapor cloud deploy ' to host your project for free!
                           enjoy!

If you want to create an API instead of a complete Web application, you can use the API templates and--template = APIs provided by the vapor new demo to look at the source code of the page templates and API templates to see how they work, Next we run the application we just created and actually take a look at it. Step fourth-compile and run the vapor application

Swift applications must be compiled, unlike Python or ruby applications, which means that you must run a build process before you run your vapor application.

First, switch to the newly created demo folder:

$ CD Demo

Then execute the Vapor build command to compile the vapor application

$ Vapor Build

The first time an application is built, the program acquires dependencies that cache these dependencies and are no longer acquired in the subsequent build process so that the build processes are faster.

After the build process is complete, run the application with the following command

$ Vapor Run serve

The server starts and the output shows:

Running Demo ...
...
Starting server on 0.0.0.0:8080

Access http://your_server_ip:8080 in the browser to view the Welcome page for the vapor application you are running. Summary

The swift community is developing steadily and there are many ways to get involved, although Swift is primarily used to build native iOS and MacOS applications, but Swift is emerging on the Linux platform, and you can read Apple's free ebook The Swift Programming language to learn more about Swift. For more information about vapor, check their documentation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.