Open-source Swift running on Linux platform

Source: Internet
Author: User
Tags virtualbox download

Open-source Swift running on Linux platform

Swift is open-source. After Xi Da Pu Ben, we found that it has now become cross-platform. The open-source Swift not only can run on MAC and iOS platforms, but also on Linux platforms. Now let's take a look at Swift on Linux.

Install the Linux environment

The swift.org website provides complete instructions for installing the Swift environment on Linux. Currently, Swift supports Ubuntu 15.10 and Ubuntu 14.04.

If you have installed the Ubuntu environment, you can skip this section and continue with the subsequent content.

If you want to run Swift in Linux, you need a Linux environment first. If you have a Linux server, it can be used as a running environment, or you have a computer with a Linux system installed, you can also use it as the runtime environment.

  1. VirtualBox

Of course, it is more common to run Linux Through virtual machines. We can use VirtualBox as the virtual machine environment.

We can download: https://www.virtualbox.org/wiki/Downloads on the VirtualBox Download Page

Open the page and download the corresponding version based on your computer's platform.

After the download is complete, follow the instructions to install VirtualBox.

Now that the virtual machine environment has been installed, We need to install an operating system for this virtual machine, that is, the Ubuntu we need.

  1. Vagrant

Those who have used virtual machines before may think that it is a common practice to download an image from the Ubuntu website and then mount it to the virtual machine.

Of course, we can also use a simpler method, namely Vagrant.

Vagrant is a command line tool of VirtualBox. It helps us quickly configure and deploy VirtualBox hosts. Here we can download the Vagrant Installer: https://www.vagrantup.com/downloads.html

Vagrant uses a file named Vagrantfile to deploy virtual machines. This file stores a script to declare our deployment tasks.

Next, create a folder as our virtual machine directory, and then create a Vagrantfile file in this folder:

Vagrant.configure(2) do |config|  config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/20151201/trusty-server-cloudimg-amd64-vagrant-disk1.box"  config.vm.provision "shell", inline: <<-SHELL    sudo apt-get --assume-yes install clang    curl -O 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    tar zxf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz    echo "export PATH=/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin:\"${PATH}\"" >> .profile  SHELLend

After the creation, we can run the following command in this directory to install it:

vagrant up

Next, vagrant will help us install and deploy the virtual machine environment. The installation process takes some time, depending on your network speed. During this time, we will analyze the content of the Vagrantfile script.

  1. The first step of this script will help us download the Ubuntu installation package, through this line of configuration information:
config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/20151201/trusty-server-cloudimg-amd64-vagrant-disk1.box"

We passconfig.vm.boxThe location of the Ubuntu installation package is configured. The URL here points to the image file of Ubuntu LTS 14.04. Of course, you can also switch to another image source, access the https://cloud-images.ubuntu.com/vagrant to view the list of existing vagrant installation images, you can choose freely (note: swift currently supports Ubuntu 14.04 and Ubuntu 15.10 ).

  1. After the image is installed, we specify the configuration script executed after the system is started:
config.vm.provision "shell", inline: <<-SHELL## shellsSHELL

In a pair<<-SHELLAndSHELLIs the configuration script we want to run. First run:

sudo apt-get --assume-yes install clang

To installclangCompiler, a C compiler provided by Apple, is a required component of Swift.

Next, usecurlCommand to download the Swift package:

curl -O 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

The Swift package in our script comes from the https://swift.org/download page and you can configure the package to be downloaded as needed.

NexttarCommand to decompress the downloaded Swift package:

tar zxf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04.tar.gz

Finally, write the Swift bin directory to the environment variable so that the system can find the command we want to execute:

echo "export PATH=/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin:\"${PATH}\"" >> .profile

After the configuration is installed, run the following command to enter the Ubuntu command line:

vagrant ssh

After entering the command line, you can enterswift --versionCommand to verify whether Swift is successfully installed.

If the installation is successful, we can receive an output similar to this:

Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Start using Swift on Linux

Now that Swift is successfully installed, we can start to create our first program. By convention, it is naturally a Hello World Program.

Usevim helloworld.swiftCommand to create a source file.

