# Swift Package Installation

**URL:** <https://forums.swift.org/t/swift-package-installation/35728>\
**Category:** Pitches\
**Created:** [April 23, 2020, 10:03pm UTC](https://forums.swift.org/t/swift-package-installation/35728 "2020-04-23T22:03:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![migueldeicaza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/migueldeicaza/32/31910_2.png) [@migueldeicaza](https://forums.swift.org/u/migueldeicaza)\
**Post date:** [April 23, 2020, 10:03pm UTC](https://forums.swift.org/t/swift-package-installation/35728/1 "2020-04-23T22:03:52Z")

</div>

# Swift Package Installation

# Introduction

It would be desirable to simplify the use of executables that have been compiled with `swift build` and installing the binary, payloads and other requirements into either a global location usable by all users on the system, or make it available only to the current user.

This would simplify the distribution and usage of Swift-authored tools, as instructions for installing the software would be as simple as directing the user to type `swift tool install`.

Other ecosystems like Node and .NET have made it easy for consumers of software to grab software and make it accessible with a single command. In the Node/NPM world this is achieved with the `npm install` tool and in .NET with the `dotnet tool install` command.

# Motivation

Today when you build a Swift application using `swift build`, the resulting executable and satellite dependencies are deposited in a platform and configuration specific way. Today the result of `swift build` is deposited into the `.build` directory, in itself, not entirely obvious, and inside there is a platform specific directory, as well as a `debug` or `release` directories that contain the executable.

If would be ideal if the instructions shared with the world were as simple as instruction users to install a program with: `swift build install` `--``global` and this would take care of installing the executable in the right location, and any dynamic library dependencies that are part of the project.

This command line tool could be used to install a Swift-version of the `cron` command line tool into `/usr/local/bin`:

```
$ swift build --target swift-cron
$ swift build install --global
$ ls /usr/local/bin/swift-cron
-rwxr-xr-x 1 miguel wheel 518886 Apr 6 16:39 /usr/local/bin/swift-cron

```

Linux distributions that compile binaries and distribute those additionally require the installation step to be done to a special installation root directory, and often configure the location for files. For example, configuration files go into `$sysconfdir` which is `/etc` and we would likely want our software to be installed into `/usr`, so an RPM build file could look like this:

```
swift build --target swift-cron
swift build install --prefix=/usr --prefix-sysconf=/etc

```

The above sets the prefix for all the assets to be `/usr`, so binaries would go into `/usr/bin`, and libraries into `/usr/lib`, but any configuration files would go into `/etc` via.

# Proposed Solution

Given that `swift build` can build either products or individual targets that can include more than executables, the installation would handle installing executables, libraries and shared libraries.

Additionally, to make it so that Swift software that is installed in ways that conform to the macOS, Linux file system standards or BSD standards, we borrow the concept of prefix-based software installation where various installation subdirectories are derived from the prefix directory, but can also be overwritten on a case by case basis.

also for Linux distributions to package and , as well as to make it simpler to use Linux packaging technologies that both require custom directory installations (`$sysconfdir`, `$libdir` and so on), as well support for RPM-build

The following options would be added:

- `--install` would instruct the build process to install the assets, this option by default would default to installing to the user home directory (for example on Linux, this would be in `$HOME`). The `--install` flag additionally would have:
  - `--global`would direct the installation to install these into the appropriate system-wide directory (for example, on Linux this would be `/usr/local/bin` for the executable, and `/usr/local/lib` for libraries)
  - `--prefix` and `--prefix-xxxx` flags would override the directory prefixes set either by the default, or the `--global` option, to ensure that different assets can be installed in the proper locations

- `--uninstall` would instruct the build process to remove the files that would have previously been installed with the `--install` flag, and like `--install`, this would also take the `--global` flag and the various `--prefix` flags.

These are the various directories that could be configured for different assets, the following shows a some defaults that would make sense on Linux, but would

- Executable target: goes into `--prefix-bin`, this is computed as: `$prefix/bin`
- Library targets: goes into `--prefix-lib` , this is computed as `$prefix/lib`

Additionally, we could  
And on the systems where this makes sense, we could additionally install a `pkg-config` file that would allow the Swift library to be easily referenced by other ecosystems.

# Open Topics

## Directory Configuration

While not necessary to support these, it would be nice if these capabilities were supported in Swift out of the box.

For system-wide tools, it might make sense to define a global variable that describes where to fetch assets from, in the example I showed above, the imaginary `swift-cron` command would likely expect a `cron.config` file on `/etc`.

Historically on Unix this has been handled by using compiler level defines, or hardcoding the contents on a `config.h` file. For Swift Build, these paths could be generated as a global class or module containing the paths, for example:

```
public struct SwiftPackageConfiguration {
     public let prefix = "/usr"
     public let sysconfdir = "/etc"
     public let bindir = "/bin"
}

```

## Versioning Support

One thing that I did not considered in this proposal is a versioning story - the heart of this proposal is really the binary tool installation, and not the library assets.

I suspect that there is no need to version the binary for a tool (chances of me needing `swift-cron-1`, `swift-cron-1.2`, `swift-cron-3.2` installed in parallel is not likely high).

Libraries are a different story, those could either be versioned as they tend to be in Unix (`libSwifcron.so.1.0`, `libSwiftcron.so.1.2`), or they could be versioned by using a different directory for each version (`/usr/lib/libSwiftCron/1.2/libSwiftCron.so`).

The former has the advantage that it would work with existing idioms and would be picked up by the system dynamic linker without additional system configuration.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [April 23, 2020, 10:53pm UTC](https://forums.swift.org/t/swift-package-installation/35728/2 "2020-04-23T22:53:03Z")

</div>

Have you read this other thread yet? It outlines some of the hurdles the design will have to deal with.

> [@Should SPM allow installation of package on the system](https://forums.swift.org/t/should-spm-allow-installation-of-package-on-the-system/33168):
>
> Hi, Do you think it will be useful if SPM could install package on the system(in /usr/local/bin for exemple)? This project [GitHub - swiftbrew/Swiftbrew: Homebrew for Swift packages](https://github.com/swiftbrew/Swiftbrew) actually make it possible. Thank you,

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 24, 2020, 3:43am UTC](https://forums.swift.org/t/swift-package-installation/35728/3 "2020-04-24T03:43:04Z")

</div>

I’d rather we keep package installation within a real package manager rather than spreading them among every language’s package manger. An enhancement to brew to let it install SPM build products, perhaps even directly from a repo with a package definition, would be great.

---

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [April 24, 2020, 5:07am UTC](https://forums.swift.org/t/swift-package-installation/35728/4 "2020-04-24T05:07:33Z")

</div>

> [@Jon\_Shier](#):
>
> I’d rather we keep package installation within a real package manager rather than spreading them among every language’s package manger. An enhancement to brew to let it install SPM build products, perhaps even directly from a repo with a package definition, would be great.

Brew doesn't work on Linux unfortunately though. Although I agree having it nested inside `swift build` feels awkward to me. Why not make use of SwiftPM and have the command be `swift package install`? That keeps the logic of installation and what dependencies are needed inside SwiftPM rather than leaking it into the build system itself.

Overall though, I'm all for this!

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [April 24, 2020, 2:31pm UTC](https://forums.swift.org/t/swift-package-installation/35728/5 "2020-04-24T14:31:22Z")

</div>

In general I like the concept in terms of improving the user experience of getting Swift package based tools installed.

However, I also things it’s going to take a lot of individual pieces to get us there (because of open questions and problems linked to in other thread on this topic).

What I’d like to see us do is first focus on a proposal for a new SwiftPM product type representing an “installable artifact” (tarball, cpio, etc)... this would let us make significant forward progress on designing how packages need to describe their installable assets, and cross platform/portability concerns, without immediately jumping headlong into the whole space.

Once that is done (or concurrently) I’d suggest the next step is to focus on a “sandboxed local” installation mode where those artifacts get dropped into -/.swiftpm/artifacts or something, with some helpers to get into PATH. What I’d really like to see here is something analogous to Nix ([https://nixos.org/](https://nixos.org/)).

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [April 24, 2020, 2:34pm UTC](https://forums.swift.org/t/swift-package-installation/35728/6 "2020-04-24T14:34:37Z")

</div>

To give a concrete idea of why I consider just the “deplorable artifact” problem hard, here is a concrete example of all the kind of hacks we regularly do to take a SwiftPM package and make it into something packed for macOS or (portable) Linux distribution: [Deployable Artifacts thoughts · GitHub](https://gist.github.com/ddunbar/401d41acb04bb8c10ee0a5a4f12dcddf)

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [April 24, 2020, 2:35pm UTC](https://forums.swift.org/t/swift-package-installation/35728/7 "2020-04-24T14:35:10Z")

</div>

I’m going to just not edit the deplorable typo out of this post. 😂

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 24, 2020, 2:57pm UTC](https://forums.swift.org/t/swift-package-installation/35728/8 "2020-04-24T14:57:23Z")

</div>

> [@0xTim](#):
>
> Brew doesn't work on Linux unfortunately though.

You're so spoiled for choice on Linux you could probably extend one of the existing package managers to do this.

My logic just this: one installation location is better than `numLanguages + numPackageManagers` locations, especially when the capabilities of the language installations are so primitive compared to real package managers.

I would suggest SPM focus on being a good dependency manager first, then branch into whatever.

---

<div class="post-metadata">

**Author:** ![abertelrud](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@abertelrud](https://forums.swift.org/u/abertelrud)\
**Post date:** [April 24, 2020, 5:04pm UTC](https://forums.swift.org/t/swift-package-installation/35728/9 "2020-04-24T17:04:33Z")

</div>

I completely agree with Daniel's point about separating the building of an installable set of artifacts from the subsequent actual installation of those artifacts.

In addition to the other advantages (such as easily being able to deploy to a different machine etc), it eliminates the need to use any elevated privileges during the build. Even if some of the installable artifacts require special file ownership or permissions at deployment time, those can be set in a produced CPIO (or other kind of archive) without any special privileges at the time of building (as opposed to using `chown` or `chmod` on an actual file in the file system, as is commonly seen with `make install`).

It also lets whoever is doing the deployment choose how to do that — for example, on macOS it's common to use `darwinup` to install archives of artifacts, rather than just copying them into place.

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [April 24, 2020, 5:11pm UTC](https://forums.swift.org/t/swift-package-installation/35728/10 "2020-04-24T17:11:15Z")

</div>

It's worth pointing out that `cargo`, by default, installs to `~/.cargo/bin` on UNIXy systems, and the installation of `cargo` itself has a step that adds that to the shell's PATH automatically.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [April 24, 2020, 5:27pm UTC](https://forums.swift.org/t/swift-package-installation/35728/11 "2020-04-24T17:27:36Z")

</div>

Furthermore, `cargo` will only install binaries, not libraries or other products, which I think is very reasonable as well. Installing those requires some level of familiarity with where things are supposed to go on the respective platform, and is a job better done using the respective package manager. That's even leaving out version conflicts etc.

I think this makes sense as a utility for packagers, but would be wary of having this be something end-users are expected or even recommended to use for installing things system-wide.

While other tools like pip, nvm or rubygems have similar capabilities, the communities around those languages have all developed tools like nvm, virtualenv or rvm to avoid actually installing anything system-wide, so I think defaulting to installing somewhere in the home directory (and probably warning against using this for system-wide installs) would be a happy medium.

---

<div class="post-metadata">

**Author:** ![NobodyNada](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobodynada/32/788_2.png) [@NobodyNada](https://forums.swift.org/u/NobodyNada)\
**Post date:** [April 24, 2020, 5:31pm UTC](https://forums.swift.org/t/swift-package-installation/35728/12 "2020-04-24T17:31:23Z")

</div>

> Brew doesn’t work on Linux unfortunately though.

Homebrew actually [does have Linux support](https://docs.brew.sh/Homebrew-on-Linux). (Disclaimer: I have never tried it so I don’t know how well it actually works.)

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [April 24, 2020, 5:35pm UTC](https://forums.swift.org/t/swift-package-installation/35728/13 "2020-04-24T17:35:48Z")

</div>

> [@ddunbar](#):
>
> What I’d really like to see here is something analogous to Nix ([https://nixos.org/](https://nixos.org/)).

Do we actually want to reinvent and reimplement a whole package manager (not dependencies only) inside SwiftPM? That sounds like a huge effort for something that would always be second-in-class to the platforms native package manager at best.

---

<div class="post-metadata">

**Author:** ![David\_Sweeris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_sweeris/32/31302_2.png) [@David\_Sweeris](https://forums.swift.org/u/David_Sweeris)\
**Post date:** [April 24, 2020, 5:58pm UTC](https://forums.swift.org/t/swift-package-installation/35728/14 "2020-04-24T17:58:10Z")

</div>

> [@ddunbar](#):
>
> Once that is done (or concurrently) I’d suggest the next step is to focus on a “sandboxed local” installation mode where those artifacts get dropped into -/.swiftpm/artifacts or something, with some helpers to get into PATH. What I’d really like to see here is something analogous to Nix ([https://nixos.org/](https://nixos.org/)).

10/10, would agree with again.

---

<div class="post-metadata">

**Author:** ![David\_Sweeris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_sweeris/32/31302_2.png) [@David\_Sweeris](https://forums.swift.org/u/David_Sweeris)\
**Post date:** [April 24, 2020, 6:27pm UTC](https://forums.swift.org/t/swift-package-installation/35728/15 "2020-04-24T18:27:57Z")

</div>

> [@ahti](#):
>
> > [@ddunbar](#):
> >
> > What I’d really like to see here is something analogous to Nix ([https://nixos.org/](https://nixos.org/)).
> 
> Do we actually want to reinvent and reimplement a whole package manager (not dependencies only) inside SwiftPM? That sounds like a huge effort for something that would always be second-in-class to the platforms native package manager at best.

Would it really be second-in-class, though? My experience with CentOS's native package manager is that it does at least as much harm as good when you're working on a project whose dependencies themselves depend on different versions of the same package. If we can combine the versioning info in `package.swift` files with Swift's resiliency features to solve at least a class of "dependency hell" problems, I'm very much in favor of it.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 24, 2020, 6:34pm UTC](https://forums.swift.org/t/swift-package-installation/35728/16 "2020-04-24T18:34:11Z")

</div>

Applying resilient builds to frameworks which don't support it is dangerous, leading to build and linker errors, as well as runtime crashes. Applying it as a blanket policy would be harmful. It's also not good for the ecosystem due to the severe limitations it imposes on libraries. So I don't think it's a path to what you want here.

---

<div class="post-metadata">

**Author:** ![David\_Sweeris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_sweeris/32/31302_2.png) [@David\_Sweeris](https://forums.swift.org/u/David_Sweeris)\
**Post date:** [April 24, 2020, 6:36pm UTC](https://forums.swift.org/t/swift-package-installation/35728/17 "2020-04-24T18:36:11Z")

</div>

> [@Jon\_Shier](#):
>
> Applying resilient builds to frameworks which don't support it is dangerous, leading to build and linker errors, as well as runtime crashes. Applying it as a blanket policy would be harmful. It's also not good for the ecosystem due to the severe limitations it imposes on libraries. So I don't think it's a path to what you want here.

Right, you'd have to take into account whether a library supports resiliency before including it in that "class" of problems which can be solved this way.

---

<div class="post-metadata">

**Author:** ![ddunbar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddunbar/32/1933_2.png) [@ddunbar](https://forums.swift.org/u/ddunbar)\
**Post date:** [April 24, 2020, 6:45pm UTC](https://forums.swift.org/t/swift-package-installation/35728/18 "2020-04-24T18:45:28Z")

</div>

I dunno. My main point was to separate out these two discussions to unblock forward progress.

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [April 24, 2020, 8:21pm UTC](https://forums.swift.org/t/swift-package-installation/35728/19 "2020-04-24T20:21:53Z")

</div>

If we decide to go ahead with this pitch, I think we should not reinvent the wheel but stick to what the expectations are for the given platform. For example, this part of the pitch

> this option by default would default to installing to the user home directory (for example on Linux, this would be in `$HOME` )

is not quite correct. For example, `cargo` does this thing where it creates its own little `.cargo` which is not appropriate; it should be following the XDG Base Directory specification. [https://github.com/rust-lang/cargo/issues/1734](https://github.com/rust-lang/cargo/issues/1734)

---

<div class="post-metadata">

**Author:** ![dwaite](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dwaite/32/1200_2.png) [@dwaite](https://forums.swift.org/u/dwaite)\
**Post date:** [April 25, 2020, 2:00am UTC](https://forums.swift.org/t/swift-package-installation/35728/20 "2020-04-25T02:00:32Z")

</div>

> [@ddunbar](#):
>
> I’m going to just not edit the deplorable typo out of this post

Which makes it a deployable typo?

[Next page](https://forums.swift.org/t/swift-package-installation/35728.md?page=2)
