Installing Missing Packages the Easy Way on Linux

This is a quick tip to making the installation of missing packages in certain Linux flavors easier.

Many Linux distributions come with a program called command-not-found pre-installed (e.g., Ubuntu or Debian), that helps to discover missing packages. Say you just freshly installed Linux and type hg status in a shell. Because hg is not installed yet, you instead see the following:

$ hg status
The program 'hg' is currently not installed. You can install it by typing:
sudo apt-get install mercurial

Now this very useful, especially when the package has a different name from the command you are trying to run (like with mercurial). But why do I have to type (or copy) that message if I want to install the package? Turns out, there is an environment variable which when set, asks to install the package directly, like this:

$ hg status
The program 'hg' is currently not installed. You can install it by typing:
sudo apt-get install mercurial
Do you want to install it? (N/y)

Entering y will install the package. Very convenient. To enable this feature, add the following line to ~/.bashrc (or ~/.zshrc, or wherever your shell initialization is):

export COMMAND_NOT_FOUND_INSTALL_PROMPT=1

Life just got so much better!

Questions or comments? Send me an email or find me on twitter @stefan_heule.