Modern web development demands rapid page load speeds and lean network payloads. While legacy graphic formats such as JPEG and PNG have served the internet for decades, the Google-developed WebP format provides superior lossy and lossless compression, delivering file sizes roughly 25% to 35% smaller than comparative JPEGs without visible loss of visual fidelity.
For Linux system administrators, backend engineers, and web developers, the command-line utility cwebp (part of the official webp toolset) offers the fastest, most flexible way to encode JPEG, PNG, and TIFF images into high-performance WebP assets directly from the terminal or automated deployment scripts.
Installing the WebP Toolkit on Linux
The cwebp encoder is packaged within the official WebP repository across all modern Linux distributions:
Ubuntu, Debian, and Linux Mint
sudo apt update && sudo apt install -y webp
Fedora, RHEL, and AlmaLinux
sudo dnf install -y libwebp-tools
Arch Linux and Manjaro
sudo pacman -S libwebp
To verify that the utility is accessible in your environment path, check the version string:
cwebp -version
Converting a Single Image with Custom Compression Quality
The fundamental syntax for cwebp takes the target input image, applies your desired compression settings, and outputs the resulting .webp file via the -o parameter:
cwebp -q 80 input.jpg -o output.webp
The -q flag dictates the compression quality factor on a scale from 0 (smallest file size, lowest visual quality) to 100 (highest visual fidelity, largest file size). For most modern websites, a quality factor between 75 and 85 strikes the perfect compromise between sharp retina display rendering and minimal network transfer weights.
Lossless Compression for PNG Graphics and Transparency
If you are converting UI icons, company logos, screenshots, or illustrations that require absolute pixel preservation and intact alpha channel transparency, use the -lossless flag:
cwebp -lossless graphic.png -o graphic.webp
In lossless mode, WebP automatically preserves transparent backgrounds and sharp vector-like borders while substantially beating standard PNG compression algorithms.
Adjusting Compression Effort and Encoding Speed
For high-traffic production assets, you can instruct cwebp to expend additional CPU cycles analysing image blocks to achieve the ultimate minimal file size:
cwebp -q 82 -m 6 input.jpg -o optimised.webp
The -m flag controls compression effort from 0 (fastest processing, slightly larger file) to 6 (slowest processing, maximum algorithmic compression). Utilising -m 6 is highly recommended when pre-compiling static assets during continuous integration (CI/CD) website builds.
Batch Converting Entire Folders of Images
When migrating an existing media library, running individual commands for every photograph is impractical. You can batch convert all JPEG and PNG images within a directory using a concise Bash shell loop:
for img in *.jpg *.png; do
[ -f "$img" ] || continue
cwebp -q 80 "$img" -o "${img%.*}.webp"
done
This automated loop parses each photograph in the directory, applies an 80% quality compression profile, and generates a corresponding .webp file while safely preserving your original source images.