Perl: setup LD_LIBRARY_PATH
Blogs20122012-10-11
Perl setting LD_LIBRARY_PATH
When cpan installs Perl module with dynamic share object (.so) loading, it is probably with error like this:
Can’t load module.so: cannot open shared object file: No such file or directory. The error comes from /usr/lib64/perl5/DynaLoader.pm line 82:
if ($ldlibpthname_defined &&
$ldlibpthname ne 'LD_LIBRARY_PATH' &&
exists $ENV{LD_LIBRARY_PATH}) {
push(@dl_library_path, split(/$pthsep/, $ENV{LD_LIBRARY_PATH}));
}It indicates that perl can’t find the relative .so file, and probably the env variable LD_LIBRARY_PATH is not set. The following are several ways to set LD_LIBRARY_PATH for Perl scripts:
-
Add in Perl script itself:
BEGIN { $ENV{LD_LIBRARY_PATH} = "/usr/local/lib"; } # after setup the path, call Perl module which relys on the path: use Text::module;If problem, refer to this: Runtime Linker and LD_LIBRARY_PATH for proper description.
- Add in $HOME/.bash_profile
This is local variable, only available for current login user. Edit $HOME/.bash_profile, adding the following line in the bottom: export LD_LIBRARY_PATH=/usr/local/lib - Add in /etc/profile
This is global variable, will effect all login users. Edit /etc/profile file, adding the following line in the bottom: export LD_LIBRARY_PATH=/usr/local/lib - Operate in command line
This is just for this section, will disappear when logout. In the command line: $ export LD_LIBRARY_PATH=/usr/local/lib $ perl -e ‘use Text:module; …’
It is always a good idea of using solution 1, put initial setting in perl’s BEGIN{} block.
In the crontab, for the variables inherit, we can do like this:
0 1 * * * (export LD_LIBRARY_PATH=/usr/local/lib; $HOME/perl_script >/dev/null 2>&1)
