How to implement package install suggestion on Debian?

I have noticed that while on Ubuntu, if I type the following:

mc

and it isn’t installed, I get the message below:

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

However in Debian, that is not available. It just gives a “-bash: /usr/bin/mc: No such file or directory” message. How can I implement the same functionality in bash command line on Debian? Yes, I know that if it is package suggestion that I want, I can simply do a regex search using apt-cache search. However I was hoping for the simpler suggestion immediately on typing the name of the program.

As per discussions, the functionality is provided by the package command-not-found. However even after installing it, and also installing bash-completion package, this isn’t available on the Debian bash shell.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The reason that installing command-not-found did not start providing suggestions for non-installed packages was that I had missed a small notification from dpkg as part of the install.

One is supposed to run the command update-command-not-found immediately after running apt-get install command-not-found. In fact dpkg prompts for running this command.

Method 2

The debian package seems to be incomplete. The README says that you should source /etc/bash_command_not_found in your .bashrc. Strangely that file is not included in the package.

The debian command-not-found package is based on the ubuntu package. The ubuntu package seems to be more complete in that regard.

Content of the ubuntu version of bash_command_not_found:

command_not_found_handle() {
  if  [ -x /usr/lib/command-not-found ]; then
     /usr/lib/command-not-found -- "$1" 
     return $?
  else
     return 127
  fi        
}

Add those lines to your ~/.bashrc (or /etc/bash.bashrc) and the command-not-found feature should work.

Method 3

I solve this problem by:

First install command-not-found

sudo apt update
sudo apt install command-not-found

After that

sudo update-command-not-found
sudo apt update

Method 4

If you’re not sure if update is working correctly or if your command-not-found is installed properly, just use

sudo apt reinstall command-not-found

then

sudo apt-get update

and lastly, before updating command-not-found use terminal as a root, by

sudo -i

You’ll see that your hostname (part of the phrase before @) changed to “root”
Then write this command

update-command-not-found

no need to write “sudo” here, because you’re already recognized as a root and sudo didn’t work here for me anyway. Only root access did. Nonetheless it should work fine now.

Method 5

In Debian, after installation of command-not-found, you should issues this command:

apt-get update

or

apt-file update

please note that the command update-command-not-found doesn’t affect. Also you should logout and login again to make the new command-not-found shell scripts in /etc/profile run. Or you can run/source them manually.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x