如果你对Swift热情万分,请看此文,此文介绍 Swift 源码方式安装,如果你只是希望安装运行、编译环境,请看此文。
环境准备
- Ubuntu 14.04
- apt-get
源的选择
由于库比较新,需要使用官方源,修改方法如下。
首先备份源列表(for sure):
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
而后用gedit或其他编辑器打开:
gksu gedit /etc/apt/sources.list[![](http://upload-images.jianshu.io/upload_images/1117005-992e595497ec6611.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://wiki.ubuntu.org.cn/File:Qref_Kubuntu_Logo.png) kdesudo kate /etc/apt/sources.list [![](http://upload-images.jianshu.io/upload_images/1117005-07c0c0698951b45a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://wiki.ubuntu.org.cn/File:Qref_Xubuntu_Logo.png) gksu mousepad /etc/apt/sources.list[![](http://upload-images.jianshu.io/upload_images/1117005-07c0c0698951b45a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://wiki.ubuntu.org.cn/File:Qref_Xubuntu_Logo.png) gksu leafpad /etc/apt/sources.list (12.04版)[![](http://upload-images.jianshu.io/upload_images/1117005-bef2c87baf12910c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://wiki.ubuntu.org.cn/File:Qref_Edubuntu_Logo.png) gksu gedit /etc/apt/sources.list
从下面列表中选择合适的源,替换掉文件中所有的内容,保存编辑好的文件:
deb http://archive.ubuntu.com/ubuntu/ vivid main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ vivid-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ vivid-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ vivid-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ vivid-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ vivid main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ vivid-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ vivid-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ vivid-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ vivid-backports main restricted universe multiverse
一定要执行下面的Shell更新 apt-get
sudo apt-get update
安装依赖
执行以下命令安装 Swift 依赖
sudo apt-get install git cmake ninja-build clang uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config
如果你的系统版本低于 14.04,执行以下Shell:
sudo apt-get install clang-3.6
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
下载 Swift 源码到本机
创建一个 Swift 的目录,然后在该目录下执行以下命令 Clone Github 上的源文件到本机。
git clone https://github.com/apple/swift.git swift
git clone https://github.com/apple/swift-llvm.git llvm
git clone https://github.com/apple/swift-clang.git clang
git clone https://github.com/apple/swift-lldb.git lldb
git clone https://github.com/apple/swift-cmark.git cmark
git clone https://github.com/apple/swift-llbuild.git llbuild
git clone https://github.com/apple/swift-package-manager.git swiftpm
git clone https://github.com/apple/swift-corelibs-xctest.git
git clone https://github.com/apple/swift-corelibs-foundation.git
叹完一杯星巴克后,再回来,发现,已经执行完这些命令了。
设置环境变量并安装 Ninja
export PATH=/Applications/CMake.app/Contents/bin:$PATH
git clone https://github.com/martine/ninja.git
cd ninja
./configure.py --bootstrap
然而,安装完这些以后,到现在,对我们来说并没有什么卵用。
接下来,我们再讨论一下从源码安装后的一些开发指南。