You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11 WSL Ubuntu22.04下cpanm路径优先级异常问题求助

WSL Ubuntu中PATH顺序正常却调用Windows下cpanm的问题

问题场景

在Windows11的WSL Ubuntu22.04环境中安装Perl::LanguageServer模块,执行了以下操作:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install cpanminus
cpanm Perl::LanguageServer

但出现报错:

-bash: /mnt/c/Strawberry/perl/bin/cpanm: perl: bad interpreter: No such file or directory

查看PATH顺序,/usr/bin确实位于/mnt/c/Strawberry/perl/bin之前:

echo $PATH | perl -nE 'say for split ":"'
# 输出片段:
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
...
/mnt/c/Strawberry/perl/bin
...

同时确认两个路径下的cpanm状态:

/usr/bin/cpanm --version
# 输出:cpanm (App::cpanminus) version 1.7045 (/usr/bin/cpanm)

/mnt/c/Strawberry/perl/bin/cpanm --version
# 输出:-bash: /mnt/c/Strawberry/perl/bin/cpanm: perl: bad interpreter: No such file or directory

原因分析

bash为了提升命令查找效率,会将已经执行过的命令路径缓存到**哈希表(hash table)**中。如果在调整PATH顺序之前,你曾经执行过cpanm命令,当时bash找到的是Windows下Strawberry Perl的cpanm路径,并将其存入缓存。后续即使PATH顺序修改,bash依然会优先使用缓存中的路径,而不会重新遍历PATH。

解决办法

  1. 查看缓存的cpanm路径:
hash cpanm
  1. 清除单个命令的缓存:
hash -d cpanm
  1. 或者清除所有命令缓存:
hash -r

执行上述操作后,再运行cpanm Perl::LanguageServer,bash就会重新按照PATH顺序查找,使用/usr/bin下的cpanm。

另外,也可以直接指定绝对路径执行,彻底绕过缓存问题:

/usr/bin/cpanm Perl::LanguageServer

内容的提问来源于stack exchange,提问作者Håkon Hægland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 03:42:42