Sub topic 3
Setting up a Dev Hub
This chapter assumes you know Salesforce metadata, the Salesforce CLI (sf), and scratch orgs. A Dev Hub is the org that owns your packages. You create packages and package versions against it, and the CLI needs to be logged in to it before you can do either.
What a Dev Hub does
A Dev Hub is a regular Salesforce org with the Dev Hub feature switched on. It stores three kinds of records:
- Package records, created with
sf package create. - Package version records, created with
sf package version create. - Scratch org records, which you may already know from daily development.
Package ownership lives in the Dev Hub, not in the orgs where you install packages. Install targets only need the packages installed.
Enable the Dev Hub
Use a production org or a Developer Edition org as your Dev Hub. You cannot enable it in a sandbox. Many teams use their production org, because that keeps package records next to the org that receives the released versions.
- Log in to the org as a user with the System Administrator profile, or with equivalent permissions.
- Open Setup and search for Dev Hub.
- Turn on Enable Dev Hub.
- If your org supports it, also turn on Enable Unlocked Packages and Second-Generation Managed Packages.
Once enabled, you cannot disable the Dev Hub.
Authorize the Dev Hub in the CLI
Log in and give the org an alias:
sf org login web --alias MyDevHub --set-default-dev-hubThe --set-default-dev-hub flag makes this org the default for commands that take --target-dev-hub. The examples in this tutorial pass the flag explicitly so each command is clear on its own.
Check the result:
sf org listThe Dev Hub appears with a marker in the default column. If you can't see it, repeat the login and confirm you chose the right org.
Permissions
The user who creates packages needs access to the Dev Hub features. A system administrator has this by default. For other users, assign a permission set that includes these permissions:
| Permission | Why it is needed |
|---|---|
| Create and Update Second-Generation Packages | Run `sf package create` and `sf package version create` |
| Create and Update Unlocked Packages | Required where the org shows this separate permission |
| Download AppExchange Packages | Not needed for unlocked packages |
Permission names vary by release, so check the names shown in your org.
Common errors
- "This command requires a Dev Hub": the
--target-dev-hubalias points to an org where Dev Hub is not enabled. Runsf org listand check the alias. - No default Dev Hub: you did not use
--set-default-dev-hub. Either runsf config set target-dev-hub=MyDevHubor pass--target-dev-hubon each command. - Insufficient access when creating a package: the logged-in user lacks the package permissions above.
Exercise
You enabled Dev Hub in a sandbox to test packaging, but the setting is missing. Why, and what should you do?
Show answer
Dev Hub can only be enabled in a production org or a Developer Edition org, not in a sandbox. Enable it in production or in a Developer Edition org, then log in with sf org login web --alias MyDevHub --set-default-dev-hub.