11

I am thinking of the use case of this output:

The program 'tiger' is currently not installed. You can install it by typing:
sudo apt-get install tiger

How can I get it to prompt me to install that package? For example it would output this:

The program 'tiger' is currently not installed. You can install it by typing:
sudo apt-get install tiger
# Confirmation can go here
[sudo] password for tim:
The following NEW packages will be installed
  tiger tripwire
0 to upgrade, 11 to newly install, 0 to remove and 0 not to upgrade.
Need to get 8,416 kB of archives.
After this operation, 26.5 MB of additional disk space will be used.
Do you want to continue? [Y/n]  # and/or confirmation can go here

I don't want to have to run it myself. How can I get it to do it automatically, and give me the option to not install?

Tim
  • 32,861
  • 27
  • 118
  • 178
  • 1
    @EliahKagan That's fine, it will feel like a normal workflow: Can't install -> Want to install this one thing? -> Start installing -> It's not just one thing, sure you want to continue? -> Install. – Tim May 10 '15 at 15:28

1 Answers1

23

If you set the environment variable COMMAND_NOT_FOUND_INSTALL_PROMPT to 1, like

export COMMAND_NOT_FOUND_INSTALL_PROMPT=1

you will be ask if you want to install the package:

me@myhost:~$ tiger
The program 'tiger' is currently not installed. You can install it by typing:
sudo apt-get install tiger
Do you want to install it? (N/y)

If you answer y it will run

sudo apt-get install tiger

See How do I set environment variables? for how to set environment variables.

N0rbert
  • 99,918
  • What is it by default? And can I make Y the default, not N? – Tim May 10 '15 at 15:51
  • By default the variable is not set. As of 15.04 there is no way to make Y the default. – Florian Diesch May 10 '15 at 16:12
  • 3
    Very nice! Where is this documented? I had to check the source code of /usr/lib/python3/dist-packages/CommandNotFound/CommandNorFound.py which is called by usr/lib/command-not-found which is the script called when a command isn't found to find it. It seems to be an Ubuntu feature (I don't have it on my Debian). Is it mentioned somewhere in the Ubuntu docs? Also, why can't you make Y the default? Just set it in ~/.profile or /etc/profile if you want it for all users. – terdon May 10 '15 at 17:17
  • This is so awesome! – Wilhelm Erasmus May 10 '15 at 19:26
  • 1
    @FlorianDiesch Cool! I had not been aware of this feature at all. This answer has freed me from ignorance I didn't even know I had. Btw in light of this that question on Super User might be considered an XY problem; you might want to post an answer there, too. – Eliah Kagan May 10 '15 at 23:05