Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

If you try to execute a program that is not installed you will get a message

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

and instead of offering you to install the program for you, you have to type the actual command. How can this behavior be changed?

share|improve this question

3 Answers 3

The package that is responsible for this behavior is the command-not-found Install command-not-found package, and it already includes the desired behavior although it is disabled by default. You can enable it by adding the following line to your ~/.bashrc

export COMMAND_NOT_FOUND_INSTALL_PROMPT=1

Now the behavior is changed to

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

Note: If you get a message about python crashing (specifically AttributeError: 'str' object has no attribute 'decode') this is bug 1073919. A fix is already available for Raring and Saucy. Alternatively (advanced users only), you can change the file /usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py, see the diff attached to the bugreport for the lines that need to be changed.

Edit: The fix is now released in version 0.3ubuntu7.1 in Raring (SRU) and for 0.3ubuntu8 in Saucy.

share|improve this answer

My solution is an hybrid between security and comfortability:

auto-apt checks the file access of programs running within its environments, and if a program tries to access a file known to belong in an uninstalled package, auto-apt will install that package using apt-get. This feature requires apt and sudo to work. It also provides simple database to search which package contains a requested file.

It's a terminal-emulator where you enter a environment where each file access is verified before hand by auto-apt and if it not exist it search in the database (a la apt-file) and download and install the desired package, then allows the program to continue. You should execute it as root:

sudo auto-apt

This is useful in cases where you are building a package and isn't in the mood to be in front of the screen looking for each configure error for missing headers, etc.

This also can be used as apt-get + apt-file replacement where you can install programs just telling the command to execute it.

You must understand that you should not leave it opened when your system isn't looked, since it's almost as if you were using the root user.

Man page: http://derpi.tuwien.ac.at/cgi-bin/man/man2html?1+auto-apt

share|improve this answer

Auto installing would be a bad idea. If you want to save some typing you can use

sudo apt-get install !!

where !! will be replaced by the last command from history. You probably could make this an alias as well, but I don't know how to add !! without evaluating it.

There are some caveats though, as mentioned by @eliah-kagan and @gertvdijk in the comments. Some programs doesn't have the same package name as the binary you would execute. Also, when passing arguments to the binary, this could result in some unexpected results.

share|improve this answer
    
Sure, I know that my suggestion isn't a great solution, but it could work "good enough" based on the users need. –  l3dx Aug 16 '13 at 7:48
5  
Command names (filename of the binary on the system) aren't necessarily the same as the name of the package. –  gertvdijk Aug 16 '13 at 7:51
3  
l3dx: Although I still have my misgivings (including @gertvdijk's point that package and command names are often different, as well as concerns about possibly very bad effects of additional command-line arguments passed to the original command), it seems you are onto something. After a bit of testing, it appears this works better in practice than (I think) it works in theory. I do still recommend expanding this answer for clarification, warning, and perhaps to detail other options. In any case, I had downvoted this, but have removed my downvote. –  Eliah Kagan Aug 16 '13 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.