Setting LD_LIBRARY_PATH in Perl

Posted on 2011-04-29
Last Modified: 2013-12-06
Hi All:

I want to set LD_LIBRARY_PATH in perl. I am doing the following:

$libpath = '/users/devtest/tmp/xoslib-';
$ENV{LD_LIBRARY_PATH} .= ':' . $libpath;

Please let me know how to make this work.

Best Regards,

Question by:sunnybrad
    LVL 48

    Expert Comment

    What you have looks correct.

    What makes you think it isn't working?
    LVL 40

    Expert Comment

    Likely you are having the problem that by the time your script needs LD_LIBRARY_PATH, the loader has already run.

    So do this (from Perl monks

    Essentially set it, then exec yourself.

    my $interactive = TRUE if ( ! system( 'tty -s' ) / 256 );
    unless ($interactive)
       unless ( $ENV{BEGIN_BLOCK} ) {
          $ENV{"LD_LIBRARY_PATH"} = '/users/devtest/tmp/xoslib-';
          $ENV{BEGIN_BLOCK} = 1;
          exec 'env',$0,@ARGV;

    Open in new window


    BEGIN {
        my $need= '/usr/local/sybase/lib';
        my $ld= $ENV{LD_LIBRARY_PATH};
        if(  ! $ld  ) {
            $ENV{LD_LIBRARY_PATH}= $need;
        } elsif(  $ld !~ m#(^|:)\Q$need\E(:|$)#  ) {
            $ENV{LD_LIBRARY_PATH} .= ':' . $need;
        } else {
            $need= "";
        if(  $need  ) {
            exec 'env', $^X, $0, @ARGV;

    Open in new window

    Again, credit goes to perl monks (tye and ghodmode), just referencing the sample I've used.

    Author Comment

    Hi Tintin:

    After the program runs I don't see the path getting set.

    pot:110> env | grep LD_L

    Please suggest a solution.

    Best Regards

    LVL 40

    Accepted Solution

    I did post a solution for that exact problem.

    The issue is your Perl interpreter runs in its own environment. Once it exits, the environment goes back to the original shell's env.

    If you want it permanently changed, add it to your .profile or /etc/profile

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now