Sub topic 11
Installing a Version in Sandboxes and Production
This chapter covers installing a second-generation managed package (2GP) version into a target org. It assumes you can already build a package version and know the difference between beta and released versions. You should also be comfortable with Salesforce DX aliases, sfdx-project.json, and org authorization with the Salesforce CLI.
What installation does
Installing a package version copies its metadata into a subscriber org as managed, namespaced components. The subscriber org gets the components, but it can't edit the protected ones. A package version is immutable, so you never edit an installed version. To change the package, you build a new version and install or upgrade to it.
Every install has three parts:
- A package version, identified by an alias (such as
[email protected]) or by its 04t ID. - A target org, which can be a scratch org, sandbox, Developer Edition org, or production org.
- Optional settings: the installation key, security type, upgrade type, and API access.
Where each version can be installed
Whether a version can be installed depends on whether it is beta or released.
| Version state | Scratch org | Sandbox | Production |
|---|---|---|---|
| Beta | Yes | Yes | No |
| Released | Yes | Yes | Yes |
A beta version is for testing only. Salesforce blocks beta versions from production orgs. Promote a version before you install it in production. Promotion is covered in the previous chapter. A released version can't be un-promoted, so test beta versions in a sandbox first.
Installing a beta version into a sandbox is the usual way to rehearse a production release. It shows you how the package behaves in an org with real data volumes, existing customizations, and other installed packages.
Install with the CLI
Use sf package install. The command from the chapter plan installs a version by alias into a sandbox:
sf package install --package [email protected] --target-org my-sandbox --wait 10Each option does a specific job:
--packagetakes a package version alias fromsfdx-project.json, or a 04t ID. An alias is easier to read and keeps scripts stable.--target-orgtakes the alias or username of an org you have already authorized.--wait 10makes the command wait up to 10 minutes. Without it, the command returns while the install is still running, and you must check its status yourself.
If you created the version with an installation key, add --installation-key. If you used --installation-key-bypass as in the earlier build command, no key is needed.
A fuller install for a production-like target might look like this:
sf package install \
--package [email protected] \
--target-org my-production \
--wait 20 \
--publish-wait 10 \
--security-type AdminsOnly \
--no-promptThe options added here matter in practice:
--publish-waitwaits for the package version to become available in the target org's region after promotion. Newly released versions can take a few minutes to become visible.--security-typesets who gets access to the package's custom objects and Apex classes.AllUsersgrants access to all users, andAdminsOnlygrants access only to administrators.--no-promptskips the confirmation prompt, which is needed for unattended scripts. Use it only where you intend to bypass the prompt.
If you don't pass --security-type, the CLI uses AdminsOnly.
Checking install status
When you install with a short or missing --wait, check the request later. List the installed packages in the org to confirm the result:
sf package installed list --target-org my-sandboxThe output shows each installed package with its version number and ID. Use it to confirm which version a sandbox is running before you test an upgrade.
Install by URL
Not everyone installing your package uses the CLI. Customers and testers can install a version from a browser. Every version has a 04t ID, and the install URL has this form:
https://login.salesforce.com/packaging/installPackage.apexp?p0=04tXXXXXXXXXXXXXXXUse https://test.salesforce.com/packaging/installPackage.apexp?p0=04tXXXXXXXXXXXXXXX to install into a sandbox. The user logs in, picks who gets access, and starts the install. This is the form you give customers, and it is the same one used for AppExchange installs.
Find the 04t ID with:
sf package version list --packages AcmeApp --target-dev-hub devhubThe install flow
Installation involves your Dev Hub only when you build the version. The subscriber org installs from Salesforce's package store.
Installing during an upgrade
If the target org already has an earlier version, installing a later version upgrades it. Upgrade works only when the new version's ancestry is compatible with what's installed. The chapter on ancestors covers that rule. If the ancestry doesn't match, the install fails, and you have to build a version with the correct ancestor.
Managed-package rules protect subscribers during an upgrade. Already-released components such as global Apex classes and custom objects can't be deleted or have their types changed. The upgrade keeps subscriber data and subscriber configuration in place.
Dependencies are installed first
If your package depends on another package, the dependency must be in the target org before yours. You can install the dependencies manually in order, or let the CLI do it. The sf package install command in recent CLI versions installs dependencies declared on the version. In older workflows, you install each dependency yourself, base package first. Check your CLI version if the order seems wrong.
If a dependency is missing or too old, the install fails with an error naming the required package version. Install the required version, then retry.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Install of a beta version fails in production | Beta versions can't be installed in production | Promote the version, then install the released version |
| "Invalid package ID" or version not found | Wrong alias, or the version hasn't propagated yet | Check the alias with `sf package version list`, or use `--publish-wait` |
| Installation key error | The version was built with a key | Pass `--installation-key` with the correct key |
| Missing dependency error | A required package isn't installed | Install the dependency version first |
| Upgrade fails on ancestry | The installed version isn't in the new version's ancestry | Build a version with a compatible ancestor |
| Users can't see the package's objects | Security type was `AdminsOnly` | Grant permission sets, or reinstall with `AllUsers` where appropriate |
| Install times out | `--wait` was too short | Check `sf package installed list`, then rerun with a longer wait |
Uninstalling
You can uninstall a package with sf package uninstall. Uninstalling removes the package's components and their data. Salesforce keeps an export of the package's data for a period after uninstall, but you shouldn't rely on that as a backup. Uninstall in a sandbox first. A package can't be uninstalled while other components or packages depend on it.
Practical checklist
- Authorize the target org and give it an alias.
- Confirm the version alias or 04t ID with
sf package version list. - For production, confirm the version is released.
- Install any dependencies the target org lacks.
- Run
sf package installwith a--waitlong enough for the install. - Confirm the result with
sf package installed list. - Assign permission sets and run smoke tests.
Exercise
Your team built version 1.0.0-2 of AcmeApp, and it passed tests in a scratch org. A teammate runs sf package install --package [email protected] --target-org my-production and it fails. What is the most likely cause, and what should you do before you retry?
Show answer
The version is still a beta, and beta versions can't be installed in production. Install the beta version in a sandbox and test it there. Then run sf package version promote --package [email protected] and retry the production install, using --publish-wait if the released version isn't visible yet.
Exercise
You run sf package install with --wait 2 for a large package, and the command returns before the install finishes. How do you find out which version the org is running?
Show answer
Run sf package installed list --target-org <alias>. It lists the installed packages and their version numbers. If the install hasn't finished, rerun the install with a longer --wait.