Sub topic 10
Enabling Dev Hub
This chapter assumes you have a Developer Edition org that you can log in to, and that the Salesforce CLI is installed. It also assumes you know your user has the System Administrator profile, which a new Developer Edition user does.
What Dev Hub is
Dev Hub is a feature you switch on in one org. That org then becomes the parent of your scratch orgs. A scratch org is a short-lived, disposable org that you create from a configuration file. The Dev Hub org records which scratch orgs exist, who created them, and how much of your allowance you have used.
Dev Hub also manages second-generation packages and namespaces. This tutorial only needs it for scratch orgs.
You enable Dev Hub in the org you will keep, not in the scratch orgs. Scratch orgs and sandboxes cannot be Dev Hubs. Your Developer Edition org is a good choice because it does not expire the way a scratch org does.
Turn it on in Setup
- Log in to your org through its My Domain URL.
- Click the gear icon and choose Setup.
- In Quick Find, type
Dev Huband open Development > Dev Hub. - Switch Enable Dev Hub to on.
- Optionally switch on Enable Unlocked Packages and Second-Generation Managed Packages. You do not need it for scratch orgs alone.
Make the CLI use the Dev Hub
The CLI must know which org is the Dev Hub. If you authorized the org earlier, log in again and mark it as the default Dev Hub:
sf org login web --alias my-dev-org --set-default-dev-hubThen list your orgs:
sf org listThe Dev Hub org shows a mark in the default column. You can also set it without logging in again:
sf config set target-dev-hub=my-dev-orgCheck your scratch org allowance
Developer Edition orgs have small scratch org limits. Enterprise and Unlimited Edition Dev Hubs have much higher limits. Read your actual numbers from the org instead of relying on a table:
sf limits api display --target-org my-dev-orgLook for ActiveScratchOrgs and DailyScratchOrgs in the output. Each row shows the maximum and the amount remaining. Deleting a scratch org frees an active slot but does not give back the daily count.
Create a first scratch org
A scratch org is described by a JSON file. Save this as config/project-scratch-def.json:
{
"orgName": "My Scratch Org",
"edition": "Developer"
}Then create the org:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --duration-days 7
sf org open --target-org my-scratchThe duration can be 1 to 30 days. After that time Salesforce deletes the org.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Dev Hub option is missing in Setup | You are not in the Developer Edition org, or your user lacks admin rights | Log in as the System Administrator of the right org |
| `No default dev hub found` | The CLI does not know which org is the Dev Hub | Run `sf config set target-dev-hub=my-dev-org` |
| `The signup request failed` or a limit error | You used up the active or daily allowance | Delete old scratch orgs with `sf org delete scratch`, or wait a day |
| Scratch org creation fails right after enabling | The change has not finished propagating | Wait a few minutes and retry |
Exercises
Exercise
You enabled Dev Hub in your Developer Edition org, but sf org create scratch reports that no default Dev Hub was found. What are two ways to fix this?
Show answer
Run sf org login web --alias my-dev-org --set-default-dev-hub to authorize the org again and mark it as the Dev Hub. Or run sf config set target-dev-hub=my-dev-org for an org you already authorized. You can also pass --target-dev-hub my-dev-org on the create command.
Exercise
Which command shows how many scratch orgs you can still create today, and which two limit names should you read in its output?
Show answer
sf limits api display --target-org my-dev-org. Read ActiveScratchOrgs and DailyScratchOrgs.