Sub topic 10
Enabling Dev Hub
This chapter assumes you have a Developer Edition org you can log in to through My Domain. It also assumes you have authorized the Salesforce CLI (sf) against that org, as covered in earlier chapters, and that you can run commands in a terminal.
What Dev Hub is
Dev Hub is a feature of an org, not a separate org type. When it is enabled, the org becomes the parent for scratch orgs. A scratch org is a disposable Salesforce org that you create from a configuration file. The Dev Hub org records which scratch orgs it created, enforces limits on them, and lets you sign in to them from the CLI.
Dev Hub also supports second-generation packaging, which includes unlocked packages and second-generation managed packages. You do not need packaging to use scratch orgs. The same setting page turns on both.
Your Developer Edition org is a good Dev Hub because it is free, it does not expire while you keep using it, and it holds no production data.
Which orgs can be a Dev Hub
| Org type | Can be a Dev Hub? | Notes |
|---|---|---|
| Developer Edition | Yes | Lowest scratch org limits |
| Production (Enterprise, Unlimited, Performance) | Yes | Higher limits. Usually enabled by an admin. |
| Trailhead Playground | Yes | Useful for Trailhead modules that need one |
| Sandbox | No | Cannot create scratch orgs |
| Scratch org | No | A scratch org cannot create other scratch orgs |
Choose the org that will own your scratch orgs deliberately. Scratch orgs belong to the Dev Hub that created them, and you cannot move them. Teams usually use the production org as the Dev Hub. For learning, your Developer Edition org is the right choice.
Enable Dev Hub in Setup
You need the System Administrator profile, which your new Developer Edition user already has. The My Domain URL must also be deployed, which you did when you set up login.
- Log in to the org and open Setup.
- In Quick Find, type
Dev Huband select Dev Hub under Development. - Turn on Enable Dev Hub.
- Turn on Enable Unlocked Packages and Second-Generation Managed Packages. This is optional now, but it is easier to enable it at the same time.
The change takes effect straight away. You do not need to log out or wait for a deployment.
Make the CLI aware of the Dev Hub
Enabling Dev Hub in Setup does not change your CLI authorization. The CLI also needs to know which authorized org is the Dev Hub. You can mark it when you log in, or set it later.
To log in again and mark the org as the default Dev Hub, run this:
sf org login web --alias my-dev-org --set-default-dev-hubIf the org is already authorized, set the configuration value instead:
sf config set target-dev-hub=my-dev-orgBoth commands store the same setting, so you only need one of them. To scope the setting to the current project instead of your whole machine, add --global to neither command. The first command writes to the project's .sf/config.json, and sf config set --global writes to your home directory.
Check the result with:
sf org listThe Dev Hub org is marked with (D) in the first column of the output. Without a default Dev Hub, commands that create scratch orgs need --target-dev-hub my-dev-org on every call.
Scratch org limits in a Developer Edition Dev Hub
Each Dev Hub has two counters. The first is the number of scratch orgs that are active at the same time. The second is the number of scratch orgs you can create in a rolling 24-hour period.
| Dev Hub edition | Active scratch orgs | Created per day |
|---|---|---|
| Developer Edition | 3 | 6 |
| Enterprise, Unlimited, Performance | 40 | 80 |
Salesforce can change these numbers, so check the live values for your org. The CLI reports them:
sf org list limits --target-org my-dev-orgLook for the entries named ActiveScratchOrgs and DailyScratchOrgs. Each has a max value and a remaining value.
A scratch org counts against the active limit until it expires or you delete it. The default lifespan is 7 days and the maximum is 30. If you hit the active limit, delete a scratch org you no longer need:
sf org delete scratch --target-org my-scratch --no-promptDeleting a scratch org frees an active slot straight away. It does not give back the daily count, because that counter covers creations in a rolling 24-hour window.
Create a scratch org to prove it works
A scratch org needs a definition file. This is the smallest useful one. Save it as config/project-scratch-def.json:
{
"orgName": "Dev Hub Check",
"edition": "Developer"
}Then create the org from the same directory:
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias my-scratch \
--duration-days 7 \
--set-defaultOn a typical run, creation takes one to a few minutes. The command prints the scratch org ID and the generated username. Open it without a password:
sf org open --target-org my-scratchThe --set-default flag makes my-scratch the default target org, so later commands that omit --target-org will run against it. If you want your Developer Edition org to stay the default for queries, leave the flag out.
See what the Dev Hub knows
The Dev Hub stores a record for each scratch org in the ActiveScratchOrg object. You can query it with SOQL:
sf data query \
--target-org my-dev-org \
--query "SELECT SignupUsername, Status, ExpirationDate FROM ActiveScratchOrg"Run this against the Dev Hub, not against the scratch org itself. The object exists only in the Dev Hub. A related object, ScratchOrgInfo, holds the full history including expired orgs, and it is the object the CLI creates a record in when you request a new scratch org.
The Dev Hub UI shows the same data. In the app launcher, open Active Scratch Orgs or Scratch Org Infos, depending on your org's tabs.
Common errors and how to read them
Most problems come from one of three causes: the feature is off, the CLI points at the wrong org, or a limit is exhausted.
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `No default dev hub found` or a prompt for `--target-dev-hub` | The CLI has no default Dev Hub | Run `sf config set target-dev-hub=my-dev-org` |
| `This org is not enabled for Dev Hub` | Dev Hub is off, or the alias points at a different org | Check the toggle in Setup. Run `sf org list` and confirm the alias. |
| `LIMIT_EXCEEDED` or a message about active scratch orgs | You reached the active or daily limit | Delete unused scratch orgs. Wait for the daily window if you are at the daily cap. |
| Dev Hub toggle is missing in Setup | Wrong org type, such as a sandbox, or the user lacks permission | Use a Developer Edition, production, or Trailhead Playground org. Log in as an administrator. |
| Scratch org creation fails on a feature or setting | The definition file requests something the edition does not support | Remove the feature or setting. Add them back one at a time. |
Edge cases to know about
- Authorization differs from enablement. A colleague can enable Dev Hub in an org, but your CLI cannot use it until you authorize that org yourself.
- Several Dev Hubs on one machine. You can authorize more than one Dev Hub. Only one can be the global default, and a project can override it through its own config. If a project creates scratch orgs in the wrong place, check
sf config list. - Scratch orgs outlive the CLI alias. If you remove the local authorization with
sf org logout, the scratch org still exists in the Dev Hub and still counts toward your limits. Delete it withsf org delete scratchbefore you log out. - Inactivity still applies. Enabling Dev Hub does not change the Developer Edition inactivity rules. Log in regularly, as covered in the chapter on limits, or you may lose the Dev Hub along with the org.
Exercise
You enabled Dev Hub in Setup, but sf org create scratch asks for a --target-dev-hub value and fails without it. The org is authorized under the alias my-dev-org. What is the shortest fix that stops this happening on later commands, and how do you confirm it worked?
Show answer
Run sf config set target-dev-hub=my-dev-org. Then run sf org list. The Dev Hub org is marked (D) in the first column. Enabling Dev Hub in Setup only changes the org. The CLI separately needs a default Dev Hub setting, so it knew nothing about your choice until you set it.
Exercise
Your Developer Edition Dev Hub has created 3 scratch orgs and is still within its daily count. A fourth sf org create scratch call fails with a limit error. You delete one scratch org and immediately retry. Why might the retry succeed, and why might you still hit a limit later in the day?
Show answer
The failure came from the active scratch org limit of 3, and deleting a scratch org frees an active slot, so the retry can succeed. The daily limit of 6 counts creations in a rolling 24-hour period, and deleting does not give those back. After six creations you are blocked until older creations drop out of the 24-hour window, even if no scratch orgs are active.