95

I need to clean up my server from PHP 5.3 packages (there are plenty of them) in order to be able to compile PHP 5.2. What is the easiest/safest method to get rid of them all?

corev
  • 1,317

10 Answers10

140

This method is not advised to be used without careful review. Read below comments.


This should completely remove any package with a name that starts with php and anything related to it.

sudo apt-get purge 'php*'

DON'T PRESS y UNTIL YOU ENSURE that in the removing packages list there are no other packages (besides related to php packages), like:

php-common* python-openssl* php-curl* ... and tons of packages

If so, type n, copy the list (& tidy up from the unrelated packages), and manually remove them:

sudo apt-get purge      php-common* php-curl*    ... and tons of packages
T.Todua
  • 551
  • 1
  • 4
  • 15
RobinJ
  • 8,910
  • OK thanks both of you. I just chosen your answer because Marco does not seem to have lack of Karma :) – corev Sep 03 '11 at 18:11
  • Alright, any ideas on the best way to install PHP 5.2 now? – corev Sep 03 '11 at 18:19
  • Download: http://be.php.net/distributions/php-5.2.17.tar.bz2, extract, open a terminal inside the folder, ./configure, make, sudo apt-get install checkinstall, sudo checkinstall. – RobinJ Sep 03 '11 at 18:30
  • 14
    This exact command just wiped ALL the packages from my system. I don't advice anyone to use it ever. – Caballero Oct 21 '13 at 10:32
  • @RobinJ - I am afraid I was having the same feeling that it was not working in our interest. Why did it remove packages that didn't have PHP any where in its name? – Ziyan Junaideen Jan 14 '14 at 07:46
  • 24
    php* will get it treated as a regex, and ever package with ph in its name will get removed. – muru Oct 28 '14 at 19:06
  • I think you need to put "php*" in quotes otherwise it will interpret it as a file that doesn't exist. – Sridhar Sarnobat Jun 18 '16 at 17:12
  • 9
    Am I the only one thinking this is not really a safe idea? – Daan Dec 20 '16 at 10:21
  • This method doesn't work on my machine, I still able to do php -v and outputting the version of installed php. – satriahrh Jan 30 '18 at 05:08
  • This answer is wrong. The command line call for this uses shell globbing, not regex. The pattern php.* will match any packages beginning with php., and not handle like a regex to match any character. The proper glob which is extremely dangerous to run on its own is to use php*, and you really should not do this. – Thomas Ward May 17 '18 at 00:03
  • @ThomasWard I know. That's what I originally had in the answer and someone else changed it. – RobinJ May 17 '18 at 13:41
  • @RobinJ so fix it? There's an edit button for a reason you know. (I just fixed it, and added a bit to protect against Bash shell globbing which can cause problems when it expands php* to be any filename or directory name starting with 'php' in the current working directory.) – Thomas Ward May 17 '18 at 14:53
  • @ThomasWard Thank you. I did not do this myself as I assumed it would not be appreciated if I simply removed someone's edits (in case maybe there was something special about my system that made their suggestion not work). – RobinJ May 31 '18 at 09:12
  • maybe also clean; autoclean; autoremove – quanta May 28 '19 at 09:19
  • 1
    Be very careful with autoremove. – RobinJ May 28 '19 at 16:27
  • please, someone remove this answer. – Esther Jan 18 '21 at 23:20
113

I do not recommend running sudo apt-get purge php*.

That was scary! Fortunately, I didn't type the -y option, because it chose about hundred of packages without php in their name.

sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

How it works:

First, a list of packages is generated using this series of commands: dpkg -l | grep php| awk '{print $2}' |tr "\n" " ".

Hint: You can run this part of the command in your terminal to see what packages would get removed. You should get something like: libapache2-mod-php5 php5 php5-cli php5-common php5-json

Finally, when you run the full command, this list of packages gets passed to sudo apt-get purge, removing all of the packages.

Hint: If it feels safer to you, you could just as easily run them separately, and copy+paste the list of packages to remove like so: sudo apt-get purge libapache2-mod-php5 php5 php5-cli php5-common php5-json

bento
  • 1,241
  • 9
    This should be the accepted answer – Dark Star1 Aug 04 '14 at 21:28
  • 1
    Sounds like you're suggesting one syntax is bad because too wide but do you mind explaining what your command does, so people may be able to judge if that's appropriate before running it? – Vince May 12 '15 at 19:24
  • 2
    What if aptitude is not installed or cannot be installed on Ubuntu, how about apt-get cache search ? – Vicky Dev Apr 27 '16 at 11:30
  • If you've upgraded your OS and doing this to get clean state - don't forget to remove repository(ies) ppa:ondrej/php if you used them before installing PHP again, otherwise you will endup with broken installs :) – jave.web Feb 20 '21 at 23:24
  • I also agree with @DarkStar1. This answer should be the accepted answer. I ran RobbinJ's answer and it didn't end well. It totally destroyed my system. Luckily, I made a backup image of the system before running his command. So, I was able to restore my system. – ThN Mar 19 '21 at 18:36
