How can I install a managed package using Salesforce DX CLI?

First Generation Managed Packages

When working with Salesforce DX and Continuous Integration tools like Travis CI, Jenkins, or GitHub Actions, a common requirement is to install managed packages into scratch orgs before deploying your source code. This is especially important if your Lightning components, Apex classes, or other metadata depend on packages published on the AppExchange. Without installing those packages first, you may encounter errors such as “No COMPONENT named markup://ldt:datatableDev found”. The solution is to install the managed package using Salesforce DX CLI as part of your build pipeline.

There are two main approaches depending on the type of managed package you are using. For First Generation Managed Packages (1GP), the installation process relies on the Metadata API. You need to create a package.xml file that specifies the InstalledPackage metadata type and then deploy it to your scratch org. An example of the package.xml looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>InstalledPackage</name>
    </types>
    <version>40.0</version>
</Package>

Alongside this, you should create a folder named installedPackages inside your packages directory. Inside that folder, you will add XML files for each managed package you want to install. The filename format should be namespace.installedPackage. For example, if the namespace of the package is ldt, the file name should be ldt.installedPackage. Here is what the contents of that file may look like:

<?xml version="1.0" encoding="UTF-8"?>
<InstalledPackage xmlns="http://soap.sforce.com/2006/04/metadata">
    <versionNumber>1.61</versionNumber>
    <password>package-password</password>
</InstalledPackage>

The versionNumber defines the exact version of the managed package you want to install. The password element is optional unless the package is protected with a password. Once you have the metadata set up, you can deploy it into your scratch org using this command:

sfdx force:mdapi:deploy -d packages -u PackageInstallation

This command pushes the InstalledPackage definitions into the target org, which installs the specified managed package. If you include multiple package definitions, Salesforce will attempt to install all of them, and each one can succeed or fail independently.

For Second Generation Managed Packages (2GP), Salesforce DX makes the process simpler by providing a direct CLI command. You can install a 2GP package by running:

sfdx force:package:install --package <packageId_or_alias> --targetusername <scratchOrgAlias>

Here, <packageId_or_alias> is typically the 04t package version ID from AppExchange or your DevHub, and <scratchOrgAlias> is the alias of your scratch org. For example:

sfdx force:package:install --package 04t3X0000024abcQAA --targetusername PackageInstallation

This command directly installs the package without requiring a package.xml or metadata folder structure. It is the recommended way for all 2GP packages. You can also add flags like --wait to specify installation time or --publishwait to wait for dependent packages to finish publishing.

In a CI/CD environment such as Travis CI, you can automate package installation by including these commands in your .travis.yml configuration file. If your project requires multiple packages that must be installed in a certain order, it is best to create a shell script that runs each sfdx force:package:install command sequentially. This ensures that dependencies are installed correctly before your source code is pushed into the scratch org.

To summarize, First Generation Managed Packages require you to use the Metadata API deployment method with InstalledPackage metadata, while Second Generation Managed Packages can be installed directly using the sfdx force:package:install command. Both methods allow you to integrate managed package installation seamlessly into your Salesforce DX workflow and Continuous Integration pipelines.

Job-Oriented Salesforce Training with 100% Money Back Assurance

Our Salesforce Course is designed to provide a thorough understanding of the Salesforce platform, equipping you with the essential skills to thrive in the CRM industry. The curriculum includes vital modules such as Salesforce Admin, Developer, and AI, combining foundational knowledge with hands-on practice. By engaging in real-world projects and assignments, you’ll develop the expertise to address complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and industry insights necessary for success in the Salesforce ecosystem.

Along with technical knowledge, our Top 10 Salesforce training institutes in Hyderabad offers personalized mentorship, exam preparation, and interview coaching to enhance your career prospects. You’ll have access to comprehensive study materials, live project experience, and dedicated support throughout your learning journey. Upon completing the course, you’ll be well-prepared for certification exams and equipped with the practical skills employers value. Begin your Salesforce career with us and unlock countless career opportunities. Sign up for a Free Demo today!

0
Would love your thoughts, please comment.x
()
x