Sub topic 1
Package types: unlocked, managed, and unmanaged
Every package version you create, install, or depend on belongs to a package type. The type decides who can edit the components, whether the code is visible to subscribers, and whether versions can be upgraded. This chapter covers the package types and how each one handles versions. Later chapters build on this: version numbers, sfdx-project.json, and dependencies.
Prerequisites
This chapter assumes you have:
- Basic Salesforce platform concepts: orgs, metadata, and Apex.
- A working Salesforce DX project and the
sfCLI. - A basic understanding of scratch orgs and Dev Hubs.
- Comfort reading JSON configuration files.
The four kinds of package
Salesforce distributes metadata in four kinds of package. They differ along two axes: the packaging generation and the control the package gives you.
- Unlocked package. A second-generation package (2GP). You build it from source in a Dev Hub. Subscribers can see and change its components.
- Second-generation managed package. Also a 2GP. It requires a namespace, hides most Apex source from subscribers, and is meant for distribution to other companies.
- First-generation managed package (1GP). A managed package created and uploaded through the UI of a special packaging org. 1GP is the older model.
- Unmanaged package. A 1GP container that copies metadata into the target org. After installation, nothing ties the components back to the package.
"Second-generation" means the package is defined by source files in your project and built with the CLI. "First-generation" means the package is defined by components you add in a packaging org, and the org is the source of truth.
Unlocked packages
An unlocked package is the usual choice for packaging your own company's metadata. It suits teams that want modular, versioned deployments instead of one large unpackaged source tree.
Key properties:
- Created from source. The
pathinsfdx-project.jsonpoints to a directory, and the package contains what that directory holds. - Namespace optional. You can create one with no namespace or with a namespace.
- Editable in subscriber orgs. An admin can change an installed component. The next upgrade overwrites changes to components that the package owns.
- Versioned with four parts. Versions use
MAJOR.MINOR.PATCH.BUILD. A version starts as a beta and can be promoted to released. - Upgradeable and uninstallable. Installing a later version upgrades the earlier one. Uninstalling removes the package's components from the org.
- Source stays visible. Subscribers can read all Apex in the package.
Create an unlocked package with the CLI:
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHubThe command registers the package with the Dev Hub and returns a package ID that starts with 0Ho. It also adds an alias for that ID to packageAliases in sfdx-project.json.
Second-generation managed packages
A 2GP managed package is meant for software you sell or distribute to orgs you don't control, such as AppExchange products. It uses the same source-driven workflow as an unlocked package, with three differences.
- A namespace is required. The namespace is a short prefix that Salesforce adds to your component names. You register it in a namespace org and link it to the Dev Hub. Then you set
namespaceinsfdx-project.json. - Code is protected. Subscribers can't read the Apex source of non-global code. Only elements marked
globalform the public API that other code can call. - Compatibility rules are stricter. Once a version is released, you can't make changes that would break subscribers. For example, you can't remove a
globalApex method that earlier versions exposed.
The project file for a managed package sets the namespace at the top level:
{
"packageDirectories": [
{
"path": "force-app",
"package": "Acme Core",
"versionName": "Acme Core 1.0",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {}
}Create the package with the Managed type:
sf package create --name "Acme Core" --package-type Managed --path force-app --target-dev-hub myDevHubBoth Unlocked and Managed are fixed at creation time. You can't convert an unlocked package to a managed one later. To change type, create a new package.
First-generation managed packages
A 1GP managed package is built in a packaging org. You add components through Setup, upload a version, and the upload becomes the installable artifact. Many AppExchange products still use this model, so you will meet it when you depend on or migrate from existing packages.
How versions differ from 2GP:
- 1GP versions use
MAJOR.MINORfor ordinary releases. A patch number appears only when you create a patch version through a patch org. - There is no build number you control. Each upload is a manual step in the UI or API.
- The packaging org holds the package definition. Source control does not.
If you start a new project, prefer 2GP. Move an existing 1GP only when you have a plan for it, because 1GP and 2GP packages are separate lineages. A 2GP version can't upgrade a 1GP installation directly. You need a documented migration path.
Unmanaged packages
An unmanaged package is a one-time delivery mechanism. Installing it copies the components into the target org. After that:
- The subscriber owns every component and can edit or delete it.
- The org doesn't track the components as belonging to a package.
- Installing a newer upload doesn't upgrade anything. It doesn't remove components that the newer version dropped.
- Dependencies between unmanaged packages are not enforced after install.
Unmanaged packages work for sample code, templates, and starter configuration that the recipient is expected to change. Don't use them for anything you need to update later. Their uploads have version numbers, but the numbers don't drive upgrades or dependency checks.
Comparing the types
| Property | Unlocked (2GP) | Managed (2GP) | Managed (1GP) | Unmanaged |
|---|---|---|---|---|
| Defined by | Source in a project | Source in a project | Packaging org | Packaging org |
| Dev Hub needed | Yes | Yes | No | No |
| Namespace | Optional | Required | Required | Not applicable |
| Version format | MAJOR.MINOR.PATCH.BUILD | MAJOR.MINOR.PATCH.BUILD | MAJOR.MINOR (patches by patch org) | Upload only, no upgrade path |
| Upgrade supported | Yes | Yes | Yes | No |
| Subscriber can edit components | Yes | Limited | Limited | Yes, full ownership |
| Apex source visible | Yes | Hidden for non-global code | Hidden for non-global code | Yes |
| Typical use | Internal modular deployment | Commercial distribution | Existing AppExchange products | Samples and templates |
Choosing a type
Use these questions in order:
- Will the recipient ever need an update from you? If not, an unmanaged package is enough.
- Is the package for your own orgs, or for a team that you work with directly? Choose unlocked.
- Will other companies install it, and do you need to protect the source or control the API surface? Choose 2GP managed.
A common pattern is to put shared utilities in an unlocked package with no namespace, and the application that depends on them in another unlocked or managed package. Later chapters show how to declare that dependency.
Common mistakes
- Choosing unmanaged for convenience. It works for a first install and then leaves you with no upgrade path. Moving to a versioned package later means reconciling components that already exist in the org.
- Using a namespace without a reason. A namespace on an unlocked package changes component API names and affects every reference to them. Add one only when you need it. You can't remove a namespace from a package that already has released versions.
- Assuming unlocked means locked down. Subscribers can edit unlocked components, and a later upgrade replaces those changes. Treat the package source as the one place to make changes.
- Mixing generations. A 2GP package can't upgrade a 1GP installation. Plan a migration instead of expecting an upgrade to work.
Summary
Unlocked packages give you versioned, source-driven deployment for your own orgs. Second-generation managed packages add a namespace, code protection, and strict compatibility rules for distribution. First-generation managed packages are the older, packaging-org-based form of the same idea. Unmanaged packages copy metadata once and carry no upgrade path. The next chapter looks at the four-part version number that 2GP packages use.
Exercise
Your team wants to package shared Apex utilities for use across your company's own production and sandbox orgs. The code can be visible to admins, and you want upgrades to replace older versions. Which package type fits, and does it need a namespace?
Show answer
An unlocked package fits, because it is versioned and upgradeable and its source is visible to subscribers. It does not need a namespace. You can create it with --no-namespace, and you should add one only if you have a specific reason such as avoiding name collisions.
Exercise
A colleague says they installed version 2 of an unmanaged package over version 1 and expects the removed Apex class to disappear from the org. Will it, and why?
Show answer
No. Installing an unmanaged package copies components into the org and does not upgrade. The org doesn't track the components as belonging to the package, so components that version 2 dropped stay in the org. Use a versioned package type if you need removals to propagate.