24

You'll probably want to purge all the php* packages from your system. Something with a wild-card should work

sudo apt-get purge php.*

You may be interested in How to rollback to PHP 5.2 for where to go next.

Marco Ceppi
  • 48,101
5

First, you have to remove Apache and all of its dependencies with:

sudo apt-get purge apache2 php5 libapache2-mod-php5 mysql-server libapache2-mod-auth-mysql php5-mysql phpmyadmin

and then:

sudo rm -rf /etc/apache2; rm -rf /etc/php5; rm -rf /var/lib/mysql; rm etc/mysql 

If you have any issues with phpMyAdmin, try entering this into the Terminal:

dpkg-reconfigure phpmyadmin
techraf
  • 3,316
  • ***Note to those who copy-paste without reading carefully, this will also remove apache, which is not what OP asked for – adavea Apr 16 '18 at 14:01
2
sudo apt-get -y purge libapache2-mod-php5 libapache2-mod-php5filter libexpect-php5 libgv-php5 libow-php5 php5 php5-adodb php5-auth-pam php5-cgi php5-cli php5-common php5-curl php5-dbg php5-enchant php5-exactimage php5-ffmpeg php5-fpm php5-geoip php5-gmp php5-idn php5-imagick php5-imap php5-interbase php5-intl php5-json php5-lasso php5-ldap php5-librdf php5-mapscript php5-memcache php5-memcached php5-mhash php5-midgard2 php5-ming php5-mssql php5-mysql php5-mysqlnd php5-odbc php5-pgsql php5-ps php5-pspell php5-radius php5-recode php5-remctl php5-rrd php5-sasl php5-snmp php5-sqlite php5-suhosin php5-svn php5-sybase php5-tidy php5-tokyo-tyrant php5-uuid php5-xcache php5-xdebug php5-xmlrpc php5-xsl

In your case, you could search for all the packages:

sudo aptitude search php5|awk {'print $2'}|grep -v i386|grep -v "^A"|tr "\n"  " "

and then purge them:

sudo apt-get purge <list of packages found>
Eric Carvalho
  • 54,385
2

In addition to any purges mentioned in the other answers, if you want to be completely sure everything is removed, you're likely to want to run

cd /
sudo find -name "php"

Which will show you files/folders with the name php in them. You're then going to want to check each of these file paths, i.e.:

cd /usr/local/include/php
ls

And delete them as is appropriate. Be careful not to delete anything that's critical to another process you need to keep running, though, unless you want to have to reinstall that too.

To remove a directory, or file, we use the rm comment with the -rf flag, so given the above example, we'd type:

sudo rm -rf /usr/local/include/php
1

The regex the two people said is incorrect.
Instead of using php.* or php* One should use ^php*

Thats the right answer.
So you would use: sudo apt-get purge "^php*"

Although I dont recommend using this method cause its highly automated and I encourge you to use sudo apt list --installed | grep php to get the list of packages and use these package names that you got from above command in this command:

sudo apt-get purge PACKAGENAME_ONE PACKAGENAME_TWO

  • This doesn't remove libapache2-mod-php, for instance. – Patrick B. Aug 13 '18 at 18:17
  • But php.* will also cover cases where php appears at the beginning of the package name. There's no reason to use both ^php* and php.* Also, it should be ^php.*; apt-get seems to know that php* is a glob, not a regex, but when you add the ^ character it can't possibly be a glob, so you'll get every package that starts in "ph." – Patrick B. Aug 14 '18 at 14:32
0
apt list --installed 2>/dev/null | awk -F '/' '{print $1}' | grep '^php' | xargs -r sudo apt purge;
sudo rm -rfv /etc/php/5.3;

Explanation

apt list --installed 2>/dev/null - list of installed packages with version and other info;

apt list --installed 2>/dev/null | awk -F '/' '{print $1}' - list of installed package names only;

apt list --installed 2>/dev/null | awk -F '/' '{print $1}' | grep '^php' - list of installed package names only starting in php;

apt list --installed | awk -F '/' '{print $1}' | grep '^php' | xargs -r sudo apt purge- pass list of installed package names only starting in php as arguments for sudo apt purgecommand;

sudo rm -rfv /etc/php/5.3 - remove all configuration files for php5.3;

If necessary

sudo rm -rfv /etc/php/5* - remove all configuration files of all php5 versions (php5.3, php5.4, php5.5 etc.);

sudo rm -rfv /etc/php - remove all configuration files of all php versions (php5, php7 etc.);

Victor S.
  • 103
0

I use sudo apt remove php8.0* It removes php and all related packages - for the given version - and downgrades the system to any other existing, installed (older) php version.

0

First I run sudo apt list --installed | grep php to show all installed PHP versions then I removed the mentioned specific PHP version (8.1) with all related dependences (extensions) by run sudo apt-get purge 'php8.1*'