Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active December 13, 2023 02:46
Show Gist options
  • Save ruario/80fefd174b3395d34c14 to your computer and use it in GitHub Desktop.
Save ruario/80fefd174b3395d34c14 to your computer and use it in GitHub Desktop.
Fetching, building and installing PatchELF

The following assumes you want a single user install. If you want a system wide install instead, run the attached script create-patchelf-pkg.sh (after first installing a suitable build environment).

Note: create-patchelf-pkg.sh also provides an uninstall script.

Setting up a Build Environment

Install a basic build environment (if you don't already have one). How to install this varies from distro to distro. Your best bet to refer to your distro's wiki or ask in their forums. That said, here are a few example install commands for a few of the popular distros:

  • Ubunt/Mint/Debian: sudo install build-essential
  • Fedora: sudo dnf install @development-tools
  • OpenSUSE: sudo zypper install -t pattern devel_basis

Building and Installing PatchELF

First go the the PatchELF release page to find out the latest version number, then issue the following commands (adjusting the version number as needed).

wget https://github.com/NixOS/patchelf/archive/refs/tags/0.18.0.tar.gz
tar xf 0.18.0.tar.gz
cd patchelf-0.18.0
./bootstrap.sh
./configure --prefix="$HOME/.local"
make install
strip --strip-unneeded ~/.local/bin/patchelf
gzip -9 ~/.local/share/man/man1/patchelf.1

Configure your path

Update your $PATH variable to include $HOME/.local/bin (if it doesn't already).

Removing PatchELF

If you later need to uninstall, issue the following:

rm -rv ~/.local/{share/doc/patchelf,share/man/man1/patchelf.1.gz,bin/patchelf}

Note: The above command assumes a bash shell.

Removing a manual system wide install

If you didn't follow any of the above and just did a system-wide install via ./configure && sudo make install—that you could now like to remove—you can use the following generic self-compiled package removal guide to uninstall PatchELF.

#!/bin/sh -eu
available () {
command -v "$1" >/dev/null 2>&1
}
if available wget; then
DL="wget -O-"
DLS="wget -qO-"
elif available curl; then
DL="curl -L"
DLS="curl -s"
else
echo "Install Wget or cURL" >&2
exit 1
fi
PKGNAME=patchelf
VERSION="$($DLS "https://github.com/NixOS/$PKGNAME/tags" | grep -om1 "/NixOS/$PKGNAME/releases/tag/[0-9\.]\+" | cut -d"/" -f 6)"
if [ -z "$VERSION" ]; then
VERSION=0.10
fi
if [ ! -e "${PKGNAME}-$VERSION.tar.gz" ]; then
$DL "https://github.com/NixOS/$PKGNAME/archive/refs/tags/$VERSION.tar.gz" > "${PKGNAME}-$VERSION.tar.gz"
fi
if [ -d "${PKGNAME}-$VERSION" ]; then
rm -fr "${PKGNAME}-$VERSION"
fi
tar xf "${PKGNAME}-$VERSION.tar.gz"
cd "${PKGNAME}-$VERSION"
./bootstrap.sh
./configure --prefix="$PWD/staging/usr/local" --mandir="$PWD/staging/usr/local/man" --docdir="$PWD/staging/usr/local/doc/$PKGNAME-$VERSION"
make install
cd staging
strip --strip-unneeded "usr/local/bin/$PKGNAME"
gzip -9 "usr/local/man/man1/$PKGNAME.1"
mkdir -p usr/local/bin
cat <<EOF> "usr/local/bin/uninstall-$PKGNAME-$VERSION"
#!/bin/sh -e
while read f; do
if [ -e "\$f" -o -h "\$f" ]; then
if [ -d "\$f" ]; then
if ! ls -A "\$f" | grep -q ^; then
if [ ! -h "\$f" ]; then
rmdir -v "\$f"
fi
fi
else
rm -v "\$f"
fi
fi
done << FILE_LIST
EOF
find . -depth | sed -n 's,^\./,/,p' | grep -vxE "/usr/local/bin/uninstall-$PKGNAME-$VERSION|/usr|/usr/local(/etc|/doc|/games|/include|/lib(32|64)?|/man(/man[a-z0-9]+)?|/s?bin|/share(/color|/man(/man[a-z0-9]+)?|/misc)?|/src)?" >> "usr/local/bin/uninstall-$PKGNAME-$VERSION"
printf "/usr/local/bin/uninstall-$PKGNAME-$VERSION\nFILE_LIST\n" >> "usr/local/bin/uninstall-$PKGNAME-$VERSION"
chmod 755 "usr/local/bin/uninstall-$PKGNAME-$VERSION"
find . ! -type d | sed 's,^\./,,' | tar caf "../../$PKGNAME-$VERSION.bin.tar.gz" -T- --owner 0 --group 0
cd ../..
rm -fr "$PKGNAME-$VERSION"
printf "\nCreated: $PKGNAME-$VERSION.bin.tar.gz\n\n"
printf "To install issue the following as root (or prefaced with sudo):\n\n"
printf " tar xf $PKGNAME-$VERSION.bin.tar.gz -C/\n\n"
printf "If you later need to uninstall, issue the following as root (or prefaced with sudo):\n\n"
printf " uninstall-$PKGNAME-$VERSION\n\n"
@mmistele
Copy link

mmistele commented Aug 23, 2018

For folks having issues with the uninstall, here's one with the absolute path (so you don't have to run it from ~):
rm -rv ~/.local/{share/doc/patchelf,share/man/man1/patchelf.1.gz,bin/patchelf}

Also there's a new version of patchelf out (0.9) - here are the updated lines for single-user install:

wget https://nixos.org/releases/patchelf/patchelf-0.9/patchelf-0.9.tar.bz2
tar xf patchelf-0.9.tar.bz2
cd patchelf-0.9
...

@ruario
Copy link
Author

ruario commented Sep 3, 2019

@mmistele Thanks for that. I totally forgot about this gist and was not notified last year when you commented. I have updated it now, correcting the path and updating the version to 0.10

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