Sub topic 3
Setting up a Dev Hub
This chapter assumes you can use the Salesforce CLI (sf), have authorized an org from it before, and know what a scratch org is. By the end you will have a Dev Hub enabled, authorized from the CLI, and ready to hold packages.
What a Dev Hub is
A Dev Hub is a Salesforce org with the Dev Hub feature switched on. It does two jobs for package work:
- It is the system of record for your packages. When you run
sf package create, a package record is created in the Dev Hub. Every package version you build is also recorded there, together with its test results, coverage, dependencies and release status. - It is where scratch orgs are created from, and where namespaces are linked for namespaced packages.
The package contents are not stored in the Dev Hub. Your source stays in version control. The Dev Hub stores the package ID (starting 0Ho), the version IDs (starting 04t), and the metadata of each build.
The org where you install a package is separate from the Dev Hub. A package built through one Dev Hub can be installed into production orgs, sandboxes and scratch orgs that have no connection to it.
Choosing which org becomes the Dev Hub
You can enable Dev Hub in:
- A production org (the usual choice for teams).
- A Developer Edition org.
- A Trailhead Playground, for learning.
You cannot enable it in a sandbox or a scratch org.
The choice matters because enabling Dev Hub cannot be undone, and package records belong to that org. If you build packages in a throwaway Developer Edition org and later abandon it, you lose the package records and the ability to build new versions of those packages. Already-installed versions keep working. For a team, enable Dev Hub in production or in a long-lived org the company controls. Enabling it in production does not change production metadata, and it adds no cost by itself.
Check three things before you enable it:
- You have an administrator-level user in the org.
- Your edition supports Dev Hub. Scratch org and package limits vary by edition, so look up the current limits for yours.
- Your security team has agreed that production holds package records. Some companies prefer a dedicated Developer Edition org, and others require production.
Enabling Dev Hub
In the org you chose:
- Open Setup and search for Dev Hub.
- Turn on Enable Dev Hub.
- Turn on Enable Unlocked Packages and Second-Generation Managed Packages.
The second toggle is the one most often missed. Dev Hub can be enabled for scratch orgs only, and then package creation fails because the package feature is off. You need both switches.
Giving users permission
Being able to log in to the Dev Hub is not enough. A user who creates packages and versions needs the Create and Update Second-Generation Packages user permission. Add it to a permission set and assign that set to each developer or to the CI user.
Users who only create scratch orgs need access to the Dev Hub but not this package permission. Keep package creation rights for the people and automation that own releases.
Authorizing the Dev Hub from the CLI
Authorize the Dev Hub once per machine, with an alias and the default Dev Hub flag:
sf org login web --set-default-dev-hub --alias MyDevHubThis opens a browser. Log in as a user in the Dev Hub org. For a Trailhead Playground or Developer Edition org the login URL is the default. For a My Domain login or a sandbox-style URL, add --instance-url.
Verify the result:
sf org list
sf package list --target-dev-hub MyDevHubsf org list should show MyDevHub marked as the default Dev Hub. sf package list returns an empty list on a new Dev Hub, which shows that package access works. If it returns an error instead, see the table below.
You can also set the default explicitly:
sf config set target-dev-hub=MyDevHubWithout a default, every package command needs --target-dev-hub MyDevHub. The examples in this tutorial pass it explicitly so each command is self-contained.
In CI, avoid the browser flow. Use a JWT-based login with a connected app and a certificate, or an auth URL stored as a secret. Both can authorize the Dev Hub and mark it as the default.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A command says the org is not a Dev Hub, or no default Dev Hub is found | You authorized a non-Dev Hub org, or authorized without `--set-default-dev-hub` | Re-run the login against the Dev Hub org, or pass `--target-dev-hub` |
| `sf package create` fails because the feature is not enabled | **Enable Unlocked Packages and Second-Generation Managed Packages** is off | Turn it on in Setup |
| Package commands fail with a permissions error | The user lacks **Create and Update Second-Generation Packages** | Assign a permission set that includes it |
| You cannot find the Dev Hub setting in a sandbox | Sandboxes cannot be Dev Hubs | Enable it in production or a Developer Edition org |
| Scratch org creation works but package creation fails | Dev Hub is on, package feature is off, or permission is missing | Check both toggles and the permission |
| Login succeeds against the wrong org | Several aliases point at similar orgs | Run `sf org list` and check the username and org ID |
A frequent trap is a developer who authorizes their sandbox as the "default Dev Hub" because the flag was copied from an example. Package commands then fail in confusing ways. Always check sf org list after login.
Operating a Dev Hub
- Treat it as shared infrastructure. Everyone creating package versions writes to the same Dev Hub, so version numbers and package aliases are shared.
- Limit who can create packages. A stray
sf package createadds a permanent record, so restrict the permission. - Know the daily limits. Dev Hubs cap how many package versions you can create per day and how many scratch orgs are active at once. Check the current limits for your edition, and plan CI so that every commit does not build a version.
- Keep one Dev Hub per company or business unit unless you have a strong reason to split. Dependencies between packages work only when the packages are in the same Dev Hub, so splitting makes cross-package dependencies harder.
The next chapters use MyDevHub as the alias in every command. Make sure sf package list --target-dev-hub MyDevHub runs without error before you move on.
Exercise
A teammate enabled Dev Hub in a full-copy sandbox so they could experiment with packages. They can't find the setting. Separately, your Dev Hub in production is enabled, but sf package create fails for a new developer while sf org create scratch works for them. Explain both problems and the fixes.
Show answer
Sandboxes can't be Dev Hubs, so the setting isn't available there. The teammate should use the production org, a Developer Edition org, or a Trailhead Playground as the Dev Hub. For the developer, scratch org creation only needs Dev Hub access. Package creation also needs the "Enable Unlocked Packages and Second-Generation Managed Packages" setting to be on, and the user must have the "Create and Update Second-Generation Packages" permission. Since the same Dev Hub works for others, the most likely cause is the missing permission, so assign a permission set that contains it.
Exercise
Why does the choice of Dev Hub org matter more than it first appears, and which org would you pick for a team?
Show answer
Enabling Dev Hub can't be undone, and the package and version records live in that org. If the org is discarded, you can't create new versions of those packages. A team should pick a long-lived org the company controls, usually production, or a dedicated Developer Edition org if policy requires it.