Installing mozjpeg on Ubuntu 16.04 & 18.04 (Forge)
Tags: devops, images, performance, webperf
Published: 2020-02-14 23:00:13

---
title: Installing mozjpeg on Ubuntu 16.04 & 18.04 (Forge)
published: true
date: 2016-12-14 00:57:00 UTC
tags: devops,images,performance,webperf
canonical_url: https://nystudio107.com/blog/installing-mozjpeg-on-ubuntu-16-04-forge
---

# Installing mozjpeg on Ubuntu 16.04 & 18.04 (Forge)

### If you want to go the extra mile opti­miz­ing your images, here’s how to install mozjpeg on Ubun­tu 16.04 or 18.04 (Forge)

Andrew Welch / [nystudio107](https://nystudio107.com)

![Extra Mile](https://nystudio107-ems2qegf7x6qiqq.netdna-ssl.com/img/blog/_1200x675_crop_center-center_82_line/extra-mile.jpg)

**Updat­ed** for the new CMake-based build system.

For those of you who read the [Cre­at­ing Opti­mized Images in Craft CMS](https://dev.to/gaijinity/creating-optimized-images-in-craft-cms-2hc5-temp-slug-2354251) arti­cle, and are inter­est­ing in going the extra mile… there is anoth­er loss­less JPEG image opti­miza­tion tool called <kbd>mozjpeg</kbd> that you might want to utilize.

The rea­son I did­n’t include it in the orig­i­nal arti­cle is that there’s no pack­age that you can just <kbd>apt-get install</kbd>. It requires a lit­tle more work, but it’s worth it. In my tests, I’m sav­ing between <kbd>3-5%</kbd> on the images cre­at­ed with <kbd>mozjpeg</kbd> vs. <kbd>jpegoptim</kbd>, depend­ing on the image. Here’s a nice [Com­par­i­son of JPEG Loss­less Com­pres­sion Tools](https://blarg.co.uk/blog/comparison-of-jpeg-lossless-compression-tools) if you like look­ing at stats.

For exam­ple, here’s an image I used in my blog in its unop­ti­mized (but prop­er­ly resized) glory:

![Nice View Unoptimized](https://nystudio107-ems2qegf7x6qiqq.netdna-ssl.com/img/blog/_992x496_crop_center-center_100_line/nice-view_unoptimized.jpg)

Next, the image opti­mized with <kbd>jpegoptim</kbd>:

![Nice View Jpegoptim](https://nystudio107-ems2qegf7x6qiqq.netdna-ssl.com/img/blog/_992x496_crop_center-center_100_line/nice-view_jpegoptim.jpg)

And here is the same image opti­mized with <kbd>mozjpeg</kbd>:

![Nice View Mozjpeg](https://nystudio107-ems2qegf7x6qiqq.netdna-ssl.com/img/blog/_992x496_crop_center-center_100_line/nice-view_mozjpeg.jpg)

Can you tell the dif­fer­ence between these images? You should­n’t, because the opti­miza­tion is _loss­less_. The image itself is unchanged. The sav­ings are not a huge dif­fer­ence in terms of <kbd>jpegoptim</kbd> vs. <kbd>mozjpeg</kbd> (<kbd>81,950</kbd> bytes vs <kbd>79,822</kbd> bytes), but <kbd>2k</kbd> is <kbd>2k</kbd>… and the dif­fer­ence can be more than this <kbd>~3%</kbd> sav­ings depend­ing on the image.

<kbd>jpegoptim</kbd> went from <kbd>82,011</kbd> bytes in the orig­i­nal image to <kbd>81,950</kbd> bytes, a sav­ings of just <kbd>0.07%</kbd>

<kbd>mozjpeg</kbd> went from <kbd>82,011</kbd> bytes in the orig­i­nal image to <kbd>79,822</kbd> bytes, a sav­ings of <kbd>2.66%</kbd>

Obvi­ous­ly the huge gains in opti­miz­ing images are in ensur­ing that they are [prop­er­ly sized & com­pressed](https://dev.to/gaijinity/creating-optimized-images-in-craft-cms-2hc5-temp-slug-2354251) to begin with, which these were, cour­tesy of Imager. But lop­ping off the unnec­es­sary cruft in a loss­less man­ner via <kbd>mozjpeg</kbd> is the cher­ry on top.

And it’s not that hard to install it. So if you’re on-board, and feel com­fort­able using a ter­mi­nal… away we go!

The instruc­tions here were all done on a VPS run­ning Ubun­tu 16.04 or 18.04 via [Forge](https://forge.laravel.com). They also work fine on my local dev [Home­stead](https://laravel.com/docs/5.3/homestead) envi­ron­ment, which is also Ubun­tu 16.04.

First, make sure you have all of the pack­ages we need to build <kbd>mozjpeg</kbd> installed via:

```

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

```

Next <kbd>cd</kbd> to your build direc­to­ry (just <kbd>cd ~</kbd> is prob­a­bly fine) and clone the git repo:

```

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

```

Then do the fol­low­ing to con­fig­ure and com­pile <kbd>mozjpeg 3.x</kbd>:

```

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

```

Go grab a cup of cof­fee, the <kbd>cmake</kbd> com­mand will take a bit. Now we need to install it; by default it’ll be installed in <kbd>/opt/mozjpeg</kbd> directory:

```

sudo make install

```

Now we’ll cre­ate a sym­link to the spe­cial <kbd>mozjpeg</kbd> ver­sion of <kbd>jpegtran</kbd> into a path that’s in our <kbd>$PATH</kbd> (and where [Imager](https://github.com/aelvan/Imager-Craft) expects it to be):

```

sudo ln -s /opt/mozjpeg/bin/jpegtran /usr/bin/mozjpeg

```

Then go into the <kbd>imager</kbd> plu­g­in fold­er, dupli­cate the <kbd>config.php</kbd> file, rename it <kbd>imager.php</kbd>, and copy that <kbd>imager.php</kbd> file into your <kbd>craft/config</kbd> direc­to­ry. This file lets us cus­tomize how Imager works. Open the <kbd>imager.php</kbd> file up in your text edi­tor, and enable <kbd>mozjpeg</kbd> like this:

```

'mozjpegEnabled' => true,
  'mozjpegPath' => '/usr/bin/mozjpeg',

```

Make sure you set <kbd>'jpegoptimEnabled' =&gt; false,</kbd> if you were using <kbd>jpegoptim</kbd> already. I also do this:

```

'optimizeType' => 'runtime',

```

…so that Imager opti­mizes the images imme­di­ate­ly, rather than via a task, because I had a sit­u­a­tion where my CDN was grab­bing & caching the images before the Imager task opti­mized them.

That’s it! Enjoy your super-opti­mized images, and con­grats on going the extra mile!

I’d also high­ly sug­gest read­ing the arti­cle [Using WebP images in Craft with Imager](https://www.vaersaagod.no/en/using-webp-images-in-craft-with-imager) to add <kbd>webp</kbd> sup­port via Imager.

## Further Reading

_If you want to be notified about new articles, follow [nystudio107](https://twitter.com/nystudio107) on Twitter._

<small>Copyright ©2020 nystudio107. Designed by nystudio107</small>