How to Install PHP-IMAGICK on Ubuntu 22.04 or 20.04

For developers and system administrators keen on enhancing their image manipulation capabilities within PHP, the Imagick PHP extension emerges as a pivotal tool. This extension, which interfaces with the renowned ImageMagick library, offers a dynamic approach to creating, editing, and managing images. If you’re operating on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa, this guide will elucidate the steps to install PHP-IMAGICK, ensuring you harness its full potential.

Key Advantages of Imagick:

  • Versatility: Supports over 200 image formats, catering to common and niche image types.
  • Functionality: From basic resizing and cropping to intricate transformations and compositing, Imagick is feature-rich.
  • Performance: Embry by the C-written ImageMagick suite, Imagick ensures optimal performance by interfacing directly with the OS.
  • Quality: Renowned for preserving image quality, regardless of the operations executed.
  • Adaptability: Stays relevant with compatibility for the latest PHP versions, including PHP 8.x.

Integrating Imagick with PHP not only amplifies the range of image operations you can perform but also streamlines batch processing of images. Unlike PHP’s native functions, Imagick can handle complex operations more efficiently. The integration means you can tap into ImageMagick’s features directly within PHP, eliminating the need for external program calls or shell commands, thus bolstering the security and efficiency of your applications.

As we delve deeper, we’ll outline the installation process of PHP-IMAGICK on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa. The subsequent sections will provide a comprehensive walkthrough, ensuring a seamless integration of Imagick into your PHP setup.

Section 1: Install PHP-IMAGICK Extension with Ubuntu Repository

In this part of our guide, we will walk through installing the Imagick PHP extension from the default Ubuntu repository. This installation method suits users not utilizing custom PPAs for their PHP setup.

Step 1: Refreshing and Upgrading Ubuntu Packages

To ensure a smooth installation, we start by updating our system. This step guarantees that all pre-existing packages on our Ubuntu machine are up-to-date. We can achieve this by executing the following command:

sudo apt update && sudo apt upgrade

The sudo apt update command fetches the package lists from the repositories and “updates” them to provide information on the latest versions of packages and their dependencies. Following this, sudo apt upgrade installs the latest versions of all the packages currently installed on the Ubuntu system from the sources enumerated in /etc/apt/sources.list file.

Step 2: Installing the PHP-IMAGICK Extension

Now, we’re ready to install the PHP-Imagick extension. As we’re using the official Ubuntu repository, the following command should suffice:

sudo apt install php-imagick imagick

The sudo apt install php-imagick imagick command downloads and installs the PHP-Imagick extension on your system. This command pulls the necessary packages from the Ubuntu repositories and sets up PHP-Imagick.

Step 3: Validating the Installation

Upon completing the installation, verifying if the extension is installed correctly is critical. To do this, we will inspect the list of PHP modules and search for Imagick. Here’s how to execute this step:

php -m | grep imagick

The php -m command lists all the PHP modules installed on your system. Piping this (|) to grep imagick filters the output to show only lines containing the string “imagick.” You should see an output with the current version if the installation was successful.

Section 2: Install PHP-IMAGICK with the PPA Method on Ubuntu

For those desiring to stay on the cutting edge, we will explore a second method of installing the Imagick PHP extension. This method utilizes the Ondřej Surý PPA, maintained by the PHP maintainer at Debian. This PPA is regularly updated with the latest stable versions of PHP, making it a superior choice for those seeking the newest features and updates.

Step 1: Preparing for PPA Installation

To start, we need to install some necessary packages that enable the addition of PPAs to our system. We can accomplish this by executing the following command:

sudo apt install software-properties-common -y

The software-properties-common package provides the necessary software to set up and manage software properties like PPAs. The -y flag automatically accepts the prompts that might arise during installation, streamlining the process.

Step 2: Importing the Ondřej Surý PPA

Once we’ve set up the required tools, we can add the Ondřej Surý PPA to our system’s software repository list. The following command will get this done:

sudo add-apt-repository ppa:ondrej/php -y

With this command, we’re telling our system to add (add-apt-repository) the Ondřej Surý PHP PPA (ppa:ondrej/php) to the list of software sources Ubuntu checks for packages.

Step 3: Syncing the New PPA

We must update our local package index to incorporate the newly added PPA. We can do this by executing an APT update:

sudo apt update

Step 4: Upgrading Dependencies

Before proceeding with the PHP-Imagick installation, we need to upgrade all existing packages and their dependencies to the latest versions from the new PPA:

sudo apt upgrade

Step 5: Installing PHP-IMAGICK Extension

With our system prepared, we can now install the PHP-Imagick extension. This can be done by issuing the following command:

sudo apt install php-imagick imagick

The php-imagick package can be installed with the corresponding PHP version for those requiring a specific PHP version. For instance, to install PHP-Imagick for PHP 7.4, use:

sudo apt install php7.4-imagick imagick

Similarly, for PHP 8.0, PHP 8.1, 8.2, and PHP 8.3, the commands would be:

sudo apt install php8.0-imagick imagick
sudo apt install php8.1-imagick imagick
sudo apt install php8.2-imagick imagick
sudo apt install php8.3-imagick imagick

Step 6: Validating the Installation

As before, once the installation is complete, it’s essential to verify that PHP-Imagick was installed successfully:

php -m | grep imagick

If all goes well, you should see imagick in the output; you are confirming that the PHP-Imagick extension is ready for your system. This completes the installation process using the Ondřej Surý PPA.

Wrapping Up

In this technical deep-dive, we’ve examined the critical steps and nuances in deploying the PHP-IMAGICK extension on Ubuntu, shedding light on its potential to revolutionize image processing within your PHP projects. We covered both installation methods – using Ubuntu’s official repositories and the renowned Ondřej Surý PPA – ensuring a versatile approach to suit diverse requirements. This exploration should equip you with the essential knowledge and competencies to install, validate, and leverage PHP-IMAGICK effectively in your Ubuntu Linux environment.

Your Mastodon Instance
Share to...