Optimizing Gentoo

Lately, I have been busy hacking away at the Gentoo forums, looking for ways to best optimize my system, other than the normal CFlags optimizations and ones covered in the manual.

If you haven’t set up USE or CFlags, check out Gentoo’s main website, read the manual again, and check out some of the other documentation they have.

Free up Space

Deleting downloaded source code and package work directories is a good way to save a lot of harddrive space, while being risk-free. If you are a 56K user, skip this one, as it deletes files that may need to be downloaded again later. Personally, I use a cron job to do this, but you may use it any way you wish. Try this out for size:

rm -r /usr/portage/distfiles/* && rm -r /var/tmp/portage/*

For my cron job, I also update nightly (not ideal for servers or anyone but bleeding-edge type people):

emerge sync && emerge -uD world && rm -r /usr/portage/distfiles/* && rm -r /var/tmp/portage/* && shutdown -h now


The next optimization came as a surprise to me. After over two years of using Gentoo, I have just realized that “ccache” is not installed on a system by default, but is defaulted to on in portage. Summary of ccache (taken from man):

ccache is a compiler cache. It speeds up re-compilation of C/C++ code
by caching previous compiles and detecting when the same compile is
being done again.

It will only make a small difference in most cases, but go ahead and:

emerge ccache
emerge info | grep ccache
ccache -s

On “emerge info | grep ccache,” it should display the version and say “[enabled].” If not, be sure to add it to /etc/make.conf in your portage options.


Installing and using deltup will give a great benefit to people using 56K or other low-speed internet connections. Deltup checks for differences in previously updated packages and downloads the new one, threading the old package into the new one, effectively reducing bandwidth by a large margin. Unfortuonately, deltup has become outdated, but there is a new project out that is kept up, so here’s how to get it up and running.

First, you need to know how to “inject” ebuilds into portage. Basically, you are adding your own package to the system. Open up /etc/make.conf and change the value of PORTDIR_OVERLAY. I suggest changing it to “PORTDIR_OVERLAY=/usr/overlay/portage” and make sure that the directory exists.

It’s almost as easy as that. Just place your ebuild into the overlay directory and emerge the name of the package.

Read the official dynamic deltup server page for details in installing it.

Swap Management

The best way to have swap installed is having your root parition on one drive and swap on a completely seperate one, but that is not always possible with most users. For users who have kernel 2.6.0 and up, you can use a feature called swappiness.

Swappiness defaults to a value of 60 and is raised and lowered by editing /etc/sysctl.conf with:

vm.swappiness = x

It ranges from a value of 0 to 100. The closer to 0 you get, the more the kernel tries to free RAM before using swap. The closer to 100 you get, the more readily the kernel will use swap.

Portage Search Speedup

A simple and risk-free (easily removable) addition to portage is to use psycho to speed up searching.

Psycho should already be emerged in your system, but if it isn’t, just “emerge psycho” and then add the portion of code below to /usr/bin/emerge (+ denotes added code, do not actually insert a +).

import portage

import psyco
except ImportError:
#End revision

import emergehelp,xpak,string,re,commands,time,shutil,traceback,atexit,signal,s$
from stat import *
from output import *

Kernel 2.6.X

If you are still using kernel 2.4.X, I highly recommend the upgrade to 2.6.X. (gentoo-development-sources is good) 2.6.X improves system speed by a large margin, is more stable, and is being updated constantly. More on compiling the kernel can be found on the Gentoo forums or in the Gentoo documentation.

More optimizations coming as I find them.

Continue to Optimizing Gentoo (Part 2)

8 Responses to “Optimizing Gentoo”

  1. JRB Says:

    emerge -uD nightly! Not a good idea. This is the kind of things that leads users of other distros to say Gentoo is unstable, and to criticize Gentoo users. Updating nightly, especially automatically, is an incredibly bad idea. In most cases you will be breaking things and spend more time updating configuration files than you will using your system. It’s certainly not a good idea in terms of security, because you are constantly changing your system without even knowing what is being done. I always laugh when I hear people say…”I can’t believe I was hacked, I emerge world every night.”

  2. Dale Pontius Says:

    However I do run “emerge sync” and “emerge -uDp world” every night, and the next day I can see what needs to be done. One of these days I plan to incorporate an “emerge -f” into that, so things will be there and ready for me.

  3. JAT Says:

    I agree that automatically emerging world every night is a bad practice. I update my system frequently, but always run emerge -uDp before so I can see what is going to be done. Care needs to be taken when updating config files as well (etc-update) as this can be a big source of headaches if done carelessly.

  4. stahlsau Says:

    I don´t think it´s dangerous to update full-automated during the night. I do it for some time and never had problems. The most updates in portage are bugfixes and downgrades cause of securityholes, both things you´ll want to get done as fast as you can if you want a secure system.
    I run dispatch-conf every morning, mostly there´s nothing to do and if there is something, it´s done in a few minutes, so one really doesn´t spend more time fixing the configs than using the system ;)
    Also, i´ve never broken things through upgrading, if i broke s/t it was because i refused to downgrade or s/t like that.

  5. Clete R. Blackwell 2 Says:

    stahlsau — what does dispatch-conf do?

  6. Peter Herndon Says:

    stahlsau, you are either a relative newbie to Gentoo, you are tracking stable only, or you’ve been *REALLY* lucky. I’ve had updates seriously break my ‘puter about four times in two years of running Gentoo. Admittedly, I’ve always tracked unstable, so I am happy to take responsibility for it, but it has happened. I’ve never done the updates as a cronjob, though, I’m always sitting there doing something else while it updates in the background.

  7. Vivek Says:

    Good one for gentoo buffs :)

  8. Freek Sanders Says:

    The portage speedup change should be marginally different.

    Since the whole code is python, it should be indented:

    import psyco
    except ImportError:
    #End revision

    PS: use tabs instead of spaces

Leave a Reply