Then input I in Vim to enter the insert mode, and enter the following in the source file:

print("Hello, world")

After writing the code, Press Esc to enter the operation mode, and then enter: wq to save and exit.

After the source file is created, we can useswiftcCommand to compile our code.

swiftc helloworld.swift

After compilation, we uselsCommand to view the contents of the current directory,helloworld.swiftFile, the compiler generateshelloworldExecutable File. We can run this file:

./helloworldHello, world

It's easy to run a complete program, from code to compilation success ~, Swift programs do not need to import many basic libraries or write any main functions. By default, outer statements are used as the entry points of the program, which are integrated into modern development thinking.

Of course, what Swift can do is far more than simply "Hello World". The open-source meaning of Swift is not just to allow us to develop and compile Swift programs in Linux, nor can we see the Swift source code. The significance of open source lies in its community function, which allows us to fully share the development achievements of everyone. Like the NodeJS community, I believe that developers who have used NodeJS will feel like this-for example, if your project requires an image processing function, go to the NodeJS package management community npmjs.com to find it, you can immediately find a third-party library to solve this problem. In addition, these third-party libraries are almost seamlessly integrated with your project. This is what the open-source community gives us the greatest strength.

Use Swift package management

Similarly, the open-source Swift provides a package management platform. We believe that in the future, whether you use Swift to develop an iOS APP or even a Linux server program, you can quickly find a third-party library to quickly solve the problem on the package management platform.

Next we will look at how to use the Swift package management system. The Swift open-source community provides an example on Github to illustrate this problem. First, install the git command tool on Ubuntu:

sudo apt-get --assume-yes install git

After the installation is successful, copy the Swift package management example from Github:

git clone https://github.com/apple/example-package-dealer.git

After the copy is successful, we uselsCommand to see that this package contains four files:

CONTRIBUTING.md  Package.swift  README.md  main.swift

Here, main. swift is the code file of the program, while Package. swift is the Package management file. Let's take a look at the contents of Package. swift:

import PackageDescriptionlet package = Package(    name: "Dealer",    dependencies: [        .Package(url: "https://github.com/apple/example-package-deckofplayingcards.git", majorVersion: 1),    ])

The name of this package is defined here:name: "Dealer"And then define its dependency, which depends on another code base:

https://github.com/apple/example-package-deckofplayingcards.git

Using the definition of Package. swift,swift buildThe command automatically captures the required third-party libraries for our project based on the corresponding dependency.

Now we can run the build command:

swift build

As you can see,swift buildProcessing the dependencies of various projects for us:

Cloning Packages/example-package-deckofplayingcardsCloning Packages/example-package-fisheryatesCloning Packages/example-package-playingcardCompiling Swift Module 'FisherYates' (1 sources)Linking Library:  .build/debug/FisherYates.aCompiling Swift Module 'PlayingCard' (3 sources)Linking Library:  .build/debug/PlayingCard.aCompiling Swift Module 'DeckOfPlayingCards' (1 sources)Linking Library:  .build/debug/DeckOfPlayingCards.aCompiling Swift Module 'Dealer' (1 sources)Linking Executable:  .build/debug/Dealer

From the output, we can see that the dependency project of the copied project depends on other projects, forming a dependency chain.

Whenswift buildAfter the command is run, we will find that one morePackagesDirectory, which stores the dependent items we captured:

+ Packages |example-package-deckofplayingcards-1.0.2|example-package-fisheryates-1.0.2|example-package-playingcard-1.0.1

This is the basic concept of the Swift package management system.

Conclusion

We believe that we should be familiar with package management, Swift package management andCocoaPods,CarthageThese commonly used package management tools are very similar. As Swift package management tools are not the first, this is Apple's first time. I believe that Swift will grow stronger after open source.

Install and deploy the Swift development environment on Ubuntu 15.10

Swift changes: what will happen from 2.2 to 3.0

Swift is officially open-source, and Swfit core library and Package Manager are also open-source.

Apple Swift tutorial

Use Swift to build an iOS email application

Swift 2.0 open-source

Build a Swift language development and learning environment in Linux

Swift details: click here

This article permanently updates the link address:

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.