Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Swift package via package manager?

I am currently following the document from swift.org to play around with the new Swift Package Manager.

I cloned the demo project from Github and run the following command from terminal.

git clone https://github.com/apple/example-package-dealer.git
cd example-package-dealer
swift build
.build/debug/Dealer

While I run swift build, error arise.

<unknown>:0: error: no such file or directory: 'build'

Any idea?

like image 606
Zigii Wong Avatar asked Dec 19 '15 09:12

Zigii Wong


People also ask

How do I install a Swift package?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

Does Swift have a package manager?

The Swift Package Manager is a tool for managing the distribution of Swift code. It's integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. The Package Manager is included in Swift 3.0 and above.

How do I add a Swift package to Xcode project from GitHub?

Open your Xcode project, navigate the File tab within the macOS bar, and click on “Add Packages”. In the Add New Package window, you can select from recently used or Apple Swift Packages. Alternatively, you can search for a package via the name or the URL to the Github page.

What is the Swift Package Manager (Swift Package Manager)?

The Swift Package Manager has come a long way and support for Xcode improves with every release of Apple's IDE. Even though there are several third party solutions for managing the dependencies of a project, CocoaPods and Carthage being the most popular, the Swift Package Manager has become a viable option in recent years.

How do I import a swift package into an app project?

Starting with Xcode 11, Swift packages can now be directly added and imported into an app project using Xcode’s new Swift Packages option, which is located within the File menu.

How do I add a package to rxswift?

The easiest option to add a Swift package is by entering the package URL in the search field in the top right. The package URL is the location where the Swift Package Manager can find the Package.swift file of the package, that is, the manifest of the package. For RxSwift, that simply means adding the GitHub URL.

How do I find the version of a package in Swift?

The Swift Package Manager performs a process called dependency resolution to figure out the exact version of the package dependencies that an app or other Swift package can use. The Package.resolved file records the results of the dependency resolution and lives in the top-level directory of a Swift package.


4 Answers

I stuck for an hour. Sadly, it's just an epic fail that downloading the wrong swift package. If you want to use swift build, MAKE SURE you download the development version.

like image 86
yuhua Avatar answered Oct 18 '22 06:10

yuhua


You did not add the newly installed swift to your PATH. The instructions for doing that are here.

On OS X:

export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"

On Linux:

export PATH=/path/to/Swift/usr/bin:"${PATH}"

Then to test it works:

swift build --version
like image 37
mxcl Avatar answered Oct 18 '22 06:10

mxcl


I think it is a problem with the latest snapshot:

  • Ubuntu 14.04 Swift 2.2 Snapshot of January 11 contains swift-build in usr/bin
  • Ubuntu 14.04 Swift 2.2 Snapshot of January 25 doesn't contain swift-build in usr/bin

Besides, the January 25 release also seems to miss other files (libFoundation.so and libXCTest.so in usr/lib/swift/linux for instances).

Either there has been a structure change....or, simply, the latest snapshot had a problem ;) While they fix the snapshot, simply take the older (January 11th) snapshot, and you should be fine.

like image 38
Sebastien Poivre Avatar answered Oct 18 '22 05:10

Sebastien Poivre


I was facing the same issue and in my case, I recently updated my Xcode to 8.2.1 and swift 3.0 comes with it. I was getting this log.

Ranvijay-Mac-mini:PerfectTemplate ranaranvijaysingh$ swift build
error: unable to invoke subcommand: /Library/Developer/CommandLineTools/usr/bin/swift-build (No such file or directory)

The path it was taking was incorrect. It was suppose to be:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin


To change the path, run this command.

export PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:$PATH

And DONE.
Run : swift build again on your project and if you get this error.

xcrun: error: unable to lookup item 'PlatformPath' from command line tools installation
xcrun: error: unable to lookup item 'PlatformPath' in SDK '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
error: Invalid platform path

then you need to change the SDK path as well.
In my case, I had two .sdk at path

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ 

MacOSX.sdk  MacOSX10.12.sdk

To know what is your SDK path, run this command.

xcrun --sdk macosx --show-sdk-path

My case i got this.

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk

To change it run this command.

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

and NOW DONE. Try running swift build now.

like image 3
Rana Ranvijay Singh Avatar answered Oct 18 '22 04:10

Rana Ranvijay Singh