I can't write an exact guide as I don't use ubuntu, but I can tell you it was easier than I thought it would be. I did the same thing on Fedora a few months back. Basically I went for a 'rip-and-replace' kind of solution, probably not the best one thinking about it now.
Originally Posted by mmstick
I removed the relevant packages (outside the package manager in some cases as it would otherwise mean getting rid of dependencies I wanted to keep) and then built Mesa, GLAMOR, xf86-video-ati and libtxc_dxtn in that order (libtxc_dxtn is a runtime dependency if I remember rightly). The key thing was using the right configure switches to make sure the dri modules and so on go to the right location, but you can figure this out by looking at your existing system first. Also, if you are on an x64 system, you will obviously need to build Mesa twice if you want 32-bit compatibility.
Sorry, not very precise I know but hopefully will point you in the right direction - bear in mind the way I did it could potentially hose your X install if the ABIs are mismatched or anything else like that, so take great care :-D
I think there is also a way to build a separate Mesa and use that with LD_LIBRARY_PATH, not tried it myself though.