26

Yesterday I wanted to test some software and in the documentation it said, to install I just needed to type

cpan -i Software

I never used cpan, I just know that it is the perl package manager. (Is it..?) However, it turned out that I needed loads of dependencies, and stupid as I am, I just installed all of them. (First, I had to set up cpan which asked me lots of questions) Long story short, I just want to remove all of it again. I googled a bit, and it seems like cpan does not have an uninstall routine, especially for all the packages at once. Can I just remove some directory or will I run into troubles?

6 Answers 6

31

the cpan command isn't really a package manager like apt-get is. It is more a tool that downloads and installs from CPAN (the site, or one of its mirrors). After it has finished this task it doesn't remember much about what was done before, at least not enough to remove previously installed modules, at least not reliably, cleanly or dependency-safely. (Update: After looking at App::pmuninstall, it can be used to handle dependencies, but it does so by connecting to outside (read: web) sources, which compute these separately, which is fine, but I stand by the previous statement that CPAN.pm doesn't do this.)

I used to worry about removing modules, but now I realize that most Perl modules take up so little room that I just don't worry about having a few extra modules installed that you will never use. So unless you are on a computer with a REALLY small disc, I would just let it be.

On Windows or if you are using a non-system Perl on Linux/Mac you could just remove Perl and reinstall it. I would not recommend this if you are using the system installed Perl on Linux/Mac however as you could break your OS doing this (you might be ok if you were careful, but not worth it to save a few Mb!).

In the future, you can easily install a local version of Perl using perlbrew, there are tutorials all over the web if the docs aren't sufficient (they should be). This also has the bonus of letting you play with the newest and greatest Perl versions, which your system likely doesn't come with yet. Then if you install a mountain of junk, or even break it doing something crazy, remove that version and reinstall/install a different version.

Another nice tool is cpanminus (or cpanm for short) which is a newer, more user friendly cpan tool. All the cool kids are using it.

6
  • 1
    Thank you for the nice explanation. I digged a bit deeper into the lib directories and decided to keep all those files as you suggested.
    – janoliver
    Oct 15, 2011 at 14:22
  • @xenoterracide does that depend on having used cpanm to install? a cursory glance at the doc seemed to suggest that. either way, I don't see the need to uninstall almost any of the modules on CPAN, at least from a data storage perspective. Still its nice to know that something exists Oct 15, 2011 at 19:02
  • nope, cpanm afaik doesn't do anything as far as writing stuff out that cpan and cpanp do. Oct 15, 2011 at 21:13
  • cool +1! I notice that it does go to the web for dependencies, but given the limitations that CPAN.pm gives it, I'm glad they figured something out rather than ignoring them! Oct 15, 2011 at 23:03
  • I just was looking at this and thinking, one reason to uninstall modules is when you're developing one, you remove one of it's modules in the dev release, but it's still installed, and thus is in your lib. Removing it is the better idea. Sep 27, 2012 at 18:30
19

You can uninstall individual modules with cpanplus (ships with Perl) like this:

cpanp uninstall SQL::Abstract

You can view all modules installed with the cpan script like this:

perldoc perllocal

Putting the two together:

for module in $(perldoc -u perllocal | grep -F 'C<Module> L<' | sed 's/^.*L<\(.*\)|.*>$/\1/') ; do
    cpanp uninstall "$module"
done
2
  • 1
    I guess line 2 of the script should be cpanm --uninstall "$module" Apr 12, 2014 at 22:09
  • or cpan uninstall "$module"
    – Pit
    Dec 22, 2022 at 14:05
2

I'm not sure about removing "all of it". But to remove a single module you can use App::pmuninstall with it's sole script pm-uninstall to uninstall modules. You might then be able to write some kind of script to recursively remove the deps.

1

If you can't use cpan any more because there are incompatible modules in you path, you can remove all installed modules by hand. For example, I upgraded from Fedora 22 to Fedora 23 and the Perl version changed. All modules installed previously via cpanm into /usr/local/lib64/perl5 did not work any more and prevented me from using cpanm.

$ cpanm --uninstall Apache::DBI
Attempt to reload Scalar/Util.pm aborted.
Compilation failed in require at /usr/share/perl5/vendor_perl/File/Temp.pm line 18.
...

I could solve this by moving that directory:

$ mv /usr/local/lib64/perl5 /root/usr-local-lib64-perl5

The name of that directory may vary on your system.

Carefull: If a module installed files outside of that directory, for example system library files, these files will remain there.

1

I will change Flimm's answer to use cpanm and optionally uninstall cpanm itself in the end of the script:

#!/usr/bin/env bash

for module in $(perldoc -u perllocal | grep -F 'C<Module> L<' | sed 's/^.*L<\(.*\)|.*>$/\1/' | sort | uniq) ; do
    if [[ "$module" =~ "App::cpanminus" ]]; then
        continue
    fi
    echo "Uninstalling $module..."
    yes | cpanm --uninstall "$module"
done

cpanm --uninstall App::cpanminus
0

I think the best option is uninstall Perl and install it again.

1
  • 8
    The only problem with this suggestion is that you should NOT do this is you are using your system-wide install on Linux/Mac. If you are on Windows then this is actually probably preferred. Oct 15, 2011 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.