In this tutorial, we will learn how to install PHP 8 on Ubuntu 22.04 | 20.04 | 18.04. But before we really dive into the installation process, let’s understand a quick history and the benefits of using PHP 8.1.
PHP is a server-side language, which means that it is executed on the web server. This makes it ideal for creating dynamic web pages that can interact with databases and other resources. PHP is also a very popular language, with over 80 million active users.
You may also want to follow the below resources on LEMP and LAMP Stack:
New features in PHP 8.1
Here are some of the new key features witnessed in the PHP 8.1 release:
- Enums: Enums are a new type that allows you to define a set of named constants. This can be useful for representing things like days of the week, months of the year, or traffic lights.
- Fibers: Fibers are a new lightweight concurrency mechanism that allows you to run multiple tasks concurrently within the same PHP process. This can be useful for improving the performance of long-running tasks.
- Readonly Properties: Readonly properties are a new feature that allows you to define properties that cannot be modified after they have been initialized. This can be useful for improving the readability and maintainability of your code.
- Intersection Types: Intersection types are a new type that allows you to combine two or more types into a single type. This can be useful for representing things like objects that have both a string and an integer property.
- Never Return Type: The never return type is a new type that represents a function that never returns. This can be useful for indicating that a function will always throw an exception or terminate the program.
- Final Class Constants: Final class constants are a new feature that allows you to define constants that cannot be overridden by child classes. This can be useful for improving the readability and maintainability of your code.
- array_is_list(): The array_is_list() function is a new function that allows you to determine whether an array is a list. This can be useful for working with arrays that contain heterogeneous data.
- array unpacking for arrays with string keys: Array unpacking is a new feature that allows you to unpack the values of an array into variables. This can be useful for simplifying your code.
- cURL Improvements: The cURL extension has been improved in PHP 8.1. This includes support for DNS-over-HTTPS (DoH) and the ability to upload arbitrary string contents as a file.
Prerequisites
- Ubuntu 22.04 | 20.04 | 18.04 LTS x64 server instance
- Access to SSH
- HTTP and HTTPS Connectivity
- root privileges / sudo privileges
How to Install PHP 8 on Ubuntu
Step #1: Updating System Packages
To update the packages, run the following command:
sudo apt update && sudo apt upgrade
This command will first update the package list, and then it will upgrade all of the installed packages to the latest version.
If you want to update specific packages, you can use the apt-get upgrade command with the package names as arguments. For example, to update the PHP package, you would use the following command:
sudo apt-get upgrade php
To see a list of all of the available updates, you can use the apt-list --upgradable
command.
Note: It is important to update your system packages regularly to ensure that you are running the latest security patches and bug fixes. You should also update your packages before installing new software, as this will ensure that the new software is compatible with the latest versions of your existing packages.
Step #2: Add Ondřej Surý PPA repository
Here are the steps on how to add the Ondřej Surý PPA repository to your Ubuntu system:
Open a terminal window and run the following command to add the PPA repository:
sudo apt update sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y sudo add-apt-repository ppa:ondrej/php
Update your system’s package list again by running:
sudo apt update
Step #3: Install PHP 8.1
Open a terminal window and type the following command:
sudo apt install php8.1
Now, you will be prompted to enter your sudo password. Enter your password and hit the Enter or return button.
The command will start installing PHP 8.1. This may take a few minutes to complete.
Once the installation is complete, you can verify that PHP 8.1 is installed by running the following command:
php -v
You will receive output like this:
PHP 8.1.2 (cli) (built: Apr 7 2022 17:46:26) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.2, Copyright (c) Zend Technologies with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Step 4: Install PHP 8.1 Extensions
PHP extensions can be installed by running the command below:
sudo apt install php8.1-extension_name
We just need to replace the extension_name
with the extension that you want to install.
Here, we’re going to install all the necessary PHP extensions. To do that run:
sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-soap
php8.1-cli
: The php8.1-cli
package is a command-line interpreter for the PHP scripting language. It allows you to run PHP scripts from the command line, as well as perform other tasks such as debugging and testing PHP code.
php8.1-common
: The php8.1-common
package is a collection of common files that are used by other PHP packages. It includes things like documentation, examples, and configuration files.
php8.1-mysql
: The php8.1-mysql
package is a PHP extension that allows you to connect to MySQL databases. It is a required package if you want to use PHP to access MySQL databases.
php8.1-zip
: The php8.1-zip
is used for working with compressed files.
php8.1-gd
: The php8.1-gd
package is a PHP extension that allows you to create and manipulate images using the GD graphics library. It is a powerful tool that can be used for a variety of tasks, such as creating thumbnails, watermarking images, and generating charts and graphs.
php8.1-mbstring
: The php8.1-mbstring
package is a PHP extension that provides support for multibyte strings. This means that it can be used to work with strings that contain characters from multiple languages, such as Chinese, Japanese, and Arabic.
php8.1-curl
: The php8.1-curl
package is a PHP extension that allows you to make HTTP requests using the cURL library. This means that you can use PHP to access web resources, such as websites, APIs, and file downloads.
php8.1-xml
: The php8.1-xml
package is a PHP extension that provides support for XML processing. This means that it can be used to parse, manipulate, and generate XML documents.
php8.1-bcmath
: The php8.1-bcmath
is used when working with precision floats.
php8.1-soap
: The php8.1-soap
package is a PHP extension that allows you to communicate with SOAP (Simple Object Access Protocol) services. This means that you can use PHP to interact with web services that use SOAP, such as those provided by banks, government agencies, and other organizations.
Step #5: Testing the PHP Environment
Check the PHP version. You can do this by opening a command prompt and typing the following command:
php -v
This will output the version of PHP that is installed on your system.
Check the PHP configuration. You can do this by opening the php.ini file in a text editor. This file is located in the php
directory in your system’s root
directory.
Test a PHP script
You can create a simple PHP script and test it to make sure that PHP is working properly. Here is an example of a simple PHP script:
Save this script as hello.php
and then open it in a web browser. If PHP is working properly, you should see the text “Hello, world!” displayed in the browser.
Use a PHP testing tool. There are a number of PHP testing tools available that can help you to test your PHP environment. Some popular PHP testing tools include:
- PHPUnit: https://phpunit.de/
- Behat: https://behat.org/
- Codeception: https://codeception.com/
These tools can help you to test your PHP code for a variety of errors, including syntax errors, logic errors, and performance errors.
Learn PHP
- PHP for Beginners
- PHP for Beginners – Become a PHP Master – CMS Project
- Become a WordPress Developer: Unlocking Power With Code
Conclusion
Once you have completed the steps mentioned above, PHP 8.1 should be installed and configured on your Ubuntu 20.04 system. You can then start using PHP to create and run web applications.
Here are some additional resources that you may find helpful:
- PHP Documentation: https://www.php.net/manual/en/
- PHP Tutorial: https://www.phptutorial.net/
- PHP Stack Exchange: https://stackoverflow.com/questions/tagged/php
We hope you would find this tutorial on how to install PHP 8 on Ubuntu useful.