Swift is open source, and it's now a cross-platform, with open source Swift not only running on MAC and IOS platforms, but now also running on Linux platforms. The swift.org website provides complete guidelines for installing the Swift environment on Linux. Current Swift supports Ubuntu 15.10 and Ubuntu 14.04 two versions.
Here I use Ubuntu 14.04, to lead you to experience the use of Swift on the Linux platform, interested students feel to look over, if it does help you, remember to vote like, let more people see, thank you very much;
Tools/Materials
Ubuntu 14.04 Installation Swift
- 1
Installing the Clang compiler
The clang compiler is the C compiler provided by Apple and is a required component of Swift. Run the following command at the terminal to perform the installation:
sudo apt-get--assume-yes install clang
- 2
Get Swift Packages
Swift packages come from the Https://swift.org/download page, or you can configure the packages you need to download according to your own needs. ;
To download the Swift package via the Curl command:
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
- 3
Install Swift
1. Using the TAR command, unzip the completed Swift package:
Tar zxf swift-2.2-snapshot-2015-12-01-b-ubuntu14.04.tar.gz
2. 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}\ ">>/home/ Vagrant/.profile
Command-line execution causes the script to take effect:
Source/home/vagrant/.profile
3, command line, enter the Swift--version command to verify that Swift is installed successfully.
If the installation succeeds, we can receive output like this:
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
END
Using Swift on the Ubuntu14.04
- 1
Create a source file
Swift installation succeeded in creating our first program, which, by convention, is naturally the Hello world program.
Create a source file using the Vim hello.swift command.
Then input I into the insert mode in Vim, enter in the source file:
Print ("Hello, World")
After the code is finished, we press ESC into operation mode and enter: Wq Save to exit.
- 2
Compile run
After the source file is created, use the SWIFTC command to compile the code.
SWIFTC Hello.swift
After compiling, we use the LS command to look at the contents of the current directory, in addition to the hello.swift file, the compiler generated a Hello executable for us. We can run this file:
./hellohello
The operation was successful.
You can also execute the source file directly using the Swift command
Swift Hello.swift
The Swift program does not need to import many base libraries, nor does it need to write any of the main functions, which by default will be the entrance to the program, which incorporates modern development thinking.
END
Using Swift package Management
- 1
Open Source Swift provides a package management platform, and I 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 repository for quick problem solving on the package management platform.
The Swift open source community on Github provides an example of this problem, first we install the GIT command tool on Ubuntu:
sudo apt-get--assume-yes install git
- 2
Clone the Swift package management sample from Github to local
git clone https://github.com/apple/example-package-dealer.git
After cloning succeeds, we use the LS command to see that there are 4 files in this package:
Contributing.md Package.swift readme.md Main.swift
Where Main.swift is the code file for the program, and Package.swift is the package management file, let's take a look at Package.swift's content
This defines the name of our package: Name: "Dealer", and then defines its dependency, which relies on another code base;
- 3
Crawling down dependent projects
With the Package.swift definition, the Swift Build command automatically crawls the required third-party libraries for our projects based on the corresponding dependencies.
To run the Swift Build command installation dependency, you can see that swift build has processed the dependencies of the various projects for us;
From the output can be seen, we copied out of this project dependent projects, but also rely on other projects, forming a dependency chain.
When the Swift build command finishes running, we find that there is a Packages directory in the root directory of the project, which contains the dependencies we crawled down:
Swift Open source Linux Ubuntu Install