singha
singha

Reputation: 3561

How to set the environment variable LD_LIBRARY_PATH in linux

I have first executed the command: export LD_LIBRARY_PATH=/usr/local/lib

Then I have opened .bash_profile file: vi ~/.bash_profile. In this file, I put:

LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH

Then if the terminal is closed and restarted, typing echo $LD_LIBRARY_PATH displays no result.

How to set the path permanently?

Upvotes: 349

Views: 1663661

Answers (13)

Yanjan. Kaf.
Yanjan. Kaf.

Reputation: 1725

How about makefile

when you compiler your program, before executing, put

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

into your makefile, it will be executed; and you will be able to run your program, along with other libraries

Upvotes: 1

paperclip optimizer
paperclip optimizer

Reputation: 485

Everyone seems to be missing the forest for the trees.

The real answer is that '~/.bash_profile' is by default only sourced for LOGIN SHELLS.

The bash config file you are probably looking for if you are starting and closing terminals from your desktop GUI is '~/.bashrc', which is the file sourced by default when starting interactive, non-login shells.

https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc

Upvotes: 2

Joseph
Joseph

Reputation: 837

In Ubuntu 20.04 Linux this is just not obvious and straight forward as it should be.

I will attempt to make it simple for anyone who is pulling out their hair just like I was with my Ubuntu 20.04.3 Linux.

Start by identifying the path where your library files' folder is located. In my case, the *.so files that I was working with were located in a folder called libs and this folder's path in my Ubuntu box is /usr/lib

So now I want to add the path /usr/lib to LD_LIBRARY_PATH such that when I run echo $LD_LIBRARY_PATH in my Ubuntu terminal I will be able to see the path /usr/lib echoed as shown below;

joseph$ echo $LD_LIBRARY_PATH
:/usr/lib

Here are the steps I used

  1. Open terminal in Ubuntu 20.04 Linux box
  2. Change path to /etc/ld.so.conf.d/ by running cd /etc/ld.so.conf.d/
  3. Create a file with a *.conf extension at the end with a text editor like e.g. vim or gedit in my case I created it as follows sudo gedit my_project_libs.conf
  4. Inside the .conf file that I created named my_project_libs.conf I added the path to my libs by adding this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib
  5. Thereafter, I then run gedit ~/.bash_profile to open the ~/.bash_profile file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want included in LD_LIBRARY_PATH
  6. I also ran gedit ~/.bashrc to open the ~/.bashrc file so that I can add inside it this line export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib which includes the path to the folder with my libraries /usr/lib that I want included in LD_LIBRARY_PATH
  7. When you are done adding the line in step 5, save and close.
  8. In your terminal, type the following sudo ldconfig and press enter on your keyboard. Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.

In my case, this is what I see :/usr/lib when I run echo $LD_LIBRARY_PATH in my newly opened Ubuntu terminal session

joseph$ echo $LD_LIBRARY_PATH
:/usr/lib

That's how I got it to work for me in my Ubuntu 20.04.3 Linux box.

Upvotes: 3

Ariel Monaco
Ariel Monaco

Reputation: 4102

Keep the previous path, don't overwrite it:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/

You can add it to your ~/.bashrc:

echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/' >> ~/.bashrc

Upvotes: 243

MrUser
MrUser

Reputation: 1237

Add

LD_LIBRARY_PATH="/path/you/want1:/path/you/want/2"

to /etc/environment

See the Ubuntu Documentation.

CORRECTION: I should take my own advice and actually read the documentation. It says that this does not apply to LD_LIBRARY_PATH: Since Ubuntu 9.04 Jaunty Jackalope, LD_LIBRARY_PATH cannot be set in $HOME/.profile, /etc/profile, nor /etc/environment files. You must use /etc/ld.so.conf.d/.conf configuration files.* So user1824407's answer is spot on.

Upvotes: 50

K15.Multik
K15.Multik

Reputation: 639

Alternatively you can execute program with specified library dir:

/lib/ld-linux.so.2 --library-path PATH EXECUTABLE

Read more here.

Upvotes: 44

singingsingh
singingsingh

Reputation: 1434

  1. Go to the home folder and edit .profile
  2. Place the following line at the end

    export LD_LIBRARY_PATH=<your path>

  3. Save and Exit.

  4. Execute this command

    sudo ldconfig

Upvotes: 10

nbroeking
nbroeking

Reputation: 8918

For some reason no one has mentioned the fact that the bashrc needs to be re-sourced after editing. You can either log out and log back in (like mentioned above) but you can also use the commands: source ~/.bashrc or . ~/.bashrc.

Upvotes: 18

Methusael Murmu
Methusael Murmu

Reputation: 103

You could try adding a custom script, say myenv_vars.sh in /etc/profile.d.

cd /etc/profile.d
sudo touch myenv_vars.sh
sudo gedit myenv_vars.sh

Add this to the empty file, and save it.

export LD_LIBRARY_PATH=/usr/local/lib

Logout and login, LD_LIBRARY_PATH will have been set permanently.

Upvotes: 6

Johnnyb
Johnnyb

Reputation: 41

I do the following in Mint 15 through 17, also works on ubuntu server 12.04 and above:

sudo vi /etc/bash.bashrc 

scroll to the bottom, and add:

export LD_LIBRARY_PATH=.

All users have the environment variable added.

Upvotes: 4

Anshul
Anshul

Reputation: 720

Put export LD_LIBRARY_PATH=/usr/local/lib in ~/.bashrc [preferably towards end of script to avoid any overrides in between, Default ~/.bashrc comes with many if-else statements]

Post that whenever you open a new terminal/konsole, LD_LIBRARY_PATH will be reflected

Upvotes: 18

user1824407
user1824407

Reputation: 4559

You should add more details about your distribution, for example under Ubuntu the right way to do this is to add a custom .conf file to /etc/ld.so.conf.d, for example

sudo gedit /etc/ld.so.conf.d/randomLibs.conf

inside the file you are supposed to write the complete path to the directory that contains all the libraries that you wish to add to the system, for example

/home/linux/myLocalLibs

remember to add only the path to the dir, not the full path for the file, all the libs inside that path will be automatically indexed.

Save and run sudo ldconfig to update the system with this libs.

Upvotes: 290

Some programmer dude
Some programmer dude

Reputation: 409166

The file .bash_profile is only executed by login shells. You may need to put it in ~/.bashrc, or simply logout and login again.

Upvotes: 25

Related Questions