How to install managed package using Salesforce DX CLI?

To install a First Generation Managed Package via sfdx, you do it the same way you do it with the metadata API. The command looks like this:

sfdx force:mdapi:deploy -d packages

The packages folder contains a package.xml file with the following contents:

<?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>

The packages folder should contain a sub-folder named installedPackages. Within that folder, you should have installedPackage files that contain data like the following:

<?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>

Password is optional unless required by the package.

The file name for the package will be namespace.installedPackage. For example, since your package's namespace is ldt, the file name would be ldt.installedPackage.

You can install multiple packages in a single deployment. Each package can succeed/fail independently of the others.

This will not work for Second Generation Managed Packages. For those, use the sfdx force:package:install command. You can write a shell script to automate installing many packages in a preferred order of dependencies.


Have you tried using:

sfdx force:package:install -i 04t6A0000004eytQAA

Docs for this are found in the CLI command reference.

The only thing is that you'd need to know the ID of the package to pass to the command, but if you can get that, you should be in good shape.

EDIT

After doing some playing around with this now, when you run the install of an app exchange managed package through the UI, the package Id (or rather, the package version Id if I'm not mistaken) is shown in the address bar of your browser.

Please do not consider this an endorsement of Skuid. I have no affiliation to them, rather it was the first package that came to mind.

This is the value you need to supply. Taking that Id value, I have now successfully done the following:

$ sfdx force:project:create -n test-project
target dir = /Users/pchittum/Stuff/git/sfdx-pilot
   create test-project/sfdx-project.json
   create test-project/README.md
   create test-project/config/project-scratch-def.json

$ cd test-project/
$ sfdx force:org:create -f config/project-scratch-def.json -s 
Successfully created scratch org: 00D0v0000008eXmEAI, username: test-mffvsibtb6ly@pchittum_company.net

$ sfdx force:package:install -i 04t4A000000cESSQA2 
PackageInstallRequest is still InProgress or Unknown. You can query the status using
sfdx force:package:install:get -i 0Hf0v0000008QWdCAM 

$ sfdx force:package:install:get -i 0Hf0v0000008QWdCAM 
Successfully installed package [04t4A000000cESSQA2]

Please note I have no affiliation with the above AppExchange partner. It was just the first thing that came to mind.


This helped me get the right id for app exchange packages currently installed in my devhub:

$ sfdx force:package:installed:list -u [DevHubAlias]

=== Installed Package Versions [2]
ID                  PACKAGE ID          PACKAGE NAME           NAMESPACE  PACKAGE VERSION ID  VERSION NAME  VERSION
──────────────────  ──────────────────  ─────────────────────  ─────────  ──────────────────  ────────────  ────────
0A346000000qUm7CAE  03336000000DlP2AAK  HEDA                   hed        04t360000011zP6AAI  HEDA 1.43     1.43.0.3
0A346000000H6SeCAK  033C00000006mUuIAI  Apsona for Salesforce  apsona     04tC0000000ggXVIAY  Update        1.8.0.3

$ sfdx force:package:install -p 04t360000011zP6AAI -u scratch -w 5

Note: use the -p parameter instead of the old -i param.

Note: the HEDA package I am installing also required some additional setup in the scratch org before I could install it. Specifically I had to create a dummy account record type.