How to Install dmg File on Mac from Command Line

Installing software is the most common way to customize your Mac so that it can perform the tasks you need. When downloading and installing software from the Internet, make sure that you trust the source to avoid infecting your computer with malware by mistake.

Many applications are distributed as disk images, a compressed binary format. If you double-click a disk image in the Finder, it is mounted automatically. Once mounted, installation of the application is typically done by dragging an icon to the Applications folder. The same can be accomplished from the command line using two commands, hdiutil, and cp.

The following steps show the installation of a popular VNC client for OS X called “Chicken of the VNC”. It can be used as a remote desktop client for Linux, Mac, or Windows hosts.

The download file is named “cotvnc-20b4.dmg”. Here are the steps needed to install it remotely from the command line.

Note: this technique can be used from a local Terminal window or a remote SSH connection.

Mount the disk image

The first step is to mount (or attach) the disk image. From the command line, use:

$ hdiutil mount cotvnc-20b4.dmg

I received the following output:

Checksumming Driver Descriptor Map (DDM : 0)…
    Driver Descriptor Map (DDM : 0): verified   CRC32 $767AD93D
Checksumming Apple (Apple_partition_map : 1)…
    Apple (Apple_partition_map : 1): verified   CRC32 $DD66DE0F
Checksumming disk image (Apple_HFS : 2)…
..............................................................................
         disk image (Apple_HFS : 2): verified   CRC32 $EF1F362F
Checksumming  (Apple_Free : 3)…
                   (Apple_Free : 3): verified   CRC32 $00000000
verified   CRC32 $F5A3FFA1
/dev/disk1           Apple_partition_scheme          
/dev/disk1s1         Apple_partition_map             
/dev/disk1s2         Apple_HFS                       /Volumes/Chicken of the VNC

A mounted disk image appears on the Desktop, in the Finder, and more importantly, shows up as a directory in /Volumes. In this case, the last line of output from hdiutil showed exactly where the disk image was mounted.

You can also use the below command to confirm the mounting of the disk image:

$ diskutil list

Sometimes when a disk image is mounted, it will prompt you to agree to a license first. In that case, the text that would normally appear in a GUI dialog box instead appears in the Terminal window. Once you scroll to the bottom of the agreement, you can type in Y to continue or N to stop. The Firefox disk image is one example of a package that displays a license before mounting.

Install the application

Use the cp command to copy the application to /Applications:

$ sudo cp -R "/Volumes/Chicken of the VNC/Chicken of the VNC.app" /Applications

The -R switch means to copy recursively, in other words, copy everything from that location including all subdirectories and files below. It is important to leave off the trailing “/” from the “Chicken of the VNC.app” directory, or the command will not copy the directory itself, just the contents. After entering your password, the application will be installed and ready to use.

Most applications can simply be copied to the /Applications directory. However, some are distributed in a .pkg format and must be installed using the installer command instead of cp. To install a .pkg, use this command:

$ sudo installer -package /path/to/package -target "/Volumes/Macintosh HD"

Unmount the disk image

To tidy up, return to your home directory and unmount the disk image:

$ cd ~
$ hdiutil unmount "/Volumes/Chicken of the VNC/"

You should see this message after the unmount:

/Volumes/Chicken of the VNC/" unmounted successfully.

Remove an App in the Finder

1. In the Finder, navigate to the Applications folder.
2. Select the application you want to uninstall, then drag it to the Trash.
3. If necessary, authenticate as Local Administrator.
4. Choose Finder > Empty Trash, then click Empty Trash in the confirmation dialog.

The more you use a Mac, the more likely you’ll need to install programs and occasionally uninstall programs that you no longer use. With the right software, you can customize your Mac to perform nearly any task.

Related Post