1.本机
主机:mac
系统:macOS Ventura 13.2.1
语言:Swift version 5.7.2(swiftlang-5.7.2.135.5 clang-1400.0.29.51)
由于macOS 开发工具XCode内置Swift,无需再次安装,
只需设置环境变量 export TOOLCHAINS=swift
2.云主机
主机:腾讯云
系统:Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)
语言:Swift version 5.7.3 (swift-5.7.3-RELEASE)
注意:在安装Swift工具链之前,最好先更新主机资源库
# ssh连接主机(腾讯云默认linux为ubuntu账户)
ssh ubuntu@xxx.xxx.xxx.xxx
# 更新apt资源库
sudo apt update
sudo apt upgrade
前往swift官网,寻找心仪的swift版本,我本次选择的版本为5.7.3-release(符合安装平台Ubuntu 22.04,根据vapor toolox dockerfile描述,选择5.7.3)。
根据文档描述,该版本Ubuntu 22.04包含两个架构x86_64合aarch64,根据云主机架构,我选择x86_64。
根据下载文档中Using Downloads——>Linux描述,开始下载安装swift环境。
# 安装依赖包(请严格按照官方文档执行,此处仅参考)
sudo apt-get install binutils git gnupg2 libc6-dev libcurl4-openssl-dev libedit2 libgcc-9-dev libpython3.8 libsqlite3-0 libstdc++-9-dev libxml2-dev libz3-dev pkg-config tzdata unzip zlib1g-dev
# 创建一个安装目录
mkdir swift
cd swift
# 下载资源文件swift-<VERSION>-<PLATFORM>.tar.gz
sudo wget https://download.swift.org/swift-5.7.3-release/ubuntu2204/swift-5.7.3-RELEASE/swift-5.7.3-RELEASE-ubuntu22.04.tar.gz
# 下载签名文件swift-<VERSION>-<PLATFORM>.tar.gz.sig
sudo wget https://download.swift.org/swift-5.7.3-release/ubuntu2204/swift-5.7.3-RELEASE/swift-5.7.3-RELEASE-ubuntu22.04.tar.gz.sig
# 如果是首次安装,需要把pgp秘钥导入钥匙串(请严格按照官方文档执行,此处仅参考)
gpg --keyserver hkp://keyserver.ubuntu.com --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 C068 94BD 63BC 1CFE 91D3 06C6' \
'5E4D F843 FB06 5D7F 7E24 FBA2 EF54 30F0 71E1 B235' \
'8513 444E 2DA3 6B7C 1659 AF4D 7638 F1FB 2B2B 08C4' \
'A62A E125 BBBF BB96 A6E0 42EC 925C C1CC ED3D 1561' \
'8A74 9566 2C3C D4AE 18D9 5637 FAF6 989E 1BC1 6FEA' \
'E813 C892 820A 6FA1 3755 B268 F167 DF1A CF9C E069'
# 验证签名——刷新秘钥
gpg --keyserver hkp://keyserver.ubuntu.com --refresh-keys Swift
# 验证签名文档swift-<VERSION>-<PLATFORM>.tar.gz.sig
gpg --verify swift-5.7.3-RELEASE-ubuntu22.04.tar.gz.sig
# 解压资源 swift-<VERSION>-<PLATFORM>.tar.gz
tar xzf swift-5.7.3-RELEASE-ubuntu22.04.tar.gz
# 此时可发现当前位置多了一个swift-5.7.3-RELEASE-ubuntu22.04的目录,此目录中就是swift命令目录。删除已下载的压缩文本(可以不删除)
sudo rm swift-5.7.3-RELEASE-ubuntu22.04.tar.gz.sig
sudo rm swift-5.7.3-RELEASE-ubuntu22.04.tar.gz
#将swift命令目录移动到usr/share/swift中(可以不移动,如果想要移动,请注意目录路径,需根据主机的实际情况输入)
sudo mv /home/ubuntu/swift/swift-5.7.3-RELEASE-ubuntu22.04 /usr/share/swift
# 将swift命令加入PATH环境变量
export PATH=/usr/share/swift/usr/bin:"${PATH}"
# 云主机将指令加入启动文本
echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
至此,swift toolchains已安装完毕,在命令行中输入执行swift,显示如下结果:
Welcome to Swift!
Subcommands:
swift build Build Swift packages
swift package Create and work on packages
swift run Run a program from a package
swift test Run package tests
swift repl Experiment with Swift code interactively
Use `swift --help` for descriptions of available options and flags.
Use `swift help <subcommand>` for more information about a subcommand.
输入swift --version,则输出如下结果:
Swift version 5.7.3 (swift-5.7.3-RELEASE)
Target: x86_64-unknown-linux-gnu
网友评论