Casey Hofford

Freelance Web Design Services

Hire Me

Improved image compression: Installing mozjpeg on an Ubuntu Server

Mozjpeg is a piece of software that provides greatly improved compression for jpeg images. Installing this program on your server will make it easy to compress photos, often by over 80% while retaining good image quality.

One option to take advantage of this is squoosh.app, this web application allows you to compress images into a variety of formats. You can adjust the quality and see a live side-by-side preview of the image before and after. This is a great way to optimize individual images, however it quickly becomes tedious for larger tasks.

The alternative is to install the mozjpeg source code to get this compression from the command line. With this installed on your server you can easily automate image compression.

Here are the steps to get mozjpeg installed on your Ubuntu server.

Download Dependencies and Source Code

First, install needed dependencies

sudo apt-get update
sudo apt-get install cmake autoconf automake libtool nasm make pkg-config

Next we need to download and install the actual program. mozjpeg does not have compiled installers available, so you will need to build it manually. This is super easy in bash!

First download the repository from github using a git clone. You can also manually download the repo from
https://github.com/mozilla/mozjpeg

git clone https://github.com/mozilla/mozjpeg.git

Note: Do not download the zipped releases from https://github.com/mozilla/mozjpeg/releases , I initially tried using the tar.gz files here, but it looks like they are intended for windows computers.
If you are getting an error that says you cannot use cmake, make sure you have downloaded the repo.

Building mozjpeg for your system

Now that you have the code, it is time to build it for your system! These steps were done on Ubuntu 18.04 but should work on most Debian variants.
We need to enter the newly created folder with the code, create a build folder and then compile the code for our system. The commands are below.

cd mozjpeg
mkdir build && cd build
sudo cmake -G"Unix Makefiles" ../

Next we need to install the compiled code. By default, the code will be installed at /opt/mozjpeg

sudo make install

Using mozjpeg

Now the program cjpeg is installed at /opt/mozjpeg/cjpeg. You can run this program by specifying its full path each time, but it is easier if we add it to our path. This allows us to just run the command “cjpeg” directly from bash.

cd /usr/local/bin
ln -s /opt/mozjpeg/bin/cjpeg

Now mozjpeg is installed! In order to use it we can run the command

cjpeg [switches] <source-image> <destination-image>

The most common switch is -quality, which will allow you to set the quality of the compressed image. This is measured in percents. A range of 70-90 should produce good results for most use cases. Run the command as below.

cjpeg -quality 90 ./image.bmp ./image.jpg

Troubleshooting

Building NASM from source

I manually installed NASM. This is an assembly compiler that is needed for cmake. The apt install command at the beginning of this article should cover this, so don’t worry about this if you successfully compiled mozjpg. This shouldn’t be necessary, but if you are having issues with compiling from source this may help. NASM can be installed with these steps:

Find the newest stable release of NASM at
https://www.nasm.us/pub/nasm/releasebuilds/

Copy the URL of the .tar.gz file for this version.

Run the command below to install NASM

wget http://www.nasm.us/pub/nasm/releasebuilds/<VERSION NUMBER>/nasm-<VERSION NUMBER>.tar.gz
cd nasm<VERSION NUMBER>
./configure 
make  -j8
sudo make install

Sources:
Using mozjpeg to Create Efficient JPEGs
Installing mozjpeg on Ubuntu 16.04 & 18.04 (Forge)
MOZJPEG Build Instructions
Installing libjpeg-turbo

Leave a Reply

Your email address will not be published. Required fields are marked *