Apple - Is there a command to install a dmg

First, mount the dmg image : sudo hdiutil attach <image>.dmg

The image will be mounted to /Volumes/<image>. Mine contained a package which i installed with: sudo installer -package /Volumes/<image>/<image>.pkg -target /

Finally unmount the image: sudo hdiutil detach /Volumes/<image>.


You should be able to mount the .dmg using:

hdiutil attach /path/to/file.dmg

And then copy its contents (which appears in /Volumes) where ever you like.


If you want to script the install it requires a few more steps since the name of the .dmg file, the name of the Volume created, the name of the application, and the name of the device that needs to be detached can all be different. Plus they can have spaces in them.

Also a .dmg can have an .app file or a .pkg file in it and these require different behavior.

Here's a bash function to install a dmg from a remote URL:

# usage: installdmg https://example.com/path/to/pkg.dmg
function installdmg {
    set -x
    tempd=$(mktemp -d)
    curl $1 > $tempd/pkg.dmg
    listing=$(sudo hdiutil attach $tempd/pkg.dmg | grep Volumes)
    volume=$(echo "$listing" | cut -f 3)
    if [ -e "$volume"/*.app ]; then
      sudo cp -rf "$volume"/*.app /Applications
    elif [ -e "$volume"/*.pkg ]; then
      package=$(ls -1 "$volume" | grep .pkg | head -1)
      sudo installer -pkg "$volume"/"$package" -target /
    fi
    sudo hdiutil detach "$(echo "$listing" | cut -f 1)"
    rm -rf $tempd
    set +x
}

Note if your .dmg has an .app file that runs to install the program, then you will need to do something different again.