---
schema_version: "1"
id: 4bfd2002bc3e37e8b36e5f4b3b2a398fe5bf8c9b990ea6804388100b03beee0e
title: Sample Script for Installing Second-Generation Managed Packages with Dependencies
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform-pkg-exchange/managed_packaging/guides/second-generation/sfdx-dev-dev2gp-install-pkg-sample-script.md
canonical_url: https://developer.salesforce.com/docs/platform/pkg2-dev/guide/sfdx-dev-dev2gp-install-pkg-sample-script.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 79177aa152b4bffbaf2fc51d5a1ae5bdf9fc6b5143f9d650ace0d35167f48c26
products:
  - Second-Generation Managed Packaging Developer Guide
  - merged-docs
---

# Sample Script for Installing Second-Generation Managed Packages with Dependencies

Use this sample script as a basis to create your own script to install second-generation managed packages with dependencies. This script contains a query that finds dependent packages and installs them in the correct dependency order.

## Sample Script

**note:** Be sure to replace the package version ID and scratch org user name with your own specific details.

```nolang
#!/bin/bash


# The execution of this script stops if a command or pipeline has an error.

# For example, failure to install a dependent package will cause the script

# to stop execution.

set -e


# Specify a package version id (starts with 04t)

# If you know the package alias but not the id, use sf package version list to find it.

PACKAGE=04tB0000000NmnHIAS


# Specify the user name of the subscriber org.

USER_NAME=test-bvdfz3m9tqdf@example.com


# Specify the timeout in minutes for package installation.

WAIT_TIME=15


echo "Retrieving dependencies for package Id: "$PACKAGE


# Execute soql query to retrieve package dependencies in json format.

RESULT_JSON=`sf data query -u $USER_NAME -t -q "SELECT Dependencies FROM SubscriberPackageVersion WHERE Id='$PACKAGE'" --json`


# Parse the json string using python to test whether the result json contains a list of ids or not.

DEPENDENCIES=`echo $RESULT_JSON | python -c 'import sys, json; print json.load(sys.stdin)["result"]["records"][0]["Dependencies"]'`


# If the parsed dependencies is None, the package has no dependencies. Otherwise, parse the result into a list of ids.

# Then loop through the ids to install each of the dependent packages.

if [[ "$DEPENDENCIES" != 'None' ]]; then


    DEPENDENCIES=`echo $RESULT_JSON | python -c '

import sys, json

ids = json.load(sys.stdin)["result"]["records"][0]["Dependencies"]["ids"]

dependencies = []

for id in ids:

    dependencies.append(id["subscriberPackageVersionId"])

print " ".join(dependencies)

'`


    echo "The package you are installing depends on these packages (in correct dependency order): "$DEPENDENCIES

    for id in $DEPENDENCIES

    do

        echo "Installing dependent package: "$id

        sf package install --package $id -u $USER_NAME -w $WAIT_TIME --publish-wait 10

    done


else

    echo "The package has no dependencies"


fi


# After processing the dependencies, proceed to install the specified package.

echo "Installing package: "$PACKAGE

sf package install --package $PACKAGE -u $USER_NAME -w $WAIT_TIME --publish-wait 10


exit 0;
```
