Sub topic 10
Installing a package version into an org
This chapter covers how a built package version gets into a target org: the commands, the options that change behavior, and the errors you will meet. It assumes you can already create a package version (previous chapters) and use the Salesforce CLI with an authorized Dev Hub and target org.
What gets installed
You install a package version, not a package. Each version has a subscriber package version ID that starts with 04t. You can find it with sf package version list, or read it from the packageAliases section of sfdx-project.json. The CLI also accepts an alias such as [email protected], which maps to that ID.
Installation is a metadata deployment that Salesforce runs for you. The platform checks that every component in the version can be created or updated in the target org, then marks the components as belonging to the package. For an unlocked package, the components stay editable in the org unless you restrict them, but they are tracked as package members.
Where each version can go
Whether you can install a version depends on its state and the target org.
| Version state | Scratch org | Sandbox | Production |
|---|---|---|---|
| Beta (not promoted) | Yes | Yes | No |
| Released (promoted) | Yes | Yes | Yes |
A beta version is for testing. Production accepts only released versions. If you try a beta in production, the install fails with an error saying the version is not available for that org type. Promotion is covered in the next chapter.
The basic install command
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20--wait 20 makes the command poll for up to 20 minutes. Without it, the command returns right away with a request ID, and the install continues in the org. You can check on it later:
sf package install report --package-install-request-id 0Hf000000000001 --target-org MyProdOrgTo see what is already in an org:
sf package installed list --target-org MyProdOrgThis shows the package name, version number, and 04t ID for each installed package. Run it before and after an install to confirm what changed.
Options that change behavior
These flags are the ones you will use most often.
| Flag | Purpose |
|---|---|
| `--installation-key` | Required if the version was built with a key. Omit it if you built with `--installation-key-bypass`. |
| `--security-type` | `AdminsOnly` (default) or `AllUsers`. Controls who gets access to the package's objects and fields through profiles. |
| `--upgrade-type` | `Mixed` (default), `Delete`, or `DeprecateOnly`. Controls what happens to components removed from the package. |
| `--apex-compile` | `all` (default) compiles all Apex in the org after install. `package` compiles only the package's Apex. |
| `--no-prompt` | Skips confirmation prompts, which you need in automation. |
| `--publish-wait` | How long to wait for a new version to become available to the target org. |
Security type
AdminsOnly grants access only to administrators, so you then assign permission sets to everyone else. This is the safer default. AllUsers gives all internal profiles access to the package's objects and fields. Prefer AdminsOnly together with permission sets that ship in the package, because that keeps access explicit and reviewable.
Upgrade type
This matters when you install a newer version over an older one and the newer version no longer contains some component.
Mixeddeletes removed components where possible. Components that cannot be deleted, such as custom objects and fields with data, are deprecated instead.DeprecateOnlynever deletes. It marks removed components as deprecated.Deletedeletes all removed components. This can destroy data, so use it deliberately.
The next chapters on upgrades and removal go further. For now, treat Delete as a flag you rarely want.
Apex compile
After a package install, Salesforce recompiles Apex. The default all also compiles Apex that is not in the package. Unrelated compile errors elsewhere in the org can then fail your install. Setting --apex-compile package limits the check to the package's own classes and triggers, which is usually what you want in a shared org.
Installing with dependencies
Suppose Sales depends on Core at 1.0.0.LATEST. The dependency is resolved when you build the Sales version, and the specific Core version it was built against is pinned inside it. When you install Sales, the org must already have a compatible Core version.
Install in dependency order, base package first:
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20 --no-prompt
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20 --no-promptThe same order as in sfdx-project.json works well, which is why Core is listed first. If you install Sales first, the install fails with a message that a required package is missing. Do not rely on the platform to resolve dependencies for you when scripting. List each install explicitly.
The diagram shows the order and the points where an install can fail.
Installing from a URL
You can also install in a browser. The URL has this form:
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t000000000001For sandboxes, use test.salesforce.com instead of login.salesforce.com. This is convenient for hand-offs to administrators who do not use the CLI. The CLI is better for repeatable work, because the command can be recorded in a script and run the same way every time.
A typical sequence in a scratch org
For testing, install in a scratch org and then run your tests against the installed result.
sf org create scratch --definition-file config/project-scratch-def.json --alias CoreTest --set-default
sf package install --package "[email protected]" --target-org CoreTest --wait 20 --no-prompt
sf apex run test --target-org CoreTest --test-level RunLocalTests --wait 10Running tests on the installed version proves the artifact works, not only the source you built it from.
Common errors
- Version not found or not available. A new version takes a few minutes to replicate. Retry with
--publish-wait 10, or wait and run the command again. - Invalid installation key. The version was built with a key, and you omitted
--installation-keyor passed the wrong value. - Beta version in production. Promote the version first.
- Missing dependency. Install the dependency package at a compatible version before the dependent package.
- Apex compile failure after install. Look in the install report for the failing class. If it is outside the package, use
--apex-compile package. - Component conflict. The org already has a component with the same name that is not part of this package. Resolve the duplicate, by renaming or removing it, then reinstall.
Exercise
Exercise
Your team built [email protected], which depends on [email protected]. Your scripted install into a sandbox fails with a message that a required package is missing. List two things you check, and the order in which you install the packages after the fix.
Show answer
Check which packages are installed with sf package installed list --target-org <sandbox>, and check the version dependency of Sales, for example with sf package version report. If Core is absent or older than [email protected], install Core first. Then install Sales. The order is [email protected] and then [email protected], with the --wait flag set so the first install finishes before the second begins.
Exercise
Why might you add --apex-compile package to an install command in a sandbox shared by many teams?
Show answer
The default compiles all Apex in the org after the install. Compile errors in code owned by other teams could make your package install fail. With package, only the package's own Apex is compiled, so unrelated code does not block the install.