1. Overview

In this tutorial, we’ll learn how to quickly install and run programs without root privileges. First, we’ll see how to download a package and extract it to non-root locations. For that, we’ll demonstrate the apt, dnf, and pacman package managers.

In addition, we’ll also discuss how to build a package from sources and install it in a non-root directory.

2. Downloading and Extracting Binary Packages

In addition to downloading and installing a package through a package manager like apt, we can also download the binaries directly with it. Then, we can extract the package into a user-writable directory.

2.1. apt

As an example, let’s download the DEB file for translate-shell using apt:

$ apt download translate-shell

It downloads to the current working directory:

$ ls -lH
-rw-r--r-- 1 baeldung baeldung 67474 Feb 18 2023 translate-shell_0.9.7.1-1_all.deb

Next, we take the DEB file and extract it to a directory with write permissions for a non-root user. For instance, we can extract the package to $HOME/.local/share, which is the standard user application data folder on  Linux distributions:

$ dpkg -x translate-shell*.deb $HOME/.local/share/trans

Afterward, we can easily use it:

$ $HOME/.local/share/trans/usr/bin/trans --version
Translate Shell 0.9.7.1

Additionally, we can also add the binary to $PATH:

$ export PATH="$HOME/.local/share/trans/usr/bin:$PATH"

Now, we can run it like other Linux utilities:

$ trans -b en:fr "Hello, world!"
Bonjour le monde!

2.2. dnf

dnf is a package manager for Fedora, RHEL, and CentOS. Like apt, it also has an option to merely download a package:

$ sudo dnf download translate-shell

Afterward, we’ll need to convert the RPM package to cpio, which we’ll be able to extract:

$ rpm2cpio translate-shell*.rpm | cpio -idm

Let’s break this down:

  • rpm2cpio converts the RPM to CPIO
  • -i extracts the contents of the archive
  • -d creates an appropriate directory for the extracted contents
  • -m preserves the modification time of the extracted files

Once the files have been extracted, we can run them as we saw in the previous section.

2.3. pacman

pacman is the package manager for Arch, Artix, and Manjaro distributions. It also has the ability to download a package in the zst compression format:

$ sudo pacman -Sw translate-shell

Here, -S syncs the packages, and -w downloads the package into the current directory. Afterward, we can extract the package to the required non-root directory:

$ tar -xf translate-shell*.pkg.tar.zst $HOME/.local/share/trans

In the command, -x extracts the contents of the archive file specified with -f. Once the contents are extracted, we can run the program as we normally would.

3. Compiling From Source

The method in the previous sections works well for packages that are already available in the official package repositories. However, we may encounter packages that are either outdated or not available in those repositories at all. Nonetheless, we can download the sources for those packages, compile them, and install them in a non-root user directory.

As an example, we’ll clone the recent sources of btop, which also includes the recent fixes:

$ git clone --depth 1 https://github.com/aristocratos/btop

Next, let’s cd into the directory:

$ cd btop/

Next, we compile the sources:

$ make all

Now, we install the binaries to $HOME/.local/share/btop:

$ make install PREFIX=$HOME/.local/share/btop
Installing binary to: /home/baeldung/.local/share/btop/bin/btop
Installing doc to: /home/baeldung/.local/share/btop/share/btop
...

Notably, it installs to the given directory. Now, let’s take it for a test run:

$ $HOME/.local/share/btop/bin/btop
btop

Additionally, we can uninstall it by specifying the same PREFIX:

$ make uninstall PREFIX=$HOME/.local/share/btop

4. Conclusion

In this article, we learned how to download and install packages to a custom non-root directory. We used several package managers to demonstrate how to download a package from official package repositories and extract it to a custom location.

Finally, we discussed how to compile a package from sources and install it into a custom non-root directory.