janoliver
janoliver

Reputation: 7824

Uninstall all perl modules installed by cpan

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?

Upvotes: 26

Views: 43874

Answers (6)

Andrew Selivanov
Andrew Selivanov

Reputation: 1356

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

Upvotes: 1

Maikel
Maikel

Reputation: 554

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.

Upvotes: 1

Flimm
Flimm

Reputation: 150713

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

Upvotes: 19

Joel Berger
Joel Berger

Reputation: 20280

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.

Upvotes: 31

xenoterracide
xenoterracide

Reputation: 16847

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.

Upvotes: 2

DavidEG
DavidEG

Reputation: 5957

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

Upvotes: 0

Related Questions