5

I do rails development. In this app, I need to specify the environment variable LD_LIBRARY_PATH = /usr/local/oracle/lib, but when I run the app with sudo script/server, it doesn't run because that library path is not in root's env.

What should I do to make it work? I tried to put the path under root ./bashrc and it didn't work.

2
  • What is the error message? Sep 3, 2009 at 21:56
  • the error message says it can't find oci8 package. but if I run the app w/o sudo, no error (the app doesn't function correctly within the page w/o using sudo though).
    – Wendy
    Sep 4, 2009 at 3:18

8 Answers 8

6

I had a similar problem. I looked in my /etc/sudoers file and I saw these lines:

Defaults    env_reset
Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY"

To get my environment variable to be there I had to add its name after "XAUTHORITY". In your case you would have:

Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY LD_LIBRARY_PATH"

Give that a try.

Also make sure that you set BASH_ENV="~/.bashrc" in /etc/environment.

See this other answer for more details

2
  • When I emacs'd my /etc/sudoers file, the file's text warned me against modifying the actual text. It asked me to change the /etc/sudoers.d file. Do you agree? Aug 18, 2012 at 3:46
  • I don't have "env_keep" !
    – Dr.jacky
    Dec 6, 2015 at 6:03
2

The way you've reached your root user matters here. Different methods for "logging in" create different environments.

For example:

  • If bash is your root shell, it will read .bashrc upon startup
  • If bash is your root shell, and it is a "login" shell, it will read .bash_profile upon startup
  • If you are logging in via ssh, it will use .ssh/environment as well
  • If you are logging in via login, that is from the console, /etc/pam.d/login will use pam_env.so to read /etc/security/pam_env.conf (depending on the distro) and /etc/environment (if there is readenv=1) on the command line
  • etc.

So, if you are not getting the environment you want - you need to figure out the chain of logins that is leading you to the software you are starting. This might be something like: gdm (X) login -> terminal emulator -> bash shell -> su -> bash shell (root) -> software.

However, if you just want to take the best guess, /etc/environment should be read for every process in a recent distributions.

1

Crazy idea, but are you sure that the server actually runs as root? Some servers specify an account that they run under, so even though you invoke the start script as root they are actually running as another user.

2
  • So I tried: sudo env and it says USER=root That means sudo does act as root, right?
    – Wendy
    Sep 4, 2009 at 3:16
  • 2
    Yes, sudo acts as root. My question was does the start script for running the server do a sudo of it's own so that the server itself is actually running as a different user. Widely distributed servers do this all the time. For example if you run sudo mysqld start, and the script is invoked as root, but the server ends up running as user mysql. This can occassionally mess with things like library paths. Sep 4, 2009 at 12:27
0

There are a number of ways to do this, including the one Insyte suggests above.

If, however, you need to set an environment variable that doesn't belong in /etc/ld.so.conf, you could also simply put it in /etc/environment

3
  • Put $LD_LIBRARY_PATH=/usr/local/oracle_client/v8_1_7/lib in /etc/environment logout and login again.... same error...
    – Wendy
    Sep 3, 2009 at 21:44
  • Wait... are you including the "$" in there? It should just be: LD_LIBRARY_PATH=/usr/local/oracle_client/v8_1_7/lib What error do you get exactly? Can you copy-paste the message here? Sep 4, 2009 at 14:49
  • better, put /usr/local/oracle_client/v8_1_7/lib in /etc/ld.so.conf and run ldconfig
    – araqnid
    Dec 19, 2009 at 12:58
0

If this is a redhat-family distro:

echo 'export LD_LIBRARY_PATH=/usr/local/oracle/lib' > /etc/profile.d/oracle
1
  • I should've mentioned it's ubuntu... is there an equivalent directory as /etc/profile.d/oracle on ubuntu? thanks.
    – Wendy
    Sep 4, 2009 at 3:21
0

Have you tried this:

sudo su - root -c "LD_LIBRARY_PATH=/usr/local/oracle/lib <command to start the app>"

Note that there is no semicolon between setting the environment variable and running the command.

0

There are ways to add it to your script, but I would suggest an easier, more robust approach would be to add the path to the config for the dynamic linker directly:

$ sudo editor /etc/ld.so.conf.d/railslibs

Add the path you mention in your question to that file and run sudo ldconfig.

That should make an env munging unnecessary.

2
  • Followed your approach and I found if I sudo su - , I can see the root does have that library path. However, I still can't get the app up and running, still got the same error message. Any idea?
    – Wendy
    Sep 3, 2009 at 21:20
  • Hmmm... My point was that if you add it to the ld config, you shouldn't need to have it in LD_LIBRARY_PATH. If you run "sudo ldconfig -v" does your oracle directory show up in the output?
    – Insyte
    Sep 4, 2009 at 16:54
0

You could try setting the environment variable from within the rails process try adding something like below in $RAILS_ROOT/config/environment.rb

Rails::Initializer.run do |config|
   ENV['LD_LIBRARY_PATH'] = "/usr/local/oracle_client/v8_1_7/lib"
end

you probably need to make sure this gets set before you add the require or gem statements for you oracle database bindings.

You must log in to answer this question.