1local::lib(3) User Contributed Perl Documentation local::lib(3)
2
3
4
6 local::lib - create and use a local lib/ for perl modules with PERL5LIB
7
9 In code -
10
11 use local::lib; # sets up a local lib at ~/perl5
12
13 use local::lib '~/foo'; # same, but ~/foo
14
15 # Or...
16 use FindBin;
17 use local::lib "$FindBin::Bin/../support"; # app-local support library
18
19 From the shell -
20
21 # Install LWP and its missing dependencies to the '~/perl5' directory
22 perl -MCPAN -Mlocal::lib -e 'CPAN::install(LWP)'
23
24 # Just print out useful shell commands
25 $ perl -Mlocal::lib
26 PERL_MB_OPT='--install_base /home/username/perl5'; export PERL_MB_OPT;
27 PERL_MM_OPT='INSTALL_BASE=/home/username/perl5'; export PERL_MM_OPT;
28 PERL5LIB="/home/username/perl5/lib/perl5"; export PERL5LIB;
29 PATH="/home/username/perl5/bin:$PATH"; export PATH;
30 PERL_LOCAL_LIB_ROOT="/home/usename/perl5:$PERL_LOCAL_LIB_ROOT"; export PERL_LOCAL_LIB_ROOT;
31
32 From a .bash_profile or .bashrc file -
33
34 eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"
35
36 The bootstrapping technique
37 A typical way to install local::lib is using what is known as the
38 "bootstrapping" technique. You would do this if your system
39 administrator hasn't already installed local::lib. In this case,
40 you'll need to install local::lib in your home directory.
41
42 Even if you do have administrative privileges, you will still want to
43 set up your environment variables, as discussed in step 4. Without
44 this, you would still install the modules into the system CPAN
45 installation and also your Perl scripts will not use the lib/ path you
46 bootstrapped with local::lib.
47
48 By default local::lib installs itself and the CPAN modules into
49 ~/perl5.
50
51 Windows users must also see "Differences when using this module under
52 Win32".
53
54 1. Download and unpack the local::lib tarball from CPAN (search for
55 "Download" on the CPAN page about local::lib). Do this as an
56 ordinary user, not as root or administrator. Unpack the file in
57 your home directory or in any other convenient location.
58
59 2. Run this:
60
61 perl Makefile.PL --bootstrap
62
63 If the system asks you whether it should automatically configure as
64 much as possible, you would typically answer yes.
65
66 3. Run this: (local::lib assumes you have make installed on your
67 system)
68
69 make test && make install
70
71 4. Now we need to setup the appropriate environment variables, so that
72 Perl starts using our newly generated lib/ directory. If you are
73 using bash or any other Bourne shells, you can add this to your
74 shell startup script this way:
75
76 echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"' >>~/.bashrc
77
78 If you are using C shell, you can do this as follows:
79
80 % echo $SHELL
81 /bin/csh
82 $ echo 'eval `perl -I$HOME/perl5/lib/perl5 -Mlocal::lib`' >> ~/.cshrc
83
84 After writing your shell configuration file, be sure to re-read it
85 to get the changed settings into your current shell's environment.
86 Bourne shells use ". ~/.bashrc" for this, whereas C shells use
87 "source ~/.cshrc".
88
89 Bootstrapping into an alternate directory
90
91 In order to install local::lib into a directory other than the default,
92 you need to specify the name of the directory when you call bootstrap.
93 Then, when setting up the environment variables, both perl and
94 local::lib must be told the location of the bootstrap directory. The
95 setup process would look as follows:
96
97 perl Makefile.PL --bootstrap=~/foo
98 make test && make install
99 echo 'eval "$(perl -I$HOME/foo/lib/perl5 -Mlocal::lib=$HOME/foo)"' >>~/.bashrc
100 . ~/.bashrc
101
102 Other bootstrapping options
103
104 If you're on a slower machine, or are operating under draconian disk
105 space limitations, you can disable the automatic generation of manpages
106 from POD when installing modules by using the "--no-manpages" argument
107 when bootstrapping:
108
109 perl Makefile.PL --bootstrap --no-manpages
110
111 To avoid doing several bootstrap for several Perl module environments
112 on the same account, for example if you use it for several different
113 deployed applications independently, you can use one bootstrapped
114 local::lib installation to install modules in different directories
115 directly this way:
116
117 cd ~/mydir1
118 perl -Mlocal::lib=./
119 eval $(perl -Mlocal::lib=./) ### To set the environment for this shell alone
120 printenv ### You will see that ~/mydir1 is in the PERL5LIB
121 perl -MCPAN -e install ... ### whatever modules you want
122 cd ../mydir2
123 ... REPEAT ...
124
125 If you use .bashrc to activate a local::lib automatically, the
126 local::lib will be re-enabled in any sub-shells used, overriding
127 adjustments you may have made in the parent shell. To avoid this, you
128 can initialize the local::lib in .bash_profile rather than .bashrc, or
129 protect the local::lib invocation with a $SHLVL check:
130
131 [ $SHLVL -eq 1 ] && eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"
132
133 If you are working with several "local::lib" environments, you may want
134 to remove some of them from the current environment without disturbing
135 the others. You can deactivate one environment like this (using bourne
136 sh):
137
138 eval $(perl -Mlocal::lib=--deactivate,~/path)
139
140 which will generate and run the commands needed to remove "~/path" from
141 your various search paths. Whichever environment was activated most
142 recently will remain the target for module installations. That is, if
143 you activate "~/path_A" and then you activate "~/path_B", new modules
144 you install will go in "~/path_B". If you deactivate "~/path_B" then
145 modules will be installed into "~/pathA" -- but if you deactivate
146 "~/path_A" then they will still be installed in "~/pathB" because pathB
147 was activated later.
148
149 You can also ask "local::lib" to clean itself completely out of the
150 current shell's environment with the "--deactivate-all" option. For
151 multiple environments for multiple apps you may need to include a
152 modified version of the "use FindBin" instructions in the "In code"
153 sample above. If you did something like the above, you have a set of
154 Perl modules at "~/mydir1/lib". If you have a script at
155 "~/mydir1/scripts/myscript.pl", you need to tell it where to find the
156 modules you installed for it at "~/mydir1/lib".
157
158 In "~/mydir1/scripts/myscript.pl":
159
160 use strict;
161 use warnings;
162 use local::lib "$FindBin::Bin/.."; ### points to ~/mydir1 and local::lib finds lib
163 use lib "$FindBin::Bin/../lib"; ### points to ~/mydir1/lib
164
165 Put this before any BEGIN { ... } blocks that require the modules you
166 installed.
167
168 Differences when using this module under Win32
169 To set up the proper environment variables for your current session of
170 "CMD.exe", you can use this:
171
172 C:\>perl -Mlocal::lib
173 set PERL_MB_OPT=--install_base C:\DOCUME~1\ADMINI~1\perl5
174 set PERL_MM_OPT=INSTALL_BASE=C:\DOCUME~1\ADMINI~1\perl5
175 set PERL5LIB=C:\DOCUME~1\ADMINI~1\perl5\lib\perl5
176 set PATH=C:\DOCUME~1\ADMINI~1\perl5\bin;%PATH%
177
178 ### To set the environment for this shell alone
179 C:\>perl -Mlocal::lib > %TEMP%\tmp.bat && %TEMP%\tmp.bat && del %TEMP%\tmp.bat
180 ### instead of $(perl -Mlocal::lib=./)
181
182 If you want the environment entries to persist, you'll need to add them
183 to the Control Panel's System applet yourself or use
184 App::local::lib::Win32Helper.
185
186 The "~" is translated to the user's profile directory (the directory
187 named for the user under "Documents and Settings" (Windows XP or
188 earlier) or "Users" (Windows Vista or later)) unless $ENV{HOME} exists.
189 After that, the home directory is translated to a short name (which
190 means the directory must exist) and the subdirectories are created.
191
192 PowerShell
193
194 local::lib also supports PowerShell, and can be used with the
195 "Invoke-Expression" cmdlet.
196
197 Invoke-Expression "$(perl -Mlocal::lib)"
198
200 The version of a Perl package on your machine is not always the version
201 you need. Obviously, the best thing to do would be to update to the
202 version you need. However, you might be in a situation where you're
203 prevented from doing this. Perhaps you don't have system administrator
204 privileges; or perhaps you are using a package management system such
205 as Debian, and nobody has yet gotten around to packaging up the version
206 you need.
207
208 local::lib solves this problem by allowing you to create your own
209 directory of Perl packages downloaded from CPAN (in a multi-user
210 system, this would typically be within your own home directory). The
211 existing system Perl installation is not affected; you simply invoke
212 Perl with special options so that Perl uses the packages in your own
213 local package directory rather than the system packages. local::lib
214 arranges things so that your locally installed version of the Perl
215 packages takes precedence over the system installation.
216
217 If you are using a package management system (such as Debian), you
218 don't need to worry about Debian and CPAN stepping on each other's
219 toes. Your local version of the packages will be written to an
220 entirely separate directory from those installed by Debian.
221
223 This module provides a quick, convenient way of bootstrapping a user-
224 local Perl module library located within the user's home directory. It
225 also constructs and prints out for the user the list of environment
226 variables using the syntax appropriate for the user's current shell (as
227 specified by the "SHELL" environment variable), suitable for directly
228 adding to one's shell configuration file.
229
230 More generally, local::lib allows for the bootstrapping and usage of a
231 directory containing Perl modules outside of Perl's @INC. This makes it
232 easier to ship an application with an app-specific copy of a Perl
233 module, or collection of modules. Useful in cases like when an upstream
234 maintainer hasn't applied a patch to a module of theirs that you need
235 for your application.
236
237 On import, local::lib sets the following environment variables to
238 appropriate values:
239
240 PERL_MB_OPT
241 PERL_MM_OPT
242 PERL5LIB
243 PATH
244 PERL_LOCAL_LIB_ROOT
245
246 When possible, these will be appended to instead of overwritten
247 entirely.
248
249 These values are then available for reference by any code after import.
250
252 See lib::core::only for one way to do this - but note that there are a
253 number of caveats, and the best approach is always to perform a build
254 against a clean perl (i.e. site and vendor as close to empty as
255 possible).
256
258 Options are values that can be passed to the "local::lib" import
259 besides the directory to use. They are specified as "use local::lib
260 '--option'[, path];" or "perl -Mlocal::lib=--option[,path]".
261
262 --deactivate
263 Remove the chosen path (or the default path) from the module search
264 paths if it was added by "local::lib", instead of adding it.
265
266 --deactivate-all
267 Remove all directories that were added to search paths by "local::lib"
268 from the search paths.
269
270 --quiet
271 Don't output any messages about directories being created.
272
273 --always
274 Always add directories to environment variables, ignoring if they are
275 already included.
276
277 --shelltype
278 Specify the shell type to use for output. By default, the shell will
279 be detected based on the environment. Should be one of: "bourne",
280 "csh", "cmd", or "powershell".
281
282 --no-create
283 Prevents "local::lib" from creating directories when activating dirs.
284 This is likely to cause issues on Win32 systems.
285
287 ensure_dir_structure_for
288 Arguments: $path
289 Return value: None
290
291 Attempts to create a local::lib directory, including subdirectories and
292 all required parent directories. Throws an exception on failure.
293
294 print_environment_vars_for
295 Arguments: $path
296 Return value: None
297
298 Prints to standard output the variables listed above, properly set to
299 use the given path as the base directory.
300
301 build_environment_vars_for
302 Arguments: $path
303 Return value: %environment_vars
304
305 Returns a hash with the variables listed above, properly set to use the
306 given path as the base directory.
307
308 setup_env_hash_for
309 Arguments: $path
310 Return value: None
311
312 Constructs the %ENV keys for the given path, by calling
313 "build_environment_vars_for".
314
315 active_paths
316 Arguments: None
317 Return value: @paths
318
319 Returns a list of active "local::lib" paths, according to the
320 "PERL_LOCAL_LIB_ROOT" environment variable and verified against what is
321 really in @INC.
322
323 install_base_perl_path
324 Arguments: $path
325 Return value: $install_base_perl_path
326
327 Returns a path describing where to install the Perl modules for this
328 local library installation. Appends the directories "lib" and "perl5"
329 to the given path.
330
331 lib_paths_for
332 Arguments: $path
333 Return value: @lib_paths
334
335 Returns the list of paths perl will search for libraries, given a base
336 path. This includes the base path itself, the architecture specific
337 subdirectory, and perl version specific subdirectories. These paths
338 may not all exist.
339
340 install_base_bin_path
341 Arguments: $path
342 Return value: $install_base_bin_path
343
344 Returns a path describing where to install the executable programs for
345 this local library installation. Appends the directory "bin" to the
346 given path.
347
348 installer_options_for
349 Arguments: $path
350 Return value: %installer_env_vars
351
352 Returns a hash of environment variables that should be set to cause
353 installation into the given path.
354
355 resolve_empty_path
356 Arguments: $path
357 Return value: $base_path
358
359 Builds and returns the base path into which to set up the local module
360 installation. Defaults to "~/perl5".
361
362 resolve_home_path
363 Arguments: $path
364 Return value: $home_path
365
366 Attempts to find the user's home directory. If no definite answer is
367 available, throws an exception.
368
369 resolve_relative_path
370 Arguments: $path
371 Return value: $absolute_path
372
373 Translates the given path into an absolute path.
374
375 resolve_path
376 Arguments: $path
377 Return value: $absolute_path
378
379 Calls the following in a pipeline, passing the result from the previous
380 to the next, in an attempt to find where to configure the environment
381 for a local library installation: "resolve_empty_path",
382 "resolve_home_path", "resolve_relative_path". Passes the given path
383 argument to "resolve_empty_path" which then returns a result that is
384 passed to "resolve_home_path", which then has its result passed to
385 "resolve_relative_path". The result of this final call is returned from
386 "resolve_path".
387
389 new
390 Arguments: %attributes
391 Return value: $local_lib
392
393 Constructs a new "local::lib" object, representing the current state of
394 @INC and the relevant environment variables.
395
397 roots
398 An arrayref representing active "local::lib" directories.
399
400 inc
401 An arrayref representing @INC.
402
403 libs
404 An arrayref representing the PERL5LIB environment variable.
405
406 bins
407 An arrayref representing the PATH environment variable.
408
409 extra
410 A hashref of extra environment variables (e.g. "PERL_MM_OPT" and
411 "PERL_MB_OPT")
412
413 no_create
414 If set, "local::lib" will not try to create directories when activating
415 them.
416
418 clone
419 Arguments: %attributes
420 Return value: $local_lib
421
422 Constructs a new "local::lib" object based on the existing one,
423 overriding the specified attributes.
424
425 activate
426 Arguments: $path
427 Return value: $new_local_lib
428
429 Constructs a new instance with the specified path active.
430
431 deactivate
432 Arguments: $path
433 Return value: $new_local_lib
434
435 Constructs a new instance with the specified path deactivated.
436
437 deactivate_all
438 Arguments: None
439 Return value: $new_local_lib
440
441 Constructs a new instance with all "local::lib" directories
442 deactivated.
443
444 environment_vars_string
445 Arguments: [ $shelltype ]
446 Return value: $shell_env_string
447
448 Returns a string to set up the "local::lib", meant to be run by a
449 shell.
450
451 build_environment_vars
452 Arguments: None
453 Return value: %environment_vars
454
455 Returns a hash with the variables listed above, properly set to use the
456 given path as the base directory.
457
458 setup_env_hash
459 Arguments: None
460 Return value: None
461
462 Constructs the %ENV keys for the given path, by calling
463 "build_environment_vars".
464
465 setup_local_lib
466 Constructs the %ENV hash using "setup_env_hash", and set up @INC.
467
469 Be careful about using local::lib in combination with "make install
470 UNINST=1". The idea of this feature is that will uninstall an old
471 version of a module before installing a new one. However it lacks a
472 safety check that the old version and the new version will go in the
473 same directory. Used in combination with local::lib, you can
474 potentially delete a globally accessible version of a module while
475 installing the new version in a local place. Only combine "make install
476 UNINST=1" and local::lib if you understand these possible consequences.
477
479 • Directory names with spaces in them are not well supported by the
480 perl toolchain and the programs it uses. Pure-perl distributions
481 should support spaces, but problems are more likely with dists that
482 require compilation. A workaround you can do is moving your
483 local::lib to a directory with spaces after you installed all
484 modules inside your local::lib bootstrap. But be aware that you
485 can't update or install CPAN modules after the move.
486
487 • Rather basic shell detection. Right now anything with csh in its
488 name is assumed to be a C shell or something compatible, and
489 everything else is assumed to be Bourne, except on Win32 systems.
490 If the "SHELL" environment variable is not set, a Bourne-compatible
491 shell is assumed.
492
493 • Kills any existing PERL_MM_OPT or PERL_MB_OPT.
494
495 • Should probably auto-fixup CPAN config if not already done.
496
497 • On VMS and MacOS Classic (pre-OS X), local::lib loads File::Spec.
498 This means any File::Spec version installed in the local::lib will
499 be ignored by scripts using local::lib. A workaround for this is
500 using "use lib "$local_lib/lib/perl5";" instead of using
501 "local::lib" directly.
502
503 • Conflicts with ExtUtils::MakeMaker's "PREFIX" option. "local::lib"
504 uses the "INSTALL_BASE" option, as it has more predictable and sane
505 behavior. If something attempts to use the "PREFIX" option when
506 running a Makefile.PL, ExtUtils::MakeMaker will refuse to run, as
507 the two options conflict. This can be worked around by temporarily
508 unsetting the "PERL_MM_OPT" environment variable.
509
510 • Conflicts with Module::Build's "--prefix" option. Similar to the
511 previous limitation, but any "--prefix" option specified will be
512 ignored. This can be worked around by temporarily unsetting the
513 "PERL_MB_OPT" environment variable.
514
515 Patches very much welcome for any of the above.
516
517 • On Win32 systems, does not have a way to write the created
518 environment variables to the registry, so that they can persist
519 through a reboot.
520
522 If you've configured local::lib to install CPAN modules somewhere in to
523 your home directory, and at some point later you try to install a
524 module with "cpan -i Foo::Bar", but it fails with an error like:
525 "Warning: You do not have permissions to install into
526 /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux at
527 /usr/lib64/perl5/5.8.8/Foo/Bar.pm" and buried within the install log is
528 an error saying "'INSTALL_BASE' is not a known MakeMaker parameter
529 name", then you've somehow lost your updated ExtUtils::MakeMaker
530 module.
531
532 To remedy this situation, rerun the bootstrapping procedure documented
533 above.
534
535 Then, run "rm -r ~/.cpan/build/Foo-Bar*"
536
537 Finally, re-run "cpan -i Foo::Bar" and it should install without
538 problems.
539
541 SHELL
542 COMSPEC
543 local::lib looks at the user's "SHELL" environment variable when
544 printing out commands to add to the shell configuration file.
545
546 On Win32 systems, "COMSPEC" is also examined.
547
549 • Perl Advent article, 2011
550 <http://perladvent.org/2011/2011-12-01.html>
551
553 IRC:
554
555 Join #toolchain on irc.perl.org.
556
558 Matt S Trout <mst@shadowcat.co.uk> http://www.shadowcat.co.uk/
559
560 auto_install fixes kindly sponsored by http://www.takkle.com/
561
563 Patches to correctly output commands for csh style shells, as well as
564 some documentation additions, contributed by Christopher Nehren
565 <apeiron@cpan.org>.
566
567 Doc patches for a custom local::lib directory, more cleanups in the
568 english documentation and a german documentation contributed by Torsten
569 Raudssus <torsten@raudssus.de>.
570
571 Hans Dieter Pearcey <hdp@cpan.org> sent in some additional tests for
572 ensuring things will install properly, submitted a fix for the bug
573 causing problems with writing Makefiles during bootstrapping,
574 contributed an example program, and submitted yet another fix to ensure
575 that local::lib can install and bootstrap properly. Many, many thanks!
576
577 pattern of Freenode IRC contributed the beginnings of the
578 Troubleshooting section. Many thanks!
579
580 Patch to add Win32 support contributed by Curtis Jewell
581 <csjewell@cpan.org>.
582
583 Warnings for missing PATH/PERL5LIB (as when not running interactively)
584 silenced by a patch from Marco Emilio Poleggi.
585
586 Mark Stosberg <mark@summersault.com> provided the code for the now
587 deleted '--self-contained' option.
588
589 Documentation patches to make win32 usage clearer by David Mertens
590 <dcmertens.perl@gmail.com> (run4flat).
591
592 Brazilian portuguese translation and minor doc patches contributed by
593 Breno G. de Oliveira <garu@cpan.org>.
594
595 Improvements to stacking multiple local::lib dirs and removing them
596 from the environment later on contributed by Andrew Rodland
597 <arodland@cpan.org>.
598
599 Patch for Carp version mismatch contributed by Hakim Cassimally
600 <osfameron@cpan.org>.
601
602 Rewrite of internals and numerous bug fixes and added features
603 contributed by Graham Knop <haarg@haarg.org>.
604
606 Copyright (c) 2007 - 2013 the local::lib "AUTHOR" and "CONTRIBUTORS" as
607 listed above.
608
610 This is free software; you can redistribute it and/or modify it under
611 the same terms as the Perl 5 programming language system itself.
612
613
614
615perl v5.38.0 2023-07-21 local::lib(3)