OptiPNG:
http://optipng.sourceforge.net/
For jpegran (older version, but I think same logic applies):
jpegtran is part of a package of few tools known as libjpeg
1) Get the source code from here. It's the file called jpegsrc.v6b.tar.gz. Using cURL you can download like:
curl
http://www.ijg.org/files/jpegsrc.v6b.tar.gz > /tmp/libjpeg.tar.gz
2) Uncompres the package, e.g. tar -xzvf /tmp/libjpeg.tar.gz
3) go to the directory that contains the uncompressed code, e.g. cd /tmp/jpeg-6b
4) ./configure
5) sudo make install
Via
http://www.lcdf.org/gifsicle/ the source for Gifsicle can be found and also an OSX port I believe.