Install Perl Module on Linux Without Root Permission
In this article
Yes, you can install Perl modules without root permission by installing them into your home directory using cpan or cpanm (cpanminus). This is useful on shared servers or systems where you don't have sudo access. The key is to configure a local library path (like ~/perl5) and set the environment variables so Perl finds those modules. You'll need a basic terminal and Perl already installed (check with perl -v).
What You'll Need
- A Linux system with Perl installed (any recent version works).
- Basic command-line access (terminal).
- If you plan to use
cpanm, you may need to install it first—but you can do that without root too (see Step 3). - Internet access to fetch modules from CPAN.
Step-by-Step
- Set up a local Perl library directory.
Create a directory where your modules will live, for examplemkdir -p ~/perl5. This will be your personal module path. - Configure environment variables.
Add these lines to your shell profile (~/.bashrcfor bash,~/.zshrcfor zsh):export PERL5LIB=~/perl5/lib/perl5export PERL_LOCAL_LIB_ROOT=~/perl5export PERL_MB_OPT="--install_base ~/perl5"export PERL_MM_OPT="INSTALL_BASE=~/perl5"
Then runsource ~/.bashrc(or the appropriate file) to apply changes. - Install cpanminus (cpanm) if you don't have it.
cpanm is a lighter, easier tool than the defaultcpan. To install it without root, use the built-in cpan to bootstrap it:cpan App::cpanminus
This will install cpanm into your local directory (since you set the install base). If cpan asks for configuration, choose "yes" to auto-configure, and when it asks for a sudo password, just press Enter (it will use your local path). - Install your desired module with cpanm.
Once cpanm is available, run:cpanm Module::Name
For example, to installLWP::Simple, runcpanm LWP::Simple. cpanm will automatically install into~/perl5because of the environment variables. - Verify the installation.
To check that the module is found, run a quick Perl one-liner:perl -MModule::Name -e 'print "OK\n"'
If it prints OK, the module is installed and accessible. - If you prefer the default cpan client:
Runcpanand when prompted for configuration, choose "yes" to auto-configure. Then at the cpan prompt, typeinstall Module::Name. Because of the environment variables, it will install locally. Exit withquit.
Common Mistakes
- Forgetting to set the environment variables before running cpan/cpanm. If you don't set
PERL5LIBand the install options, the module may install to the system directory and fail with a permission error. - Not sourcing the profile after editing it. The changes won't take effect in the current terminal until you run
source ~/.bashrc. - Using
sudoout of habit. If you have sudo, you might accidentally install system-wide, but if you don't, sudo will fail. Stick to the local path. - Assuming the module is installed system-wide. Some modules have dependencies that also need to be installed locally—cpanm handles this automatically, but if you use cpan manually, you may need to install dependencies one by one.
- Not checking if the module is already available in your distro's package manager. Sometimes you can install a module via
aptoryumwithout root, but that's rare; the local CPAN method is more universal.
Tips
- Use
cpanmover the defaultcpan—it's faster, resolves dependencies automatically, and is less interactive. - If you have multiple projects, consider using
local::libto manage separate module directories per project. The environment variables we set are essentially what local::lib does. - When you log into a new shell, remember to source your profile or the environment variables won't be set. You can also add the export lines to your
~/.profileto cover login shells. - If a module fails to compile, it might require system libraries (like
libssl-dev). You cannot install those without root, but sometimes you can install the library into your home directory and setLD_LIBRARY_PATH—this is advanced and not always worth it. - Check the module's documentation for any special prerequisites. Some modules need a C compiler (gcc) and make—these are usually already installed on Linux, but if not, you may need to ask the system administrator.
Troubleshooting
"Can't locate Module.pm in @INC" — This means Perl isn't finding your local modules. Check that PERL5LIB is set correctly: run echo $PERL5LIB and ensure it points to ~/perl5/lib/perl5. Also verify the module actually installed there (look in ~/perl5/lib/perl5 for the module file).
cpanm command not found — If you installed cpanm but it's not in your PATH, it might be in ~/perl5/bin. Add that to your PATH: export PATH=~/perl5/bin:$PATH and add it to your profile.
Permission errors during install — If you see "Permission denied" when cpan tries to write to system directories, it means the environment variables weren't set before running cpan. Re-check your exports and source your profile.
Module fails to compile — This is often due to missing system dependencies. Read the error output carefully—it will usually tell you which header file or library is missing. You may need to ask the system administrator to install the package, or try a pure-Perl alternative module.
cpan asks for a password — If cpan prompts for a sudo password, it's trying to install system-wide. Cancel (Ctrl+C) and ensure PERL_MM_OPT and PERL_MB_OPT are set before starting cpan.
Environment variables lost after logout — Make sure you added the export lines to your ~/.bashrc (or equivalent) and not just typed them in the terminal. Also, if your shell is not bash, adjust the file accordingly.
Frequently Asked Questions
Do I need root to install any Perl module?
No. As long as you can write to your home directory, you can install any pure-Perl module. Modules that require compiling C code may need a compiler, but that's usually already present.
Will my modules be available to all users?
No, they will only be available to your user account. Other users won't see them unless they also set up the same environment variables.
Can I install a specific version of a module?
Yes, with cpanm you can use cpanm Module::Name@1.23 to install a specific version. This is useful for compatibility.
What if the module has dependencies that also need installing?
cpanm automatically installs all dependencies into your local directory. If you use the default cpan client, you may need to install them manually, but cpan can also resolve dependencies if you answer "yes" to the auto-configuration.
Is there a way to avoid setting environment variables every time?
Yes, after you set them in your shell profile, they will be set automatically for every new terminal session. Just make sure to source the profile once after editing.
Can I use this method on a shared hosting server?
Yes, this is exactly the intended use case. Many shared hosting environments don't give root access, but you can install Perl modules in your home directory as described.