Skip to content

Instantly share code, notes, and snippets.

@gwerbin
Last active August 18, 2023 07:52
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gwerbin/dcba755b0484423e9e45 to your computer and use it in GitHub Desktop.
Save gwerbin/dcba755b0484423e9e45 to your computer and use it in GitHub Desktop.
Uninstall Ghostscript that comes with MacTex.
#!/usr/bin/env bash
# Copyright (c) 2015, 2020, and 2021 by Greg Werbin.
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with
# or without fee is hereby granted, provided that the above copyright notice and this
# permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
# SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
# SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
# THE USE OR PERFORMANCE OF THIS SOFTWARE.
# The MacTex website (https://www.tug.org/mactex/uninstalling.html) says it's
# "difficult" to uninstall Ghostscript, as installed by MacTex.
#
# Their suggestion is to "Open the MacTeX-2015 install package and select 'Show Files'
# from the resulting 'File' menu of Apple's installer", and then "find files related to
# Ghostscript and remove them."
#
# This script is an attempt to automate that process. It prints the names of the deleted
# files, and moves the files to the user's Trash. This can help you roll back the
# effects of the script in case something goes wrong.
#
# Note that this requires write access to the `/tmp` directory on your computer.
#
# Note also that different versions of MacTex might require changes to this script.
#
# Example usage:
# 1) You saved `mactex-20150613.pkg` in `~/Downloads`, and used that to install MacTex.
# 2) Run `bash uninstall-ghostscript.sh ~/Downloads/mactex-20150613.pkg`
#
# These files were removed from my system when I used this tool:
# /usr/local/bin/dvipdf
# /usr/local/bin/eps2eps
# /usr/local/bin/font2c
# /usr/local/bin/gs-X11
# /usr/local/bin/gs-X11-64Bit
# /usr/local/bin/gs-X11-Yosemite
# /usr/local/bin/gs-noX11
# /usr/local/bin/gs-noX11-64Bit
# /usr/local/bin/gs-noX11-Yosemite
# /usr/local/bin/gsbj
# /usr/local/bin/gsdj
# /usr/local/bin/gsdj500
# /usr/local/bin/gslj
# /usr/local/bin/gslp
# /usr/local/bin/gsnd
# /usr/local/bin/lprsetup.sh
# /usr/local/bin/pdf2dsc
# /usr/local/bin/pdf2ps
# /usr/local/bin/pf2afm
# /usr/local/bin/pfbtopfa
# /usr/local/bin/pphs
# /usr/local/bin/printafm
# /usr/local/bin/ps2ascii
# /usr/local/bin/ps2epsi
# /usr/local/bin/ps2pdf
# /usr/local/bin/ps2pdf12
# /usr/local/bin/ps2pdf13
# /usr/local/bin/ps2pdf14
# /usr/local/bin/ps2pdfwr
# /usr/local/bin/ps2ps
# /usr/local/bin/ps2ps2
# /usr/local/bin/unix-lpr.sh
# /usr/local/bin/wftopfa
# /usr/local/share/man/de/man1/dvipdf.1
# /usr/local/share/man/de/man1/font2c.1
# /usr/local/share/man/de/man1/gsnd.1
# /usr/local/share/man/de/man1/pdf2dsc.1
# /usr/local/share/man/de/man1/pdf2ps.1
# /usr/local/share/man/de/man1/printafm.1
# /usr/local/share/man/de/man1/ps2ascii.1
# /usr/local/share/man/de/man1/ps2pdf.1
# /usr/local/share/man/de/man1/ps2ps.1
# /usr/local/share/man/de/man1/wftopfa.1
# /usr/local/share/man/man1/dvipdf.1
# /usr/local/share/man/man1/font2c.1
# /usr/local/share/man/man1/gs.1
# /usr/local/share/man/man1/gslp.1
# /usr/local/share/man/man1/gsnd.1
# /usr/local/share/man/man1/pdf2dsc.1
# /usr/local/share/man/man1/pdf2ps.1
# /usr/local/share/man/man1/pf2afm.1
# /usr/local/share/man/man1/pfbtopfa.1
# /usr/local/share/man/man1/printafm.1
# /usr/local/share/man/man1/ps2ascii.1
# /usr/local/share/man/man1/ps2epsi.1
# /usr/local/share/man/man1/ps2pdf.1
# /usr/local/share/man/man1/ps2pdfwr.1
# /usr/local/share/man/man1/ps2ps.1
# /usr/local/share/man/man1/wftopfa.1
#
# And finally the directory:
# /usr/local/share/ghostscript
move_to_trash() {
# Move a file to the MacOS "Trash".
# If the operation fails (e.g. the file is missing), an error message is emitted.
command mv -v "$1" ~/.Trash \
|| >&2 echo "Failed to remove file or directory: $1"
}
uninstall_ghostscript () {
# `pkgutil --bom` extracts any BOM ("bill of materials") files from the package
# and emits its location. We only need `local.pkg`.
# NOTE: This filename might change in newer versions of MacTex.
bom_usr_local="$( pkgutil --bom $1 | fgrep local.pkg )"
# 1) `lsbom -s f` lists the paths of the files in the BOM.
# 2) Filter out the `ghostscript` directory (we will remove it only once everything else is gone).
# 3) Replace the `.` prefix with the proper installation prefix: `/usr/local`.
# 4) For each filename, move the file to Trash.
lsbom -s -f "$bom_usr_local" \
| fgrep -v ghostscript \
| sed 's_^\._/usr/local_' \
| while read filename; do
move_to_trash "$filename"
done
# Finally, remove the `ghostscript` directory itself.
move_to_trash /usr/local/share/ghostscript
}
uninstall_ghostscript "$@"
@meling
Copy link

