How to setup swift on macOS?

I followed the Build a Command-line Tool instructions and found that it does not work.

I want to write only CLI programs, so xcode is not installed.

I didn't use Install Swift because I found that swift --version works fine!

swift --version
swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0
➜  MyCLI swift package init --name MyCLI --type executable
Creating executable package: MyCLI
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
➜  MyCLI swift run MyCLI
error: 'mycli': Invalid manifest (compiled with: ["/Library/Developer/CommandLineTools/usr/bin/swiftc", "-vfsoverlay", "/var/folders/ht/yq7cghwx62x_dsm_q6qbtfp40000gn/T/TemporaryDirectory.JZ0Atb/vfs.yaml", "-L", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-target", "arm64-apple-macosx14.0", "-F", "/Library/Developer/CommandLineTools/Library/Developer/Frameworks", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-swift-version", "6", "-I", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-package-description-version", "6.1.0", "/Users/bifuhong/Documents/code/MyCLI/Package.swift", "-o", "/var/folders/ht/yq7cghwx62x_dsm_q6qbtfp40000gn/T/TemporaryDirectory.IGmE3P/mycli-manifest"])
error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture arm64:
  "PackageDescription.Package.__allocating_init(name: Swift.String, defaultLocalization: PackageDescription.LanguageTag?, platforms: [PackageDescription.SupportedPlatform]?, pkgConfig: Swift.String?, providers: [PackageDescription.SystemPackageProvider]?, products: [PackageDescription.Product], dependencies: [PackageDescription.Package.Dependency], targets: [PackageDescription.Target], swiftLanguageVersions: [PackageDescription.SwiftVersion]?, cLanguageStandard: PackageDescription.CLanguageStandard?, cxxLanguageStandard: PackageDescription.CXXLanguageStandard?) -> PackageDescription.Package", referenced from:
      _main in Package-1.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: 'mycli': Invalid manifest (compiled with: ["/Library/Developer/CommandLineTools/usr/bin/swiftc", "-vfsoverlay", "/var/folders/ht/yq7cghwx62x_dsm_q6qbtfp40000gn/T/TemporaryDirectory.79oKx4/vfs.yaml", "-L", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-target", "arm64-apple-macosx14.0", "-F", "/Library/Developer/CommandLineTools/Library/Developer/Frameworks", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-swift-version", "6", "-I", "/Library/Developer/CommandLineTools/usr/lib/swift/pm/ManifestAPI", "-sdk", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", "-package-description-version", "6.1.0", "/Users/bifuhong/Documents/code/MyCLI/Package.swift", "-o", "/var/folders/ht/yq7cghwx62x_dsm_q6qbtfp40000gn/T/TemporaryDirectory.ANIb7j/mycli-manifest"])
error: link command failed with exit code 1 (use -v to see invocation)
Undefined symbols for architecture arm64:
  "PackageDescription.Package.__allocating_init(name: Swift.String, defaultLocalization: PackageDescription.LanguageTag?, platforms: [PackageDescription.SupportedPlatform]?, pkgConfig: Swift.String?, providers: [PackageDescription.SystemPackageProvider]?, products: [PackageDescription.Product], dependencies: [PackageDescription.Package.Dependency], targets: [PackageDescription.Target], swiftLanguageVersions: [PackageDescription.SwiftVersion]?, cLanguageStandard: PackageDescription.CLanguageStandard?, cxxLanguageStandard: PackageDescription.CXXLanguageStandard?) -> PackageDescription.Package", referenced from:
      _main in Package-1.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If you use swift to run a single file directly, it works fine.

cat temp.swift && swift temp.swift
print(1)
1

Can you compile (swiftc) and link separately?

> swiftc -c -parse-as-library driver.swift foo.swift
> swiftc driver.o foo.o -o driver
./driver
--> foo()
// foo.swift

func foo () {
   print ("-->", #function)
}
// driver.swift

@main
enum Driver {
   static func main () {
      foo ()
   }
}

Thank you very much for your response, I followed your instructions and compiled it.

➜ cat foo.swift driver.swift
// foo.swift

func foo () {
   print ("-->", #function)
}

---

// driver.swift

@main
enum Driver {
   static func main () {
      foo ()
   }
}
➜ swiftc -c -parse-as-library driver.swift foo.swift
➜ swiftc driver.o foo.o -o driver
➜ ./driver
--> foo()

This seems to be working fine.

I solved the problem by reinstalling /Library/Developer/CommandLineTools.

1 Like

In fact, I still don't know the specific cause of this problem. I just started to learn Swift (starting from the configuration of the development environment).


Next, I will try my best to show my current development environment and how to solve this problem:

I used helix to write rust and python code on this Mac mini, so I installed /Library/Developer/CommandLineTools.

Recently, I want to learn Swift (CLI program), and at the same time, I think xcode is too heavy for current needs, so I didn't install xcode.

Run swift --version and swift package init according to the Build a Command-line Tool, and they are running well.

The problem occurred when running swift run, and the error message has been displayed in the problem description.

At this time, I didn't make any additional installation(because swift --version and swift package init work well).

After many useless attempts, I reinstalled CommandLineTools, and the problem was solved.


From this, it should be possible to judge that CommandLineTools has provided the basic tools of Swift:

➜ ls /Library/Developer/CommandLineTools/usr/bin | rg -i swift
aarch64-swift-linux-musl-clang++.cfg -> aarch64-swift-linux-musl-clang.cfg
aarch64-swift-linux-musl-clang.cfg
swift -> swift-frontend
swift-api-digester -> swift-frontend
swift-build -> swift-package
swift-cache-tool -> swift-frontend
swift-demangle
swift-driver
swift-experimental-sdk -> swift-package
swift-format
swift-frontend
swift-help
swift-package
swift-package-collection -> swift-package
swift-package-registry -> swift-package
swift-plugin-server
swift-run -> swift-package
swift-sdk -> swift-package
swift-stdlib-tool
swift-symbolgraph-extract -> swift-frontend
swift-synthesize-interface -> swift-frontend
swift-test -> swift-package
swiftc -> swift-frontend
x86_64-swift-linux-musl-clang++.cfg -> x86_64-swift-linux-musl-clang.cfg
x86_64-swift-linux-musl-clang.cfg
➜ ls /Library/Developer/CommandLineTools/SDKs -TL2
/Library/Developer/CommandLineTools/SDKs
├── MacOSX.sdk -> MacOSX15.4.sdk
├── MacOSX14.5.sdk
│   ├── System
│   ├── usr
│   ├── Entitlements.plist
│   ├── SDKSettings.json
│   └── SDKSettings.plist
├── MacOSX14.sdk -> MacOSX14.5.sdk
├── MacOSX15.4.sdk
│   ├── System
│   ├── usr
│   ├── Entitlements.plist
│   ├── SDKSettings.json
│   └── SDKSettings.plist
└── MacOSX15.sdk -> MacOSX15.4.sdk

Since I know nothing about Swift, please let me know if you want to know more about my development environment.

1 Like