How to Convert Images to WebP Format in Linux?

In today's digital age, images play a vital role in websites and various digital projects. But, the larger the image size, the longer it takes to load, leading to a poor user experience. You probably want to know the solution to this. Fortunately, Google has developed the WebP image format, which offers superior compression and quality compared to traditional image formats like JPEG and PNG. In this article, we will delve into how you can convert images to the WebP format on Linux using the WebP tools. By converting your images to WebP format, you can significantly reduce their file size, which in turn boosts website performance and enhances user experience.

Step 1: Install WebP Tools

  • Open the terminal on your Linux machine.

  • Run the following command in the terminal.

sudo apt-get install webp

The following command will generate the output look like this −

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 89.6 kB of archives.
After this operation, 292 kB of additional disk space will be used.
Get:1 bionic/main amd64 webp amd64 0.6.1-2 [89.6 kB]
Fetched 89.6 kB in 1s (86.6 kB/s)       
Selecting previously unselected package webp.
(Reading database ... 146234 files and directories currently installed.)
Preparing to unpack .../webp_0.6.1-2_amd64.deb ...
Unpacking webp (0.6.1-2) ...
Setting up webp (0.6.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

The output begins with a message that the package lists are being read, followed by the dependency tree being built and state information being read. The output then shows that the webp package is the only new package that will be installed and that it will take up 89.6 kB of disk space.

Next, the package is downloaded from the Ubuntu archives, and then it is installed on the system. Finally, the output shows that the man-db package is being triggered, which updates the system's manual pages.

  • Wait for the installation process to complete. This may take a few minutes, it depends on your internet speed and system specifications.

  • Once the installation is complete, you can verify that the WebP tools are installed by running the following command 

cwebp -version

Here's the terminal output for the cwebp -version command 

WebP Encoder version 1.1.0

This output shows the version of the cwebp command, which is 1.1.0 in this case. The cwebp command is used to convert images to WebP format in Linux. You can use the ‘-version’ option with many other Linux commands available for displaying the version information.

Now that you have successfully installed the WebP tools on your Linux system, you can now convert images to WebP format using the cwebp command by following the subsequent steps 

Step 2: Convert Images to WebP Format

  • Open the terminal  Open the terminal by pressing Ctrl+Alt+T on your keyboard or by searching for "Terminal" in the Applications menu.

  • Navigate to the directory containing the image  Use the cd command to navigate to the directory that contains the image you want to convert. For example, if your image is located in the Pictures folder, you can navigate to it using the following command.

cd ~/Pictures
  • Convert the image to WebP format  Once you're in the directory containing the image, you can use the cwebp command to convert it to WebP format. Here's the basic syntax of the cwebp command.

cwebp [options] input_file -o output_file

Here is the terminal example of output for reference how it’ll look like −

$ cwebp -q 80 input.jpg -o output.webp
Input file size: 1024x768 pixels
Output file size: 120 KB
Output file: output.webp

Here's the detailed explanation of each part −

  • cwebp  This is the command to convert images to WebP format.

  • [options]  These are optional arguments that you can use to customize the output image. In this example, we'll use the -q option to specify the quality of the output image.

  • input_file  This is the name of the image file that you want to convert.

  • -o  For output files, this option specifies the names

  • output_file  This is the name you want to give the output file.

Here's an example of how to use the cwebp command to convert an image named my_image.jpg to WebP format −

cwebp -q 80 my_image.jpg -o my_image.webp

You may get the terminal output familiar to this −

Input file size: 654 KB
Output file size: 215 KB

In this example, we've used the -q option to set the quality of the output image to 80 (out of 100). We've also specified the input file (my_image.jpg) and output file (my_image.webp).

  • View the output file  Once the conversion is complete, you can view the output file using an image viewer or a web browser that supports the WebP format.

Step 3: Batch Conversion

Sometimes, you may have multiple images to convert to WebP format. Instead of converting each image one by one, you can use a loop to convert all the images in a directory at once. Here's how to do it:

  • Open a terminal window and navigate to the directory which contains the images you want to convert.

  • Use the following command to convert all the JPEG images in the directory.

for i in *.jpg; do cwebp -q 80 "$i" -o "${i%.jpg}.webp"; done

Let's break down this command −

  • for i in *.jpg is a loop that iterates over each file in the directory that has a .jpg extension. The variable $i is used to representing each file in the loop.

  • do cwebp -q 80 "$i" -o "${i%.jpg}.webp"; is the command that converts each image to WebP format. The -q flag sets the quality of the output image to 80 (you can change this value if you want). The $i variable is used to specify the input file name, and ${i%.jpg}.webp is used to determine the output file name. ${i%.jpg} removes the .jpg extension from the input file name, and .webp is added to the end to specify the output file format.

  • done ends the loop.

So, when you run this command, the loop will iterate over each JPEG file in the directory, convert it to WebP format using the cwebp command, and save the output file with a .webp extension in the same directory. The original JPEG file will not be modified or deleted.

Note − If you have images with different file extensions or want to convert all images in a directory, you can modify the command accordingly. For example, if you have PNG images, you can use for i in *.png instead of for i in *.jpg.


To summarize, converting your images to the WebP format can be a great way to improve the loading speed of your website without compromising image quality. This article has provided a step-by-step guide on how to convert images to WebP format in Linux using the cwebp command. We have also demonstrated how to convert multiple images at once by utilizing a loop. By following these instructions, you will be able to easily optimize your website's loading time by converting your images to the WebP format. This newfound knowledge will enable you to enhance the overall user experience of your website, making it more accessible to your audience.

Updated on: 27-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started