How can I check if a Perl module is installed on my system from the command line?
The above one-liner was used for listing all modules installed in my system. However, it is not listing XML modules. However, the following executes fine.
@Sinan: you changed the question a bit too much. I think the original question was how to find out where a module is installed. Now it’s whether a module is installed.
I really have no idea. But considering the current question, your answer is pretty much the best so far. Maybe Chells can enlighten us.
10 Answers 10
You can check for a module’s installation path by:
The problem with your one-liner is that, it is not recursively traversing directories/sub-directories. Hence, you get only pragmatic module names as output.
Some of them are so well documented that they have their docs in their own .pod files. It’s a nice trick that works most of the time with that one little gotcha.
You should use -lm instead of -l . It the’s same, except that it also works if the module has no POD.
To the anonymous editor: The correct way would be perl -MDigest::SHA -e 1 rather than perl -Digest::SHA -e 1 .
$ perl -MXML::Simple -le 'print $INC'
For example, to check if the DBI module is installed or not, use
You will see error if not installed. (from http://www.linuxask.com)
If you want to quickly check if a module is installed (at least on Unix systems, with Bash as shell), add this to your .bashrc file:
alias modver="perl -e\"eval qq::VERSION;>;\ print\\\$@?qq:\\\$v?qq:qq;\"\$1"
=> modver XML::Simple No module found => modver DBI Version 1.607
What you’re doing there is not recursing into directories. It is only listing the modules in the root directory of the @INC directory.
The module XML::Simple will live in one of the @INC paths under XML/Simple.pm .
What he said above to find specific modules.
CPAN explains how to find all modules here, see How to find installed modules.
This joins the paths in @INC together in a string, separated by spaces, then calls glob() on the string, which then iterates through the space-separated components (unless there are file-globbing meta-characters.)
This doesn’t work so well if there are paths in @INC containing spaces, \, [], <>, *, ?, or ~, and there seems to be no reason to avoid the safe alternative:
If you’re running ActivePerl under Windows:
- C:\>ppm query * to get a list of all installed modules
- C:\>ppm query XML-Simple to check if XML::Simple is installed
I believe your solution will only look in the root of each directory path contained in the @INC array. You need something recursive, like:
This doesn’t properly quote $_ ; if it contains spaces, it’ll break at the words; if it contains ; it’ll break the command (shell injection) — quote using either String::ShellQuote or the not-quite optimal solution of using the quotemeta built-in.
Bravo for @user80168’s solution (I’m still counting \ ‘s !) but to avoid all the escaping involved with aliases and shells:
%~/ cat ~/bin/perlmod perl -le'eval qq ? print ( "Found $ARGV[0] Version: ", eval "$ARGV[0]->VERSION" ) : print "Not installed" ' $1
Here might be the simplest and most «modern» approach, using Module::Runtime :
perl -MModule::Runtime=use_module -E ' say "$ARGV[0] ", use_module($ARGV[0])->VERSION' DBI
This will give a useful error if the module is not installed.
Using -MModule::Runtime requires it to be installed (it is not a core module).
How to check if a package is installed from Bash?
I need to check if a specific package is installed on a machine from within a Bash script. I found something like that but I don’t know how use it correctly.
dpkg -l | grep "ansible" | awk '' if [$? -eq 0]; then echo OK else echo FAIL fi
I need check, if command dpkg -l | grep «ansible» | awk ‘
dpkg -l | grep "ansible" | awk ''
ansible = $? if [$? -eq 0]; then echo OK else echo FAIL fi
that’s not work, but I’m sure I doing that wrong. How can I read result from dpkg -l | grep «ansible» | awk ‘
What do you want to do by that code? Do you want to check if a package whose name contains ‘ansible’ is installed? All lines printed by dpkg -l do not indicate installed packages. There may be removed packages, too. Note also that dpkg -l list much more than just package names, so you have to be more careful, if you examine its output by grep .
3 Answers 3
You can check if the software is installed on this way:
if [ "$(dpkg -l | awk '/ansible/ '|wc -l)" -ge 1 ]; then echo OK else echo FAIL fi
You can’t use exit code because it will be from awk and in this case always be 0
Great! I have one more question. Can you tell me how should it look, if I need check software is installed in pip so command pip list. If I check dpkg -l for pip list I got that message in terminal DEPRECATION: The default format will switch to columns in the future. You can use —format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
@BElluu, please use «Ask Question» button and create new question. But in general you can replace dpkg -l with pip list to use as source the list of packages, installed with pip
This is bad practice in several ways: there could be several packages whose name contain string «ansible», but it could be that none of them is named «ansible». The string could be something else in the output of dpkg -l than package name. Why use awk here, as the same could be done by grep ‘ansible’ that may give non-zero exit code? wc -l is not needed in checking if a string is not null in Bash. So I wonder why this is accepted answer. I have given another answer.
If you know the exact package name, you can just ask dpkg if it’s installed with
$ dpkg -l pulsea dpkg-query: no packages found matching pulsea
The exit code is also 1 (fail) if a package isn’t installed, you can test for that (as seen later).
$ dpkg -l pulseaudio Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-==================-==============-==============-========================= ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server
Here the exit code is 0 (success), so you can do this too
$ if dpkg -l pulseaudio; then echo yes;fi Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-==================-==============-==============-========================= ii pulseaudio 10.0-1+deb9u1 i386 PulseAudio sound server yes
Note the trailing «yes» above. But now since you can just use the exit code, you don’t really care about dpkg’s output, so ignore it with an if or an && (AND list):
$ if dpkg -l pulseaudio >/dev/null; then echo yes;fi yes $ dpkg -l pulseaudio >/dev/null && echo yes yes
dpkg can also match partial names, using asterisks, like
About pipes and their exit status, if you want to see if a command somewhere in a pipeline has failed you’ll have to do something like examining the $ :
And like $? , $ changes with every command, so if you want to examine them more than once you have to save them to another variable first. In your example
$? has already changed by the if test, and it’s probably 0 since assigning a variable like that almost always succeeds.
How to determine if a specific module is loaded in linux kernel
I am just curious is there any way to determine if a particular module is loaded/installed. $ lsmod lists all modules (device driver loaded). Is there any way to check or a command that returns true/false boolean output if a module name is polled. For eg. if keyboard.o exists return true else false. I need this tip to complete my driver auto refresh program. PS: tried modinfo. I am using busybox client in my test DUT so can you give some alternatives other than modinfo?
The question is a bit ambiguous. Are you trying to check if the driver is loaded into memory or installed on the system? modinfo would help with the latter but not the former.
10 Answers 10
The modinfo module method does not work well for me. I prefer this method that is similar to the alternative method proposed:
#!/bin/sh MODULE="$1" if lsmod | grep -wq "$MODULE"; then echo "$MODULE is loaded!" exit 0 else echo "$MODULE is not loaded!" exit 1 fi
not sure if modinfo modname and checking $? will work for you, just a suggestion.
/tmp$ sudo modinfo e1000 /tmp$ echo $? 0 /tmp$ sudo modinfo keyboard ERROR: modinfo: could not find module keyboard /tmp$ echo $? 1
alternatively you also grep /proc/modules
Just a note: the modinfo approach doesn’t seem to actually work for the loaded check (as in the question title). It shows info regardless of whether the module is loaded.
This is not a good solution. modinfo shows information of a kernel module installed on the rootfs, but it doesn’t check if the module is loaded into the kernel or not. So this solution should not be used to solve the issue.
The —first-time flag causes modprobe to fail if the module is already loaded. That in conjunction with the —dry-run (or the shorthand -n ) flag makes a nice test:
modprobe -n --first-time $MODULE && echo "Not loaded" || echo "Loaded"
Edit 1: As @Nobody pointed out this also prints Loaded if the module does not exist. We can fix this by combining it with modinfo :
modinfo $MODULE >/dev/null 2>/dev/null && ! modprobe -n --first-time $MODULE 2>/dev/null && echo "Loaded" || echo "Not loaded"
Edit 2: On some systems modprobe lives in /usr/sbin , which is not in the $PATH unless you are root. In that case you have to substitute modprobe for /usr/sbin/modprobe in the above.
Oh, sorry, I was only half-right (or maybe quarter-right. Shame on you comment-up-voters). On Debian, modprobe is not in $PATH for normal users, so just copy&pasting your command only works as root. But when calling modprobe with its full path, it’s executable for normal users and only the actual inserting operation fails, so your solution works in principle. I still think querying /proc/modules is more elegant, but that’s a matter of taste. Had I downvoted your answer, I would have removed the vote now (but I didn’t).
MODULE=snd_aloop # for example test -n "$(grep -e "^$MODULE " /proc/modules)" && echo "Loaded" || echo "Not loaded"
It checks in /proc/modules . If the module is mentioned there, it’s assumed to be loaded, otherwise not.
The others seemed too long to me (the other short one requires root, this does not). Of course it’s just written out what was already mentioned as «alternatives».
Caution: modprobe accepts some variants of module names other than the primary listed in /proc/modules . For example loading snd-aloop works, but the module is named snd_aloop and is listed as such in /proc/modules and when using rmmod that’s also the only name that will work.