This guide walks through a second-generation managed package (2GP), the Salesforce package type intended for distributing an app to customer orgs. You will create a Dev Hub, set aside a namespace, build the app in a local Salesforce DX project, create a version, test it in a clean org, and publish a Salesforce install link on your website.
What you need: a computer that can run VS Code and Salesforce CLI, internet access, and a Salesforce login. The instructions use free Developer Edition and scratch orgs for learning and testing.
Choose and create your Dev Hub org
- Sign up for a free Salesforce Developer Edition org. This will be your learning Dev Hub.
- In that org, open Setup and search for Dev Hub.
- Select Enable Dev Hub, then enable Unlocked Packages and Second-Generation Managed Packages.
- Keep the org active and secure. It owns every managed 2GP package created from it, and enabling these settings cannot be undone.
Salesforce currently limits a Developer Edition Dev Hub to six scratch orgs and package versions per day and three active scratch orgs. Developer Edition orgs can expire after inactivity; if the Dev Hub expires, you can lose access to its packages and package updates.
Reserve a namespace
- Sign up for a second free Developer Edition org to reserve the namespace.
- In the namespace org, go to Setup → Package Manager → Namespace Settings → Edit.
- Enter an available namespace and save it.
- In your Dev Hub, open Setup → Namespace Registry and link the namespace to the Dev Hub.
- Keep the namespace org login. Choose the namespace before creating a package; you cannot change the namespace on the package later.
For a managed package, this prefix becomes part of the names of your custom Salesforce components. Use a name you are comfortable keeping long term.
Install the free development tools
- Download and install Visual Studio Code for Windows, macOS, or Linux.
- Install the Salesforce CLI.
- In VS Code, open Extensions and install Salesforce Extension Pack from the Marketplace.
- Open VS Code’s integrated terminal and verify that the CLI installed:
sf --versionOfficial installer page
Choose the download that matches your computer. The page illustration links to Microsoft’s public download page; choose the installer for your operating system there.
VS Code’s Salesforce extensions use Salesforce CLI behind the scenes, so install the CLI even if you plan to use VS Code menus for most tasks.
Create the local Salesforce DX project
- In VS Code, open the Command Palette and run SFDX: Create Project.
- Choose Standard, enter a project name, and select a folder.
- Alternatively, use these terminal commands:
sf project generate --name MySalesforceAppcd MySalesforceApp- Open
sfdx-project.jsonand add your namespace as a top-level property, preserving the rest of the file:
"namespace": "your_namespace"Replace your_namespace with the namespace you reserved. A DX project contains your source files; it is not itself the installable package.
Authorize the Dev Hub from the CLI
- From the Salesforce DX project folder, start the browser-based Dev Hub sign-in:
sf org login web --alias DevHub --set-default-dev-hub- In the browser, log in to the Dev Hub and approve Salesforce CLI access.
- Confirm that the org is authorized:
sf org listCreate a development scratch org
- Use the project’s
config/project-scratch-def.jsonfile. It defines the scratch org edition and features. - Create a temporary development org:
sf org create scratch --definition-file config/project-scratch-def.json --alias DevScratch --target-dev-hub DevHub --duration-days 30 --wait 10 --set-default- Open the new org:
sf org open --target-org DevScratchScratch orgs are disposable. Their availability is subject to the Dev Hub’s limits, and each scratch org expires after its selected duration.
Build the app and keep its source
- Build the objects, fields, flows, Apex, Lightning components, and other features that make up your app.
- You can author source in VS Code and deploy it to the development scratch org, or build in the org and retrieve its metadata into the project.
- Keep the components you intend to distribute in the package directory, normally
force-app. - Leave unrelated setup, test data, and experimental components out of the package.
- For an installable result, test dependencies and permissions as well as the feature itself.
The project files become the input for package creation. Keep them in source control when you are ready to maintain versions over time.
Create the managed package
- From the project directory, create the managed package container:
sf package create --name MySalesforceApp --package-type Managed --path force-app --target-dev-hub DevHub- Save the package ID and alias returned by the CLI.
- Review
sfdx-project.json; Salesforce adds the package details there. - Confirm the package name, namespace, and managed type before moving on.
The package’s namespace and type cannot be changed after the package is created.
Build a beta package version
- In
sfdx-project.json, set the package version number, for example1.0.0.NEXT, in the package directory entry. - Create a beta version:
sf package version create --package MySalesforceApp --installation-key-bypass --code-coverage --wait 30 --target-dev-hub DevHub- The CLI returns a package-version ID beginning with
04t. Save it; it identifies this exact installable snapshot. - If the command returns a creation request ID beginning with
08cbefore it finishes, check the request:
sf package version create report --package-create-request-id 08cXXXXXXXXXXXX--installation-key-bypass means an installer does not need a separate package password. If you want to restrict installation, use an installation key instead. The --code-coverage flag validates package coverage for release. If the package contains Apex, its tests must meet Salesforce’s minimum coverage requirement before promotion.
Install and test in a clean target org
- Create a second scratch org with no namespace to represent a clean subscriber org:
sf org create scratch --definition-file config/project-scratch-def.json --alias SubscriberTest --target-dev-hub DevHub --duration-days 30 --no-namespace --wait 10- Install the beta version using its
04tID:
sf package install --package 04tXXXXXXXXXXXX --target-org SubscriberTest --wait 30 --no-prompt- Open the test org, assign the required permission set, and test the app as a normal user.
- Check that the package installs cleanly and that the expected setup, permissions, and features work.
- Fix problems in the source and create a new version to retest.
Beta versions can be installed in scratch orgs and sandboxes, but not production orgs. Use a disposable target for beta testing.
Release the version and publish its link
- When the clean-org test passes, promote the tested version from beta to released:
sf package version promote --package 04tXXXXXXXXXXXX --target-dev-hub DevHub- Confirm the version is released. Promotion is permanent for that package version.
- In the Dev Hub, copy your My Domain hostname, then replace the placeholders in this URL with your Dev Hub My Domain and the released version’s
04tID:
https://YOUR-DEV-HUB-MY-DOMAIN.lightning.force.com/packagingSetupUI/ipLanding.app?apvId=04tXXXXXXXXXXXX- Open the link while signed in to
SubscriberTestto verify that Salesforce opens the installer for the intended package. - Put that URL behind an Install in Salesforce button on your website.
Salesforce stores and serves the managed package version. Your website only needs the article or app page and the install link. Anyone with the URL and a valid Salesforce login can open the installer; the installing user chooses the target org and confirms the install.
Official downloads and references
- Get a free Salesforce Developer Edition org
- Download Visual Studio Code
- Install Salesforce CLI
- Salesforce Extensions for VS Code installation guide
- Enable Dev Hub and 2GP
- Create and register a namespace
- Create a 2GP managed package
- Create package versions
- Install a 2GP package using a URL
Salesforce org editions, command flags, and distribution policies can change. Check Salesforce’s current documentation before using this process for a commercial release. A free Developer Edition org is useful for learning, but Salesforce warns it can expire and affect long-term access to packages it owns.