GNU stow is an underrated tool. Generically, it helps maintain a unified tree of files that come from different sources. More concretely, I use a bunch of software (D compilers, various tools) that I install manually instead of through my system’s package manager (for various reasons). stow makes that maintainable by letting me cleanly add/remove packages and switch between versions. Here’s how it’s done.
The ~/local/ directory
The idea is simple: you stow install all personal software inside a local/ directory inside your home directory. The resulting directory structure looks the same as if you installed the software normally to the filesystem root, so you’ll end up with ~/local/bin and ~/local/lib directories, etc.
Setting up the local/ directory for use with stow is easy. The main thing you need is a local/ directory in your home directory, with a stow/ subdirectory to store package archives:
If you’re installing programs into your local/ directory, you probably want to add local/bin to your PATH so you can easily use programs there like other programs. You can add this to the end of your ~/.profile file (or whatever init file is used by your shell):
Downloading and installing tarball packages
I like tsv-utils, a handy collection of tools for data analysis on the command line. It’s not in the normal package managers I use, but there are pre-compiled tarball archives available. Here’s how to use them with stow.
First, switch to your stow archive directory:
Then download the tarball and extract it:
You’ll now have a directory containing all the package files:
You can delete the .tar.gz archive if you want.
Now you can install the package into local/ with stow:
That creates a bunch of symbolic links inside the parent directory (~/local/) pointing to files and directories inside the package directory (~/local/stow/tsv-utils-v2.2.0_linux-x86_64_ldc2).
If you’ve set your PATH (you might need to restart your shell), you’ll now be able to run tsv-utils commands normally:
Okay, stow’s algorithm for managing symbolic links is neat, but so far there’s no practical benefit over extracting the tarball directly into local/. stow shines when you’re maintaining your package collection. For example, if you decide to uninstall tsv-utils later, you just need to switch to the archive directory and run stow again with the -D flag:
That will cleanly remove tsv-utils from the local/ directory without breaking any other installed packages. Try doing that after extracting the tarball directly to local/.
The package directory inside the stow/ directory will remain, but you can delete that too, if you want, of course.
stow doesn’t manage versions, so upgrading packages means uninstalling the old package and installing the new package. stow detects when packages collide (e.g., they both include a file called bin/tsv-summarize), so you can only install one version at a time. However, you can keep as many archive directories as you like in stow/, allowing you to easily switch back and forth between versions if you need to.
Building packages from source
Not all software comes precompiled. Sometimes you’re experimenting with your own custom version. If you want to use source packages with stow, you just need to figure out how to make the source package install to a directory in your stow/ directory, instead of your filesystem root.
Suppose I want to install my own version of the GraphicsMagick image processing tools. This will be a two-stage process. First I’ll need to download and extract the source somewhere (I keep a src/ directory for third-party source code).
GraphicsMagick uses a GNU-style build system using autotools. configure scripts take a --prefix option that sets the installation root.
The installation step automatically creates the stow/GraphicsMagick-1.3.36/ directory. Now I just need to install the built package with stow.
Other uses
This is my personal favourite usage of stow, but it’s just a generic tool for merging multiple filesystem trees in a maintainable way. Some people use it to manage their /etc/ configuration files, for example. If you try it out, I’m sure you can find other use cases.