Optimize WordPress Images Without Plugins – Ubuntu Guide

If you run a WordPress site or manage any media-heavy project on an Ubuntu server, you’ve probably wrestled with the challenge of keeping images fast-loading and storage-efficient. Most guides will point you straight to plugins like Smush or EWWW, but what if you want more control, better performance, and fewer dependencies? If you have root access to your server, you can optimize your entire media library directly from the command line—no plugins required.

Why Go Plugin-Free for Image Optimization?

Relying on plugins for image optimization is convenient, but it comes with trade-offs. Plugins can slow down your site, introduce security risks, and often require paid plans for advanced features or bulk processing. By handling image optimization at the server level, you get full control over the process, can automate it as you see fit, and ensure your site remains lean and secure.

Meet Your Optimization Tools: jpegoptim and oxipng

To get started, you’ll need two powerful command-line tools: jpegoptim for JPEGs and oxipng for PNGs. Both are open-source, lightweight, and designed for speed and efficiency. With these, you can compress images losslessly or, in the case of JPEGs, even apply lossy compression for even smaller file sizes.

Installing the Tools

On Ubuntu, installation is straightforward. Open your terminal and run:

sudo apt update
sudo apt install jpegoptim oxipng

If oxipng isn’t available in your repositories, you can install it via Rust’s Cargo package manager:

sudo apt install cargo
cargo install oxipng

Optimizing JPEG Images with jpegoptim

Basic Optimization

Optimize all JPEGs in a directory:

jpegoptim *.jpg

Recursive Optimization (Including Subfolders)

jpegoptim -r /var/www/html/wp-content/uploads

Strip Metadata for Maximum Savings

jpegoptim --strip-all -r /var/www/html/wp-content/uploads
  • Removes EXIF and comment data, reducing file size further.

Set Maximum File Size (Optional)

jpegoptim --size=200k image.jpg
  • Tries to compress image.jpg to 200 KB or less.

Optimizing PNG Images with oxipng

Basic Optimization

oxipng image.png
  • Overwrites the original with an optimized version.

Batch Optimization in a Folder

oxipng *.png
  • Optimizes all PNGs in the current folder.

Recursive Optimization (All Subfolders)

find /var/www/html/wp-content/uploads -type f -iname "*.png" -exec oxipng -o 4 {} +
  • -o 4 is a good balance of speed and compression; increase to -o max for maximum compression.

Strip Metadata and Optimize Transparency

oxipng -o max --strip all --alpha image.png
  • Removes all non-essential metadata and optimizes alpha channels (for transparent PNGs).

Automating the Process (Optional)

For regular optimization, consider a simple shell script:

find /var/www/html/wp-content/uploads -type f -iname "*.jpg" -exec jpegoptim --strip-all {} +
find /var/www/html/wp-content/uploads -type f -iname "*.png" -exec oxipng -o 4 --strip safe {} +

Save as optimize-images.sh, make executable (chmod +x optimize-images.sh), and run as needed.

Beyond WordPress: Optimizing Any Media Folder

These same techniques work for any folder of images on your server. Maybe you’re running a static site, managing a photo archive, or prepping assets for a web app. Just point the commands at your target directory. For example, to optimize a folder called /home/user/photos, simply replace the path in the commands above.

Performance and Automation

One of the biggest advantages of server-side optimization is performance. jpegoptim and oxipng are both fast and efficient, capable of processing thousands of images in minutes. oxipng, in particular, leverages multiple CPU cores for even faster PNG compression. This makes them ideal for bulk operations and regular maintenance.

If you want to automate optimization, you can wrap these commands in a shell script and run it on a schedule with cron. This ensures your media library stays optimized without manual intervention.

Best Practices

Before you start, always back up your images. While these tools are reliable, it’s best to have a fallback in case you want to revert changes. Test your settings on a small batch of images to find the right balance between file size and quality. For JPEGs, quality levels between 75-85 usually offer significant savings with minimal visible difference. For PNGs, higher optimization levels take longer but squeeze out more bytes.

Conclusion

Optimizing images directly on your Ubuntu server gives you unmatched control and efficiency, whether you’re running WordPress or managing any other kind of media library. By using jpegoptim and oxipng, you can keep your site loading fast, save on storage, and avoid the bloat and limitations of plugin-based solutions. If you’re comfortable with the command line and have root access, this approach is not only more powerful—it’s also more sustainable for the long run.

Don’t have the time to do this yourself? You can always reach out to us and we can do it for you.