This method should work on all versions of Ubuntu (and maybe Debian too) so did not add any version number in the title of this tutorial.
So firstly you have to install the building tools available for Ubuntu with:
sudo apt-get install build-essential
Once the installation is complete you will have to install the dependent jpeg library:
sudo apt-get install libjpeg-dev
Next is to download the latest source of JpegOptim from https://github.com/tjko/jpegoptim (version 1.4.3 as of now):
And extract the downloaded archive and move into the directory:
tar -xzvf RELEASE.1.4.3.tar.gz cd jpegoptim-RELEASE.1.4.3
Now all you have to do is configure the source for build and 'make' the files:
./configure make make strip make install make clean
If everything went as expected (i.e. no error occurred) then you have successfully built and installed JpegOptim to your system! :D
And to get the full path of the JpegOptim executable run:
Which should return something like:
Good Luck with your adventure!