Sub topic 9
Installing a package version into an org
This chapter covers installing a package version with the sf CLI, what happens during the install, and how to confirm the result. It assumes you can already create a package version (earlier chapters) and that you have an authorized target org. It also assumes you know the difference between a beta and a released version.
What an install needs
You install one specific package version. Every version has a subscriber package version ID that starts with 04t. You can pass the ID directly, or pass an alias from the packageAliases section of sfdx-project.json, such as [email protected]. Aliases only resolve when you run the command inside the project directory.
The install request is checked against four things:
- The version's status. Released versions install anywhere. Beta versions install only in scratch orgs and sandboxes.
- The target org. It must be able to receive the package. For example, an org-dependent unlocked package needs the org features it relies on.
- The package's dependencies. They must already be installed at a compatible version.
- The installation key, if the version was created with one.
The basic command
The command below installs a released version into an org with the alias myOrg.
sf package install --package "[email protected]" --target-org myOrg --wait 10 --publish-wait 5 --no-promptThe flags in this command do the following:
--packagetakes a04tID or an alias.--target-orgselects the org by username or alias.--waitis the number of minutes the CLI waits for the install to finish.--publish-waitis the number of minutes the CLI waits for the package to become available to the org. It helps right after you promote or create a version, because the version may not be visible in the org yet.--no-promptskips the confirmation prompt. It suits scripts and CI jobs, and you should leave it off when you run installs by hand.
If --wait expires before the install finishes, the install keeps running in the org. The CLI prints a request ID that starts with 0Hf. You can check on it later:
sf package install report --request-id 0Hf000000000001AAA --target-org myOrgOptions that change the result
The table lists the options you will use most often.
| Flag | Values | Effect |
|---|---|---|
| `--installation-key` | The key string | Required if the version was created with a key. A version created with `--installation-key-bypass` needs none. |
| `--security-type` | `AdminsOnly` (default), `AllUsers` | Sets which profiles get access to the package's objects and pages. |
| `--upgrade-type` | `Mixed` (default), `DeprecateOnly`, `Delete` | Unlocked packages only. Controls what happens to components that were removed from the new version. |
| `--apex-compile` | `all` (default), `package` | Unlocked packages only. `package` compiles only the package's Apex, which is faster in orgs with a lot of other Apex. |
The next command installs a version that has an installation key, gives all users access, and compiles only the package's Apex.
sf package install --package "[email protected]" --target-org myOrg --installation-key "s3cretKey" --security-type AllUsers --apex-compile package --wait 20AdminsOnly is the safer default because it does not widen access without a deliberate choice. Pass AllUsers only when the package's users really need immediate access.
Upgrade types
When you install a newer version over an older one, components that existed in the old version and are missing in the new one need a rule. --upgrade-type supplies it:
Mixeddeletes removed components, except for those that cannot be deleted. Those are deprecated instead.DeprecateOnlymarks every removed component as deprecated and deletes none.Deletedeletes all removed components.
Use DeprecateOnly when you are not sure whether anything outside the package still references a removed component. Deleting a component is hard to undo, and deprecating it is not.
The install flow
The diagram shows the decisions the platform makes after you submit an install.
Installing from a URL
You can also install without the CLI. Build a URL from the version ID and open it in a browser while logged in to the org:
- Production and developer orgs:
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t000000000001AAA - Sandboxes:
https://test.salesforce.com/packaging/installPackage.apexp?p0=04t000000000001AAA
This is useful for sharing a package with an admin who does not use the CLI. The install page also lets that admin choose who gets access. It does not fit automation, because you cannot script the clicks reliably.
Verifying and removing
After an install, list what the org has:
sf package installed list --target-org myOrgThe output shows each package's name, its version number, and the 04t ID of the installed version. Compare the ID with the one you meant to install. This catches the case where an alias pointed to a different version than you expected.
To remove a package:
sf package uninstall --package "[email protected]" --target-org myOrg --wait 10Uninstalling fails if something in the org still depends on the package. Another installed package that declares a dependency on it counts, as does a custom field or Apex class in the org that references its components. Remove or change the dependents first. Uninstalling an unlocked package deletes its components, so check for data first. Deleting a custom object deletes its records.
Installing in a project workflow
A common pattern is to install the dependencies from your own project into a scratch org before you push source. The order matters here: install Utils first, and install Sales App only after that.
sf org create scratch --definition-file config/project-scratch-def.json --alias scratch1 --set-default --target-dev-hub myDevHub
sf package install --package "[email protected]" --target-org scratch1 --wait 20 --no-prompt
sf project deploy start --target-org scratch1If you try the second step with a package whose own dependency is missing, the install fails and reports the missing package. A later chapter on install order covers this in detail. For now, remember that an install checks the org as it is at that moment. It does not install anything for you.
Common problems
- The version is not found. The package version was created very recently and has not propagated to the org. Add or increase
--publish-wait, or retry after a few minutes. - Beta version rejected in production. Promote the version first with
sf package version promote. Promotion is permanent for that version number, so a fix needs a new version. - Wrong or missing installation key. The error mentions the key. Check whether the version was created with
--installation-keyor--installation-key-bypass. - Install times out. The CLI stopped waiting, but the org may still be working. Run
sf package install reportwith the request ID before you start a second install. - Apex test or compile failures in the org. Existing org code that references package components can break during an upgrade. Try
--apex-compile packageto see whether the package itself compiles, then fix the org code that failed.
Summary of what to check
Before an install, confirm the version status, the target org type, the key, and the dependencies. After it, list the installed packages and compare IDs. If something goes wrong, the request ID from the install lets you read the final status and error text.
Exercise
You promoted a version of Utils an hour ago. Your CI job runs sf package install --package "[email protected]" --target-org prod --wait 10 and fails with a message that the package version cannot be found. Name one flag you would add, and explain why the install might also fail if you had skipped the promotion step.
Show answer
Add --publish-wait (for example --publish-wait 10) so the CLI waits for the version to become available to the org. Without promotion, the version would still be a beta, and beta versions can be installed only in scratch orgs and sandboxes, not in a production org.
Exercise
An install into a sandbox with --wait 5 ends with a timeout message and a request ID starting with 0Hf. What should you do before running the install again, and which command do you use?
Show answer
Check the status of the original request, because the install may still be running or may already have finished. Run sf package install report --request-id <the 0Hf ID> --target-org <sandbox alias>. Install again only if the report shows the request failed.
Exercise
You are upgrading an unlocked package in an org where some unmanaged Apex might still reference a component that the new version removed. Which --upgrade-type is the safest choice, and why?
Show answer
DeprecateOnly. It marks the removed components as deprecated and deletes none of them, so references from other code keep working. Delete and Mixed can delete removed components.