meling commented Apr 30, 2020

It is not so clear what the argument to this script should be. If run without an argument I get:

pkgutil: option `--bom' requires an argument

Does it need the mactex.pkg file that I already deleted?

@gwerbin
Copy link
Author

gwerbin commented May 20, 2020

This was a long time ago, but yes I believe so, @meling. You should be able to re-download the PKG file at least. This script basically just extracts the list of installed files from the PKG and moves them to the trash one by one (in a somewhat insecure manner, I might add).

I haven't tested this at all, but the following code would be considered better practice:

uninstall_ghostscript () {
  bom_usr_local=$(pkgutil --bom $1 | grep -F local.pkg)
  lsbom -s -f $bom_usr_local | grep -F -v ghostscript | sed 's_^\._/usr/local_' | while read filename; do 
    command mv - "$filename" ~/.Trash
  done
  command mv -v /usr/local/share/ghostscript ~/.Trash
}

@sirach5
Copy link

sirach5 commented Nov 21, 2021

This was a long time ago, but yes I believe so, @meling. You should be able to re-download the PKG file at least. This script basically just extracts the list of installed files from the PKG and moves them to the trash one by one (in a somewhat insecure manner, I might add).

I haven't tested this at all, but the following code would be considered better practice:

uninstall_ghostscript () {
  bom_usr_local=$(pkgutil --bom $1 | grep -F local.pkg)
  lsbom -s -f $bom_usr_local | grep -F -v ghostscript | sed 's_^\._/usr/local_' | while read filename; do 
    command mv - "$filename" ~/.Trash
  done
  command mv -v /usr/local/share/ghostscript ~/.Trash
}

This worked for me, thank you. I had installed two stray instances outside of brew I couldn't get rid of any other way.

@q3yi
Copy link

q3yi commented Dec 16, 2021

After test with latest MacTex.pkg(texlive2021), I believe the script is outdated and should be updated as below:

uninstall_ghostscript () {
    bom_usr_local=$(pkgutil --bom $1 | grep -F Ghostscript)
    lsbom -s -f $bom_usr_local | grep -F -v ghostscript | sed 's_^\.__' | while read filename; do

	if [[ -e $filename ]]; then
	    command mv - "$filename" ~/.Trash
	fi
    done
    command mv -v /usr/local/share/ghostscript ~/.Trash
}

uninstall_ghostscript $1

@gwerbin
Copy link
Author

gwerbin commented Dec 16, 2021

@Tsingyi as a courtesy to future readers, it's useful to explain how exactly your script differs from the original. It looks like the differences are how you define bom_usr_local, and your use of [[ -e before trying to remove the file. The 2nd one is good change (in case some files have already been deleted). But it would be helpful if you explained the 1st change: why you changed it to grep -F Ghostscript from grep -F local.pkg.

@q3yi
Copy link

q3yi commented Dec 18, 2021

@gwerbin the reason for the first change is that I tested the command pkgutil --bom with the latest MacTex.pkg The MacTeX-2021 Distribution, and found that there's no longer any pkg match pattern local.pkg in MacTex.pkg, so I change it to grep -F Ghostscript to find the files.

> pkgutil --bom MacTeX.pkg
/tmp/MacTeX.pkg.boms.BhgSgg/Ghostscript-9.53.3-Start.pkg/Bom
/tmp/MacTeX.pkg.boms.BhgSgg/Ghostscript-9.53.3-libgs-Start.pkg/Bom
/tmp/MacTeX.pkg.boms.BhgSgg/GUI-Applications-Start.pkg/Bom
/tmp/MacTeX.pkg.boms.BhgSgg/TeXLive-2021-Start.pkg/Bom

⋊> pkgutil --bom MacTeX.pkg | grep -F local.pkg

⋊> pkgutil --bom MacTeX.pkg | grep -F Ghostscript
/tmp/MacTeX.pkg.boms.moSDab/Ghostscript-9.53.3-Start.pkg/Bom
/tmp/MacTeX.pkg.boms.moSDab/Ghostscript-9.53.3-libgs-Start.pkg/Bom

In additional, the result of lsbom -s -f $bom_user_local | grep -F -v ghostscript return different results in my system. (no idea whether it's related to any system configuration) so I had to change the sed 's_^\._/usr/local_' to sed 's_^\.__' to extract the right path.

> lsbom -s -f /tmp/MacTeX.pkg.boms.skzyZj/Ghostscript-9.53.3-Start.pkg/Bom | grep -F -v ghostscript
./._usr
./usr/._local
./usr/local/._bin
./usr/local/._share
./usr/local/bin/._dvipdf
./usr/local/bin/._eps2eps
./usr/local/bin/._gs-noX11
./usr/local/bin/._gsbj
./usr/local/bin/._gsdj
./usr/local/bin/._gsdj500
./usr/local/bin/._gslj
./usr/local/bin/._gslp
./usr/local/bin/._gsnd
./usr/local/bin/._lprsetup.sh
./usr/local/bin/._pdf2dsc
./usr/local/bin/._pdf2ps
./usr/local/bin/._pf2afm
./usr/local/bin/._pfbtopfa
./usr/local/bin/._pphs
./usr/local/bin/._printafm
./usr/local/bin/._ps2ascii
./usr/local/bin/._ps2epsi
./usr/local/bin/._ps2pdf
./usr/local/bin/._ps2pdf12
./usr/local/bin/._ps2pdf13
./usr/local/bin/._ps2pdf14
./usr/local/bin/._ps2pdfwr
./usr/local/bin/._ps2ps
./usr/local/bin/._ps2ps2
./usr/local/bin/._unix-lpr.sh
./usr/local/bin/dvipdf
./usr/local/bin/eps2eps
./usr/local/bin/gs-X11
./usr/local/bin/gs-noX11
./usr/local/bin/gsbj
./usr/local/bin/gsdj
./usr/local/bin/gsdj500
./usr/local/bin/gslj
./usr/local/bin/gslp
......

@gwerbin
Copy link
Author

gwerbin commented Dec 19, 2021

Thanks @Tsingyi! It looks like they changed how the paths are defined. Did you look in the 2nd "Ghostscript" BOM? Maybe files from both BOMs should be removed.

@q3yi
Copy link

q3yi commented Dec 20, 2021

Did you look in the 2nd "Ghostscript" BOM?

@gwerbin I didn't check it carefully, just removed both in the script. Since I just want all the files related to ghostscript deleted in my system.

@LouisSY
Copy link

LouisSY commented Feb 4, 2022

How to give the write access to the /tmp directory on my computer? I'm using a Mac with apple silicon.

@milanglacier
Copy link

I am not familiar with pkgutil, but I am wondering, can I just first pkgutil --pkgs | grep -i ghost to find packages might be ghostscript installation, and then for each packages, I type pkgutil --files the-package-name, and then delete the outputed filenames one by one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment