Creating the LMS environment

icon picker
Installing PHP 8.1

Introduction

PHP is a popular server scripting language known for creating dynamic and interactive web pages. Getting up and running with your language of choice is the first step in learning to program.
This tutorial will guide you through installing PHP 8.1 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.

Prerequisites

To complete this tutorial, you will need a local or virtual machine with Ubuntu 18.04 and above installed and have administrative access and an internet connection to that machine.

Step 1 — Setting Up PHP 8.1

You’ll be completing your installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.
The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things.
Ubuntu terminal
Run the following command to update apt-get itself, which ensures that you have access to the latest versions of anything you want to install:
sudo apt-get update
Next, install software-properties-common, which adds management for additional software sources:
sudo apt -y install software-properties-common
The -y flag will automatically agree to the installation. Without that, you would receive a prompt in your terminal window for each installation.
Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:
sudo add-apt-repository ppa:ondrej/php
Finally, you update apt-get again so your package manager can see the newly listed packages:
sudo apt-get update
Now you’re ready to install PHP 7.4 using the following command:
sudo apt -y install php8.1
Check the version installed:
php -v
You will receive something similar to the following:
Output
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
Besides PHP itself, you will likely want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you wish to install:
sudo apt-get install php8.1-PACKAGE_NAME
You can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want to install:
sudo apt-get install -y php8.3-intl php8.3-xmlrpc php8.3-mysql php8.3-zip php8.3-gd php8.3-mbstring php8.3-curl php8.3-xml php8.3-bcmath
This command will install the following modules:
php8.1-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
php8.1-json - for working with JSON data
php8.1-common - documentation, examples, and common modules for PHP
php8.1-mysql - for working with MySQL databases
php8.1-zip - for working with compressed files
php8.1-gd - for working with images
php8.1-mbstring - used to manage non-ASCII strings
php8.1-curl - lets you make HTTP requests in PHP
php8.1-xml - for working with XML data
php8.1-bcmath - used when working with precision floats
PHP configurations related to Apache are stored in /etc/php/7.4/apache2/php.ini. You can list all loaded PHP modules with the following command:
php -m
You have installed PHP and verified the version you have running. You also installed any required PHP modules and were able to list the modules that you have loaded.
You could start using PHP right now, but you will likely want to use various libraries to build PHP applications quickly. Before you test your PHP environment, first set up a dependency manager for your projects.

Step 2 — Change environment variables


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.