Reading Time: 3minutes
1. Listing All installed packages (.pkg) files on Mac OS X
If you are used to Linux package management (with dpkg.deb package tool or Redhat's .rpm package manager rpm command) and out of a suddeny you have to use for some time or permanently choose to migrate to Mac OS X and joined the Jailed closed Computer model of Mr. Steve Jobs.
As a Linux command addict,yYou might be wondering on how to list all installed packages on Mac OS with a command just like you use Debian / Ubuntus terminal to list installed packages and their overall state for example with dpkg with:
or Fedora / CentOS to list all installed packages you're pretty much accustomed to:
The same is possible in Mac OS X by opening Terminal and running cmd:
MacBook-Air:Volumes root# pkgutil –pkgs
For the sake of convenience run it with a pipe to less cmd:
MacBook-Air:Volumes root# pkgutil –pkgs | less
To find whether a package with a certain name is installed or not
MacBook-Air:Volumes root# pkgutil –pkgs|grep -i oracle
To further list the content of the package (the package files contained and directory structure etc.)
MacBook-Air:Volumes root# pkgutil –files the-package-name.pkg
this is like the Linux equivalent of
dpkg -L packagename and rpm -qa rpm-packagename:
Checking the package directory location on Mac OS is done with:
MacBook-Air:Volumes root# pkgutil –pkg-info com.oracle.jre
location: Library/Internet Plug-Ins/JavaAppletPlugin.plugin
2. Deleting a Mac OS X package .pkg
After reviewing the .pkg file content for smaller sized packages the best way to remove it is to manually remove all files belonging to the package (but before make sure you are not deleting an important packageand have visually closely inspected them otherwise you might break badly your Mac OS X …) you can run below 2 commands to delete package files and directory:
# pkgutil –only-files –files the-package-name.pkg | tr '\n' '\0' | xargs -n 1 -0 sudo rm -f
# pkgutil –only-dirs –files the-package-name.pkg | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir
Once you have removed the files you can remove the receipt (e.g. remove it from Mac package database), with:
# pkgutil –forget the-package-name.pkg
pkgutil –forgetis useful if you get errors during removal attempt with pkgutil –only-files … , pkgutil –only-dirs …. cmds.
Note that to remove a package you have to be root or run the commands via sudo with admin privileges if you have it installed and using it.
When removing the files be extremely careful as some of the packages you might try to remove might be updating important Mac OS X system components or used as a backbone for the overall Mac OS GUI / background operations thus removing a system related package might leave your MacBook unbootable because of the removal of the necessery component …
If you get some errors while issuing some of the 2 above commands like:
"Operation not permitted when trying to uninstall" this is because the command or directory you're trying to remove is in current use by a running process on the Mac OS or because the file or directory has a special set permissions to prevent them from easy removal (as a precaution OS safety measures).
You might wonder what might be the purpose of a manual command removal of Mac package but, If you have to regularly delete Mac OS X packages by scripting the removal of unnecessery packages or Mac OS X updates on a many numbers of computers to automate removal jobs.
I've found also a github repository bash shell script that is automating the process of removal and could even save further time if in need to script a package information about Macs and be able to easily manage packages of mac Desktops via (lets say SSH), check out pkg-remove.sh script here.