How To Upgrade PHP Version on Ubuntu 20.04 | Easy Setup

Posted on

How To Upgrade PHP Version on Ubuntu 20.04 | Easy Setup

How To Upgrade PHP Version on Ubuntu 20.04 | Easy Setup

In this guide, we will walk you through How To Upgrade PHP Version on Ubuntu 20.04. PHP is a widely-used open-source, interpreted, and object-oriented scripting language primarily executed on the server-side. Its strength lies in web development, making it ideal for creating dynamic web applications that run on servers and generate interactive web pages.

You can now proceed to the guide steps below on how to upgrade the PHP Version on Ubuntu 20.04.

Prerequisites

Before beginning, ensure you are logged in to your Ubuntu 20.04 server as a non-root user with sudo privileges. If you haven’t already, you can follow our guide on the Initial Server Setup with Ubuntu 20.04.

1. Check Current PHP Version on Ubuntu 20.04

Ubuntu 20.04 typically comes with PHP 7.4 pre-installed. To confirm the current PHP version on your system, execute the following command in your terminal:

php -v

The output should resemble the following:

Current PHP Version on Ubuntu 20.04

2. Remove PHP 7.4 From Ubuntu 20.04

Before upgrading, you need to remove the existing PHP version. Use the following command to purge PHP 7.4 and its related packages:

sudo apt purge php7.*

After uninstalling the packages, clean up any residual dependencies by running:

# sudo apt autoclean
# sudo apt autoremove

3. Add Ondřej Surý’s PPA Repository on Ubuntu 20.04

For Ubuntu 20.04 and 18.04, PHP 8 packages are primarily available through Ondřej Surý’s PPA (Personal Package Archive). Add this repository to your system by executing:

sudo add-apt-repository ppa:ondrej/php

Once the repository is successfully added, proceed to the next step.

4. Update PHP Version on Ubuntu 20.04

First, update your local package index to reflect the newly added repository:

sudo apt update

Next, install the desired PHP version (e.g., PHP 8.1) using the following command:

sudo apt install php8.1 -y

Verify the PHP version after installation:

php -v
Update PHP Version on Ubuntu 20.04

Install common PHP extensions to support various functionalities:

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl -y

Alternative Solutions for Upgrading PHP on Ubuntu 20.04

While the PPA method described above is a common and effective way to How To Upgrade PHP Version on Ubuntu 20.04, alternative approaches exist, each with its own set of advantages and disadvantages. Here are two alternative solutions:

1. Using Docker Containers

Docker offers a way to isolate your PHP environment, making upgrades and version management much easier. This method avoids directly modifying the system’s PHP installation.

Explanation:

Docker containers provide a virtualized environment where applications can run with all their dependencies packaged together. This means you can have multiple PHP versions running on the same server without conflicts.

Steps:

  1. Install Docker: If you don’t have Docker installed, follow the official Docker documentation to install it on your Ubuntu 20.04 system.

  2. Create a Dockerfile: Create a Dockerfile in a directory of your choice. This file will define the image you want to build. For example, to use PHP 8.1 with Apache, the Dockerfile might look like this:

    FROM php:8.1-apache
    
    # Install any necessary extensions
    RUN apt-get update && apt-get install -y 
        libfreetype6-dev 
        libjpeg62-turbo-dev 
        libpng-dev 
        && docker-php-ext-configure gd --with-freetype --with-jpeg 
        && docker-php-ext-install -j$(nproc) gd mysqli pdo_mysql zip
    
    # Copy your application code
    COPY . /var/www/html/
    
    # Set document root (if needed)
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </IfModule>
    
    <Directory /var/www/html>
        AllowOverride All
    </Directory>
    
  3. Build the Docker Image: Navigate to the directory containing your Dockerfile in the terminal and run:

    docker build -t my-php-app .
  4. Run the Docker Container: Once the image is built, run the container:

    docker run -d -p 80:80 -v $(pwd):/var/www/html my-php-app

    This command maps port 80 on your host machine to port 80 in the container and mounts your current directory (containing your PHP application code) to /var/www/html inside the container.

Advantages:

  • Isolation: Prevents conflicts with system-level PHP installations.
  • Flexibility: Easy to switch between different PHP versions.
  • Reproducibility: Ensures a consistent environment across different systems.

Disadvantages:

  • Overhead: Docker introduces some overhead compared to native installations.
  • Complexity: Requires familiarity with Docker concepts and commands.

2. Using PHP Version Manager (phpbrew)

phpbrew is a tool that allows you to build and manage multiple PHP versions in your home directory. It’s a user-specific installation, avoiding system-wide changes.

Explanation:

phpbrew installs PHP versions in your home directory (~/.phpbrew), keeping them separate from the system’s PHP installation. You can easily switch between different versions using the phpbrew switch command.

Steps:

  1. Install Dependencies: Ensure you have the necessary dependencies installed:

    sudo apt-get update
    sudo apt-get install -y autoconf build-essential libxslt-dev
    sudo apt-get install -y libcurl4-openssl-dev libreadline-dev libpcre3-dev
  2. Install phpbrew: Download and install phpbrew:

    curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
    chmod +x phpbrew
    sudo mv phpbrew /usr/local/bin/
    phpbrew init

    Follow the instructions provided by phpbrew init to add the necessary environment variables to your .bashrc or .zshrc file and restart your terminal.

  3. Install PHP: Use phpbrew to install the desired PHP version:

    phpbrew install 8.1 +default +mysql +mbstring +zip +gd

    This command installs PHP 8.1 with several common extensions. Adjust the extensions list according to your needs.

  4. Switch PHP Version: Switch to the newly installed PHP version:

    phpbrew switch 8.1
  5. Verify Installation: Verify that the correct PHP version is active:

    php -v

Advantages:

  • User-specific: Installs PHP in your home directory, avoiding system-wide changes.
  • Easy Switching: Easily switch between different PHP versions.
  • Customizable: Allows you to install PHP with specific extensions.

Disadvantages:

  • Compilation Time: Compiling PHP from source can take a significant amount of time.
  • Dependencies: Requires installing dependencies before installing PHP.

Conclusion

In conclusion, How To Upgrade PHP Version on Ubuntu 20.04 involves adding a trusted third-party repository like Ondřej Surý’s PPA, installing the desired PHP version, and updating configuration files if needed. This process ensures compatibility with newer applications and improves performance and security on your system. The alternative solutions, Docker and phpbrew, offer flexibility and isolation but come with their own set of trade-offs. Choose the method that best suits your needs and technical expertise.

Hope you enjoy it. Please subscribe to us on Facebook, YouTube, and X.

Also, you may like these articles:

How to install MariaDB on Ubuntu 20.04

Set up Apache virtual host on Ubuntu 20.04

How to install Anaconda on Ubuntu 18.04

Leave a Reply

Your email address will not be published. Required fields are marked *