Installing PHP APC on Ubuntu Dapper (and Debian?)

Update: this article is for Ubuntu Dapper Drake (6.06). If you are using later versions, or other Linux distributions, then visit the following links:

APC is a op-code cache for PHP that eliminates the parsing and compiling of script(s) for every page hit. Unlike Zend, it is truly free, and seems to have a lot of commuity momentum behind it lately.

As with all optimizations, there is a trade off. APC can use more memory on your server than without it.

The instructions below assumes that you have full control of your server, whether a physical dedicated server or a virtual private server.

Although one can always compile from source, this is more time consuming than using Debian's excellent apt-get.

Here is how I did it on two machines, with one more to go.

Getting the packages

First, we run apt-get to get what we need, if it is already not there.

apt-get update
apt-get install apache2-dev php5-dev php-pear make 

Preparing for pecl

I am using Apache2 and PHP5 on the development server. Adjust the versions above if necessary to match your environment.

Since I am using Apache2, the apxs is named differently, and pecl will fail if I do not work around this. So, I had to provide an apxs which is just a symbolic link.

ln -s /usr/bin/apxs2 /usr/bin/apxs

Running pecl

Next we use pecl to download, compile and install APC.

pecl install apc

My development server is now an AMD Athlon 64, using a 64-bit kernel. It had a strange problem running the above pecl command, with an error saying:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 143 bytes) in /usr/share/php/PEAR/PackageFile/v2.php on line 1772

The same Ubuntu version on my Pentium III test server does not have this error at all. It has the same Apache and PHP versions as well.

Others have encountered this problem, and the workaround as provided in this page, is to adjust the memory inside the script (it does not work in the php.ini).

Edit the /usr/share/php/pearcmd.php, and add:

@ini_set('memory_limit', '16M');


Now you can run the above pecl command once more.

Now, all you need to do is to add the following to the /etc/php5/apache2/php.ini file:

Now, restart Apache using:

/etc/init.d/apache2 restart 

Now you have a code cache that should speed up Drupal, and you can benefit.

To upgrade APC in the future, use the commands:

pecl upgrade apc

Followed by:

 /etc/init.d/apache2 restart

More info

APC is not the only op-code cache / accelerator.

You can read more in PHP op-code caches / accelerators Drupal big site study.

There is also a benchmark of APC and eAccelerator using Drupal.

Links and Resources



Memory limit

When you say the memory limit doesn't work by changing the php.ini file, did you change the /etc/php5/cli/php.ini? My dapper system has separate ini files, and I've had to change the command line memory limit in the past to get some pear install stuff to work.

Tried them all

Yes, I tried them all, but still did not work:

# grep memory_limit /etc/php*/*/php.ini
/etc/php5/apache2/php.ini:memory_limit = 16M ; Maximum amount of memory a script may consume (8MB)
/etc/php5/cgi/php.ini:memory_limit = 16M ; Maximum amount of memory a script may consume (8MB)
/etc/php5/cli/php.ini:memory_limit = 16M ; Maximum amount of memory a script may consume (8MB)

This is a problem on amd64 Dapper only. The other two machines worked fine.
Khalid Baheyeldin

Ubuntu server

Just did this on my ubuntu server remotely from work, great write up.

Don't forget to apt-get install make, g++ and gcc -- actually spent longer working out g++ wasn't installed on my minimalist server.

php.ini now uses conf.d

For php5 + apache2 + debian etch, the APC activation "" should now be placed in /etc/php5/conf.d/apc.ini instead of /etc/php5/apache2/php.ini. This is because, as of version 5.1.6-5, php in debian has been reworked to use a conf.d style configuration for extensions. This change should hit Ubuntu 7.04 "Feisty", and also applies to the latest versions of php4. More here.

Debian Sarge

When running Debian Sarge a few extra steps are needed:

You have to upgrade pear before pecl is available. Before:

pecl install apc

Upgrade pear to 1.3.3 (< versions do not upgrade automatically to the latest)

pear upgrade pear-1.3.3

Next upgrade to the latest pear (or issue pear upgrade-all)

pear upgrade pear

During this process you may need to increase the memory limit as described to as high as 64M. After this, pecl is available.

while installing APC on my

while installing APC on my ubuntu machine i am getting bellow errors:
I was not able to successfully run APXS. Possible reasons:

1. Perl is not installed;
2. Apache was not compiled with DSO support (--enable-module=so);
3. 'apxs' is not in your path. Try to use --with-apxs=/path/to/apxs
The output of apxs follows
./configure: line 3845: apxs: command not found
configure: error: Aborting

but i installed perl also . please help me

Installed on etch without a

Installed on etch without a problem. For now works ok.


Possible error in install

Excellent tutorial for installing APC!

I had issues finding the apxs program. What worked for me was installing the apache2-threaded-dev package instead of apache2-dev (which was not found in my repositories). Also, with this package I did not need to use the symbolic link instruction.

Thanks again for the guide.

hai all, I am not able to

hai all,

I am not able to finding the problem while installing apc.And I am getting below errors. please any one can help me.

Sorry, I was not able to successfully run APXS. Possible reasons:

1. Perl is not installed;
2. Apache was not compiled with DSO support (--enable-module=so);
3. 'apxs' is not in your path. Try to use --with-apxs=/path/to/apxs
The output of /usr/bin/ follows
/tmp/pear/cache/APC-3.0.15/configure: line 3845: /usr/bin/: is a directory
configure: error: Aborting
ERROR: `/tmp/pear/cache/APC-3.0.15/configure --with-apxs=/usr/bin/' failed

and i installed perl like below
root@prashanth-desktop:~# which perl
perl perl5.8.8 perlbug perlcc perldoc perlivp
root@prashanth-desktop:~# which perl

then i configured /etc/php5/conf.d/apc.ini instead of /etc/php5/apache2/php.ini.