# Can $LOAD\_PATH be relative?

**URL:** <https://rubytalk.org/t/can-load-path-be-relative/10664>\
**Category:** ruby-talk\
**Created:** [19 March 2004 19:54 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664 "2004-03-19T19:54:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ptkwt](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ptkwt](https://rubytalk.org/u/Ptkwt)\
**Post date:** [19 March 2004 19:54 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/1 "2004-03-19T19:54:32Z")

</div>

Question based on an assertion made in another thread…

I was under the impression that $LOAD\_PATH is relative to the current  
location of the ruby executable. On Windows this certainly seems to be  
the case. I can move ruby.exe and see that $LOAD\_PATH changes.

However, on Linux this doesn’t seem to be the case, no matter where I put  
the ruby binary, $LOAD\_PATH points to /usr/local/lib/ruby/…

Is there anyway to make $LOAD\_PATH relative on Linux (and I suspect also  
on Windows if you use MingW to compile) just as it is on the Windows  
mswin32 build?

…I’m hoping this is just a configure command-line switch since I need to  
put the Ruby binary and some libs on a CDROM and have it execute from  
there.

Phil

---

<div class="post-metadata">

**Author:** ![Jim\_Menard3](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jim\_Menard3](https://rubytalk.org/u/Jim_Menard3)\
**Post date:** [19 March 2004 20:39 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/2 "2004-03-19T20:39:34Z")

</div>

[ptkwt@aracnet.com](mailto:ptkwt@aracnet.com) (Phil Tomson) writes:

> Question based on an assertion made in another thread…
> 
> I was under the impression that $LOAD\_PATH is relative to the current  
> location of the ruby executable. On Windows this certainly seems to be  
> the case. I can move ruby.exe and see that $LOAD\_PATH changes.
> 
> However, on Linux this doesn’t seem to be the case, no matter where I put  
> the ruby binary, $LOAD\_PATH points to /usr/local/lib/ruby/…
> 
> Is there anyway to make $LOAD\_PATH relative on Linux (and I suspect also  
> on Windows if you use MingW to compile) just as it is on the Windows  
> mswin32 build?
> 
> …I’m hoping this is just a configure command-line switch since I need to  
> put the Ruby binary and some libs on a CDROM and have it execute from  
> there.

You can add to $LOAD\_PATH when the script starts, and those paths can be  
relative to the script itself. Does that help? For example,

```
HERE = File.dirname( __FILE__ ) # __FILE__ does not alwyas == $0
$LOAD_PATH << HERE # Add script's directory
$LOAD_PATH << File.join(HERE, '..') # Add directory above script

```

Since you are in control of the directory structure on the CD, you should be  
able to find the Ruby libs relative to the script.

You could also use the -I command line switch or the environment variable  
RUBYLIB to add to the list of directories to search.

Jim

> **···**
>
> –  
> Jim Menard, [jimm@io.com](mailto:jimm@io.com), [http://www.io.com/~jimm/](http://www.io.com/~jimm/)  
> “Is ‘anal-retentive’ hyphenated?” – Alison Bechdel

---

<div class="post-metadata">

**Author:** ![gga](https://avatars.discourse-cdn.com/v4/letter/g/b487fb/32.png) [@gga](https://rubytalk.org/u/gga)\
**Post date:** [19 March 2004 22:34 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/3 "2004-03-19T22:34:32Z")

</div>

[ptkwt@aracnet.com](mailto:ptkwt@aracnet.com) (Phil Tomson) wrote in message news:[c3fhqv028i3@enews2.newsguy.com](mailto:c3fhqv028i3@enews2.newsguy.com)…

> Question based on an assertion made in another thread…
> 
> I was under the impression that $LOAD\_PATH is relative to the current  
> location of the ruby executable. On Windows this certainly seems to be  
> the case. I can move ruby.exe and see that $LOAD\_PATH changes.
> 
> However, on Linux this doesn’t seem to be the case, no matter where I put  
> the ruby binary, $LOAD\_PATH points to /usr/local/lib/ruby/…
> 
> Is there anyway to make $LOAD\_PATH relative on Linux (and I suspect also  
> on Windows if you use MingW to compile) just as it is on the Windows  
> mswin32 build?
> 
> …I’m hoping this is just a configure command-line switch since I need to  
> put the Ruby binary and some libs on a CDROM and have it execute from  
> there.
> 
> Phil

In a shell window, set the environment variable RUBYLIB with the list  
of paths you want to use.

---

<div class="post-metadata">

**Author:** ![Ptkwt](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ptkwt](https://rubytalk.org/u/Ptkwt)\
**Post date:** [19 March 2004 21:09 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/4 "2004-03-19T21:09:36Z")

</div>

True, I could do those things, but I’m wondering how they managed to make  
$LOAD\_PATH relative on Windows?

Phil

> **···**
>
> In article [wsqk71gd188.fsf@io.com](mailto:wsqk71gd188.fsf@io.com), Jim Menard [jimm@io.com](mailto:jimm@io.com) wrote:
> 
> > [ptkwt@aracnet.com](mailto:ptkwt@aracnet.com) (Phil Tomson) writes:
> > 
> > > Question based on an assertion made in another thread…
> > > 
> > > I was under the impression that $LOAD\_PATH is relative to the current  
> > > location of the ruby executable. On Windows this certainly seems to be  
> > > the case. I can move ruby.exe and see that $LOAD\_PATH changes.
> > > 
> > > However, on Linux this doesn’t seem to be the case, no matter where I put  
> > > the ruby binary, $LOAD\_PATH points to /usr/local/lib/ruby/…
> > > 
> > > Is there anyway to make $LOAD\_PATH relative on Linux (and I suspect also  
> > > on Windows if you use MingW to compile) just as it is on the Windows  
> > > mswin32 build?
> > > 
> > > …I’m hoping this is just a configure command-line switch since I need to  
> > > put the Ruby binary and some libs on a CDROM and have it execute from  
> > > there.
> > 
> > You can add to $LOAD\_PATH when the script starts, and those paths can be  
> > relative to the script itself. Does that help? For example,
> > 
> > HERE = File.dirname( **FILE** ) # **FILE** does not alwyas == $0  
> > $LOAD\_PATH \<\< HERE # Add script’s directory  
> > $LOAD\_PATH \<\< File.join(HERE, ‘…’) # Add directory above script
> > 
> > Since you are in control of the directory structure on the CD, you should be  
> > able to find the Ruby libs relative to the script.
> > 
> > You could also use the -I command line switch or the environment variable  
> > RUBYLIB to add to the list of directories to search.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [20 March 2004 12:58 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/5 "2004-03-20T12:58:37Z")

</div>

> In a shell window, set the environment variable RUBYLIB with the list  
> of paths you want to use.

Try it with -T1

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Bennett\_Patrick](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@Bennett\_Patrick](https://rubytalk.org/u/Bennett_Patrick)\
**Post date:** [19 March 2004 21:42 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/6 "2004-03-19T21:42:17Z")

</div>

Phil Tomson wrote:

> True, I could do those things, but I’m wondering how they managed to make  
> $LOAD\_PATH relative on Windows?

Because Ruby was coded to work that way. Why it wasn’t made to do it  
under \*nix, I have no idea. I think it’s a critical requirement,  
otherwise having private copies of Ruby installs is impossible. It will  
always go to /usr/lib/ruby, etc. no matter where Ruby is run from.  
After my message about this subject was ignored (Installation/Config  
question), I decided to just write it myself and patch Ruby.

Here’s what I did.  
After ./configure - the bottom of config.h must be changed to be rooted  
off of /lib (the rest stays the same)  
#define LOAD\_RELATIVE 1 must also be added.

(here was what mine looked like)  
#define RUBY\_LIB “/lib/ruby/1.8”  
#define RUBY\_SITE\_LIB “/lib/ruby/site\_ruby”  
#define RUBY\_SITE\_LIB2 “/lib/ruby/site\_ruby/1.8”  
#define RUBY\_PLATFORM “i686-linux”  
#define RUBY\_ARCHLIB “/lib/ruby/1.8/i686-linux”  
#define RUBY\_SITE\_ARCHLIB “/lib/ruby/site\_ruby/1.8/i686-linux”  
#define LOAD\_RELATIVE 1

in ruby.c in the ruby\_init\_loadpath() method I added a #else section  
after the #elif defined(\_EMX) which was withih the #if defined  
LOAD\_RELATIVE section.  
It looked something like this:  
#elif defined( **EMX** )  
\_execname(libpath, FILENAME\_MAX);  
#else  
/\* pmb 03/01/2004 \*/  
buf = malloc(size);  
while ((rv = readlink(“/proc/self/exe”, buf, size)) == size) {  
size \*= 2;  
buf = realloc(buf, size);  
}  
if (rv \< 0 || rv \>= FILENAME\_MAX) {  
free(buf);  
fprintf(stderr, “Unable to get path to self through  
/proc/self/exe”);  
exit(1);  
}  
strncpy(libpath, buf, rv);  
libpath[rv] = ‘\0’;  
free(buf);  
#endif

> **···**
>
> >

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [19 March 2004 21:49 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/7 "2004-03-19T21:49:26Z")

</div>

Hi –

> **···**
>
> On Sat, 20 Mar 2004, Patrick Bennett wrote:
> 
> > Phil Tomson wrote:
> > 
> > > True, I could do those things, but I’m wondering how they managed to make  
> > > $LOAD\_PATH relative on Windows?
> > 
> > Because Ruby was coded to work that way. Why it wasn’t made to do it  
> > under \*nix, I have no idea. I think it’s a critical requirement,  
> > otherwise having private copies of Ruby installs is impossible. It will  
> > always go to /usr/lib/ruby, etc. no matter where Ruby is run from.  
> > After my message about this subject was ignored (Installation/Config  
> > question), I decided to just write it myself and patch Ruby.
> 
> I’ve always been able to install private Rubies. I just set --prefix  
> when ./configure’ing. Does that not do what you need?
> 
> David
> 
> –  
> David A. Black  
> [dblack@wobblini.net](mailto:dblack@wobblini.net)

---

<div class="post-metadata">

**Author:** ![Jim\_Menard3](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jim\_Menard3](https://rubytalk.org/u/Jim_Menard3)\
**Post date:** [19 March 2004 21:59 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/8 "2004-03-19T21:59:33Z")

</div>

Patrick Bennett [patrick.bennett@inin.com](mailto:patrick.bennett@inin.com) writes:

> Phil Tomson wrote:
> 
> > True, I could do those things, but I’m wondering how they managed to make  
> > $LOAD\_PATH relative on Windows?
> 
> Because Ruby was coded to work that way. Why it wasn’t made to do it  
> under \*nix, I have no idea.

Under Unix you specify the path at compile time. The default is  
/usr/local/{bin,lib} but you can change that with arguments to configure.

Jim

> **···**
>
> –  
> Jim Menard, [jimm@io.com](mailto:jimm@io.com), [http://www.io.com/~jimm/](http://www.io.com/~jimm/)  
> “Yeah, well, don’t count your weasels before they pop, dink.” – The Tick

---

<div class="post-metadata">

**Author:** ![Bennett\_Patrick](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@Bennett\_Patrick](https://rubytalk.org/u/Bennett_Patrick)\
**Post date:** [19 March 2004 21:59 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/9 "2004-03-19T21:59:35Z")

</div>

David A. Black wrote:

> I’ve always been able to install private Rubies. I just set --prefix  
> when ./configure’ing. Does that not do what you need?

No, I needed to define a Ruby tree that could be installed (through  
source control checkout) _anywhere_ by a user. ‘Fixed’ paths are IMO,  
unacceptable. It works quite well in Windows. I was really surprised  
to find that it was all hard-coded for \*nix installs.

---

<div class="post-metadata">

**Author:** ![Bennett\_Patrick](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@Bennett\_Patrick](https://rubytalk.org/u/Bennett_Patrick)\
**Post date:** [19 March 2004 22:04 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/10 "2004-03-19T22:04:59Z")

</div>

Jim Menard wrote:

> Under Unix you specify the path at compile time. The default is  
> /usr/local/{bin,lib} but you can change that with arguments to configure.

Yes, I know. This wasn’t acceptable for my needs. Thanks though…

---

<div class="post-metadata">

**Author:** ![Ptkwt](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ptkwt](https://rubytalk.org/u/Ptkwt)\
**Post date:** [20 March 2004 18:39 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/11 "2004-03-20T18:39:30Z")

</div>

Is there an option to configure to make it relative like is is on Windows  
or did they do it with ‘#if defined’ pre-processor statements in the C  
Code.

Phil

> **···**
>
> In article [wsq1xnofr5p.fsf@io.com](mailto:wsq1xnofr5p.fsf@io.com), Jim Menard [jimm@io.com](mailto:jimm@io.com) wrote:
> 
> > Patrick Bennett [patrick.bennett@inin.com](mailto:patrick.bennett@inin.com) writes:
> > 
> > > Phil Tomson wrote:
> > > 
> > > > True, I could do those things, but I’m wondering how they managed to make  
> > > > $LOAD\_PATH relative on Windows?
> > > 
> > > Because Ruby was coded to work that way. Why it wasn’t made to do it  
> > > under \*nix, I have no idea.
> > 
> > Under Unix you specify the path at compile time. The default is  
> > /usr/local/{bin,lib} but you can change that with arguments to configure.

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [20 March 2004 12:59 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/12 "2004-03-20T12:59:49Z")

</div>

> No, I needed to define a Ruby tree that could be installed (through  
> source control checkout) \*anywhere\* by a user. 'Fixed' paths are IMO,  
> unacceptable. It works quite well in Windows. I was really surprised  
> to find that it was all hard-coded for \*nix installs.

security problem

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Sam\_Roberts1](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@Sam\_Roberts1](https://rubytalk.org/u/Sam_Roberts1)\
**Post date:** [20 March 2004 18:49 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/13 "2004-03-20T18:49:14Z")

</div>

Quoteing [ptkwt@aracnet.com](mailto:ptkwt@aracnet.com), on Sun, Mar 21, 2004 at 03:39:30AM +0900:

> > Under Unix you specify the path at compile time. The default is  
> > /usr/local/{bin,lib} but you can change that with arguments to configure.
> 
> Is there an option to configure to make it relative like is is on Windows

No, there is no such option, and it is not possible to reliably  
implement such an option on unix:

[http://www.erlenstar.demon.co.uk/unix/faq\_2.html#SEC23](http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC23)

Sam

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [21 March 2004 10:57 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/14 "2004-03-21T10:57:16Z")

</div>

> No, there is no such option, and it is not possible to reliably  
> implement such an option on unix:

Never use a command like strings, or you'll have a surprise

Never use an editor, or you'll learn how modify a file.

It's easy to do what he want, I see no good reasons to make it easier.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Bennett\_Patrick](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@Bennett\_Patrick](https://rubytalk.org/u/Bennett_Patrick)\
**Post date:** [20 March 2004 17:18 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/15 "2004-03-20T17:18:01Z")

</div>

ts wrote:

> > > > > > “P” == Patrick Bennett [patrick.bennett@inin.com](mailto:patrick.bennett@inin.com) writes:
> 
> > No, I needed to define a Ruby tree that could be installed (through  
> > source control checkout) _anywhere_ by a user. ‘Fixed’ paths are IMO,  
> > unacceptable. It works quite well in Windows. I was really surprised  
> > to find that it was all hard-coded for \*nix installs.
> 
> security problem

…and it wouldn’t be under Windows? Sorry, I don’t buy it. ☹

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [20 March 2004 17:23 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/16 "2004-03-20T17:23:46Z")

</div>

> ts wrote:

> > security problem
> 
> ...and it wouldn't be under Windows? Sorry, I don't buy it. ☹

Where I've said that it's not a security problem under Windows ?

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Sam\_Roberts1](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@Sam\_Roberts1](https://rubytalk.org/u/Sam_Roberts1)\
**Post date:** [20 March 2004 18:20 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/17 "2004-03-20T18:20:25Z")

</div>

Quoteing patrick.bennett@inin.com, on Sun, Mar 21, 2004 at 02:18:01AM +0900:

> \>\> No, I needed to define a Ruby tree that could be installed (through  
> \>\> source control checkout) \*anywhere\* by a user. 'Fixed' paths are IMO,  
> \>\> unacceptable. It works quite well in Windows. I was really surprised  
> \>\> to find that it was all hard-coded for \*nix installs.  
> \>  
> \>security problem

How (it's impossible) to find a process's executable file in Unix is a  
FAQ:

&nbsp;&nbsp;[http://www.erlenstar.demon.co.uk/unix/faq\_2.html#SEC23](http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC23)

Executables in unix don't have a knowledge of "where they are". They  
don't, in fact, have to "be" anywhere. Some are even built into the boot  
image, so don't in fact have to have ever "been" anywhere.

Do I recall you saying that you'd hacked ruby to get it's executable  
location? I'd like to see that hack, I'm pretty confident I can think up  
a number of cases where it wouldn't work.

Also, why would you think that something that is a security concern in  
unix would be in windows? Windows isn't even a multi-user OS! It also  
doesn't support hard or symbolic links, or set-user-id flags on  
executables, or allow you to delete a running executable, you can in  
unix.

Cheers,  
Sam

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [20 March 2004 18:28 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/18 "2004-03-20T18:28:15Z")

</div>

[…]

> Sam

Re read carefully my message (there is only 2 words) : I really don’t  
understand your reply.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Ceri\_Storey](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Ceri\_Storey](https://rubytalk.org/u/Ceri_Storey)\
**Post date:** [20 March 2004 18:51 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/19 "2004-03-20T18:51:04Z")

</div>

> > > security problem
> 
> […]
> 
> Executables in unix don’t have a knowledge of “where they are”. They  
> don’t, in fact, have to “be” anywhere. Some are even built into the boot  
> image, so don’t in fact have to have ever “been” anywhere.

Well, on some operating systems it’s certainly possible to find the  
executable image corresponding to a process (eg: Linux) and on others  
you can find the device number / inode no. of each text section within  
the process.

Of course, none of this is really important right now, since I belive he  
was referring to the fact that a program could be fooled into reading a  
different configuration file to the one the administrator intended, if a  
user were to either copy or create a link to the binary somewhere they  
had write access to. Of course, it shouldn’t be a problem on a well  
configured system, but that’s something of an ideal.

> Do I recall you saying that you’d hacked ruby to get it’s executable  
> location? I’d like to see that hack, I’m pretty confident I can think up  
> a number of cases where it wouldn’t work.

Operating systems that aren’t Linux being the main case here.

> Windows isn’t even a multi-user OS! It also  
> doesn’t support hard or symbolic links, or set-user-id flags on  
> executables, or allow you to delete a running executable, you can in  
> unix.

Windows 2000 and XP support both multi-user operation and soft/hard  
links with NTFS.

> **···**
>
> –  
> Ceri Storey [cez@necrofish.org.uk](mailto:cez@necrofish.org.uk)

---

<div class="post-metadata">

**Author:** ![David\_Garamond2](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@David\_Garamond2](https://rubytalk.org/u/David_Garamond2)\
**Post date:** [21 March 2004 11:48 UTC](https://rubytalk.org/t/can-load-path-be-relative/10664/20 "2004-03-21T11:48:03Z")

</div>

1-2 weeks ago I asked the same question about a Linux binary Ruby  
installation that can be installed anywhere in users’ home directory  
(without the user building Ruby himself). To accomplish this, the  
following is needed:

1. build Ruby with --enable-load-relative (you must patch Ruby with ±  
100-line patch from Nobu; sorry I couldn’t search [ruby-talk.org](http://ruby-talk.org) at the  
moment but it’s around the first week of March 2004) which will make  
$LOAD\_PATH relative;

2. either:

a) make a small wrapper for ruby executable, which makes Ruby find its  
shared libraries in “…/lib” instead of only in /lib, /usr/lib, and  
other paths specified in /etc/ld.so.conf; (I’m attaching the small  
wrapper I wrote, which again was kindly revised by Nobu);

b) build a static version of Ruby (–disable-shared in configure  
option), which will then depend only on basic shared libraries (like glibc).

After that, you can just bundle the resulting Ruby installation.

Hope that helps,

[wrapper.c](https://rubytalk.org/uploads/short-url/6RSbgbcqHcYRY3XVoHvVXoTb2qX.c) (2.05 KB)

> **···**
>
> –  
> dave

[Next page](https://rubytalk.org/t/can-load-path-be-relative/10664.md?page=2)
