Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Can't locate module in @INC when executing script over SSH

by Leudwinus (Scribe)
on Jan 27, 2023 at 18:55 UTC ( [id://11149950]=perlquestion: print w/replies, xml ) Need Help??

Leudwinus has asked for the wisdom of the Perl Monks concerning the following question:

I have the following script (json_test) on a remote FreeBSD virtual machine which runs fine when executed directly on the VM:

#!/usr/bin/env perl use lib "/home/user/perl5/lib"; use JSON; # line 4 print JSON->new->pretty->encode( { qw( a b ) } );

However, I get the following error message when I try to run it via SSH from my local machine:

leudwinus@localmachine:~$ ssh user@remotemachine './json_test' Can't locate JSON.pm in @INC (you may need to install the JSON module) + (@INC contains: /home/user/perl5/lib /usr/local/lib/perl5/site_perl/ +mach/5.32 /usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.32/mach /usr/loc +al/lib/perl5/5.32) at ./json_test line 4. BEGIN failed--compilation aborted at ./json_test line 4.

Any suggestions on how I can execute this script remotely via SSH?

On the virtual machine, I get the following information:

user@remotemachine:~$ perldoc -l JSON /home/user/perl5/lib/perl5/JSON.pm user@remotemachine:~$ perl -V Summary of my perl5 (revision 5 version 32 subversion 1) configuration +: ... @INC: /home/user/perl5/lib/perl5/5.32.1/amd64-freebsd-thread-multi /home/user/perl5/lib/perl5/5.32.1 /home/user/perl5/lib/perl5/amd64-freebsd-thread-multi /home/user/perl5/lib/perl5 /home/user/perl5/lib/perl5/5.32.1/amd64-freebsd-thread-multi /home/user/perl5/lib/perl5/5.32.1 /home/user/perl5/lib/perl5/amd64-freebsd-thread-multi /home/user/perl5/lib/perl5 /usr/local/lib/perl5/site_perl/mach/5.32 /usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.32/mach /usr/local/lib/perl5/5.32

JSON is installed on my local Ubuntu machine at /home/leudwinus/perl5/lib/perl5/JSON.pm but I didn't think that would matter.

Thanks in advance!

Replies are listed 'Best First'.
Re: Can't locate module in @INC when executing script over SSH
by haukex (Archbishop) on Jan 27, 2023 at 19:13 UTC

    Note how the two @INCs are different. When you ssh into the remote machine, the script is getting executed directly; there's no login shell to execute your ~/.profile and therefore it doesn't set up your local::lib environment variables like PERL5LIB, so that's why those directories aren't showing up in your @INC. Try putting a use local::lib; At the top of your script. And by the way, always Use strict and warnings!

      Thank you for taking the time to reply to help me out! Adding use local::lib did not work unfortunately. Even though I have local::lib installed on the remote machine, I now get this error:

      Can't locate local/lib.pm in @INC (you may need to install the local:: +lib module) (@INC contains: /usr/local/lib/perl5/site_perl/mach/5.32 +/usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.32/mach /usr/lo +cal/lib/perl5/5.32) at ./json_test line 4. BEGIN failed--compilation aborted at ./json_test line 4.

      but what did end up solving it was to replace this line:

      use lib "/home/user/perl5/lib";

      with this line:

      use lib "/home/user/perl5/lib/perl5";

      I do include both the use strict; and use warnings; pragmas in all of my programs but omitted them here for clarity and brevity. Perhaps I'll include them in the future to propagate better coding hygiene!

      Edited to add: This solution still feels "off" as it isn't very portable. So any other suggestions would still be appreciated.

        Even though I have local::lib installed on the remote machine, I now get this error: Can't locate local/lib.pm in @INC

        Do you know where you installed it? If you have root access to the remote machine, you could install local::lib to the system Perl (e.g. on Debian-based systems: sudo apt-get install liblocal-lib-perl), or you could ask the admin to do so.

        So any other suggestions would still be appreciated.

        Sure, there are lots of other ways. For example, you could build your own Perl in your home directory using perlbrew, that'll then be a self-contained install with all modules in its installation directory, so that when you run your script on that perl, there'll be no confusion about where the modules are because they'll all be in the @INC compiled into Perl. I list a few more ways of how to set @INC in this node, but which one is best in your case depends on several factors, like whether you have root on the remote machine, whether you can build your own Perl, whether this script is going to need to be portable across many machines, or just one or two, and so on - if you provide more details on your situation we can probably make better recommendations.

        Edited to add

        AFAICT that's not the only edit you made, at first your node didn't contain the error message regarding local::lib. Please mark all significant edits, see How do I change/delete my post?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11149950]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-19 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found