William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories


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:

  1. 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.

  2. 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
  3. 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
  4. 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)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: