Как установить php 8 ubuntu
Перейти к содержимому

Как установить php 8 ubuntu

  • автор:

How To Install PHP 8.1 and Set Up a Local Development Environment on Ubuntu 22.04

How To Install PHP 8.1 and Set Up a Local Development Environment on Ubuntu 22.04

A previous version of this tutorial was written by alenaholligan.

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.

Deploy your PHP applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Prerequisites

To complete this tutorial, you will need a local or virtual machine with Ubuntu 22.04 installed and have administrative access and an internet connection to that machine. You can download this operating system via the Ubuntu releases page.

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. The article An Introduction to the Linux Terminal can get you better oriented with the terminal.

On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL , ALT , and T keys on your keyboard at the same time to open the Terminal application automatically.

Ubuntu terminal

Note: Ubuntu 22.04 ships with PHP 8.1 in its repositories. This means that if you attempt to install PHP without a specified version, it will use 8.1.

If you would like to use a different version of PHP on your Ubuntu 22.04 server, you can use the phpenv project to install and manage different versions.

Run the following commands to update your list of available packages, then then install PHP 8.1:

The —no-install-recommends flag will ensure that other packages like the Apache web server are not installed.

Check your PHP version information with the following command:

You will receive output like this:

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:

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:

    sudoapt-getinstall-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

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-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/8.1/apache2/php.ini . You can list all loaded PHP modules with the following command:

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 — Setting Up Composer for Dependency Management (Optional)

Libraries are a collection of code that can help you solve common problems without needing to write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage installing and updating these packages.

Although similar, Composer is not a package manager in the same sense as yum or apt . It deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor ) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, support a global project for convenience via the global command.

This idea is not new, and Composer is strongly inspired by Node’s npm and Ruby’s bundler .

  • You have a project that depends on several libraries.
  • Some of those libraries depend on other libraries.
  • Enables you to declare the libraries you depend on.
  • Finds out which versions of which packages can and need to be installed and installs them by downloading them into your project.
  • Enables you to update all your dependencies in one command.
  • Enables you to see the Basic Usage chapter for more details on declaring dependencies.

To install Composer, download the installer first with the following curl command:

Next, verify that the downloaded installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. To facilitate the verification step, you can use the following command to programmatically obtain the latest hash from the Composer page and store it in a shell variable:

To verify the obtained value, you can run:

Output
55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae

Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run:

You’ll see the following output:

Installer verified 

If the output says Installer corrupt , you’ll need to download the installation script again and double check that you’re using the correct hash. Then, repeat the verification process. When you have a verified installer, you can continue.

To install composer globally, use the following command which will download and install Composer as a system-wide command named composer , under /usr/local/bin :

You’ll receive output similar to this:

Output
All settings correct for using Composer Downloading. Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer

To test your installation, run:

You will receive output like the following:

Output
______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.3.5 2022-04-13 16:43:00 Usage: command [options] [arguments] Options: -h, --help Display help for the given command. When no command is given display help for the list command -q, --quiet Do not output any message -V, --version Display this application version --ansi|--no-ansi Force (or disable --no-ansi) ANSI output -n, --no-interaction Do not ask any interactive question --profile Display timing and memory usage information --no-plugins Whether to disable plugins. --no-scripts Skips the execution of all scripts defined in composer.json file. -d, --working-dir=WORKING-DIR If specified, use the given directory as working directory. --no-cache Prevent use of the cache -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug . . .

This verifies that Composer was successfully installed on your system and is available system-wide.

Note: If you prefer to have separate Composer executables for each project you host on your server, you can install it locally, on a per-project basis. This method is also useful when your system user doesn’t have permission to install software system-wide.

To do this, use the command php /tmp/composer-setup.php without any arguments. This command will generate a composer.phar file in your current directory, which you can run using php composer.phar .

Step 3 — Using Composer in a PHP Project

As a final step, you may optionally initialize your project with composer init . This will create the composer.json file that will manage your project dependencies. Initializing the project will also let you define project details such as Author and License, and use Composer’s autoload functionality. You can define dependencies now or add them later.

First, make a directory and change into it to contain your project files:

Now initialize your project:

Running this command will start the setup wizard. Details that you enter in the wizard can be updated later, so feel free to leave the defaults and just press ENTER . If you aren’t ready to install any dependencies, you can choose no . Enter in your details at each prompt:

Output
Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (/) [sammy/example-project]: sammy/project1 Description []: Author [n to skip]: Sammy Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: project License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no Add PSR-4 autoload mapping? Maps namespace "Sammy\Project1" to the entered relative path. [src/, n to skip]: n < "name": "sammy/project1", "type": "project", "authors": [ < "name": "Sammy", "email": "sammy@digitalocean.com" >], "require": <> > Do you confirm generation [yes]? yes

Before you confirm the generation, you will see a sample of the composer.json file that the wizard will create. If it all looks good, you can confirm the default of yes . If you need to start over, choose no .

The first time you define any dependency, Composer will create a vendor folder. All dependencies install into this vendor folder. Composer also creates a composer.lock file. This file specifies the exact version of each dependency and sub-dependency used in your project. This assures that any machine on which your program is run, will be using the exact same version of each packages.

Note: The vendor folder should never be committed to your version control system (VCS). The vendor folder only contains packages you have installed from other vendors. Those individual vendors will maintain their own code in their own version control systems. You should only be tracking the code you write. Instead of committing the vendor folder, you only need to commit your composer.json and composer.lock files. You can learn more about ignoring specific files in How To Use Git: A Reference Guide.

Now that you have PHP installed and a way to manage your project dependencies using Composer, you’re ready to test your environment.

Step 3 — Testing the PHP Environment

To test that your system is configured correctly for PHP, you can create and run a basic PHP script. Call this script hello.php :

This will open a blank file. Put the following text, which is valid PHP code, inside the file:

 echo 'Hello World!'; ?> 

Once you’ve added the text, save and close the file. You can do this by holding down the CTRL key and pressing the x key. Then choose y and press ENTER .

Now you can test to make sure that PHP processes your script correctly. Type php to tell PHP to process the file, followed by the name of the file:

If the PHP is processed properly, you will see only the characters within the quotes:

Output
Hello World!

PHP has successfully processed the script, meaning that your PHP environment is successfully installed and you’re ready to continue your programming journey.

Conclusion

At this point, you have a PHP 8.1 programming environment set up on your Ubuntu system and can begin a coding project.

Before you start coding, you may want to set up an Integrated Development Environment (IDE). While there are many IDEs to choose from, VS Code is a popular choice as it offers many powerful features such as a graphical interface, syntax highlighting, and debugging.

With your local machine ready for software development, you can continue to learn more about coding in PHP by following How To Work With Strings in PHP.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: How To Code in PHP

PHP banner image

PHP is a popular server scripting language known for creating dynamic and interactive web pages.

Browse Series: 9 articles

  • 1/9 How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 18.04
  • 2/9 How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 20.04
  • 3/9 How To Install PHP 8.1 and Set Up a Local Development Environment on Ubuntu 22.04

How to Install PHP 8.2 on Ubuntu 22.04

How to Install PHP 8.2 on Ubuntu 22.04 - TechvBlogs

In this article, You will learn How to Install PHP 8.2 in Ubuntu 22.04.

Suresh Ramani - Author - TechvBlogs

Suresh Ramani

SSD VPS Servers, Cloud Servers and Cloud Hosting - Vultr.com

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.

PHP is a general-purpose scripting language suitable for web development. Most websites out there are programmed using PHP language because it is:

  • Fast – PHP is fast because it runs on its own memory space. Also, PHP has a Zend engine that parses PHP code and turns it into opcodes which are then interpreted.
  • Flexible – PHP is flexible because it allows for almost all databases. MySQL is the de-facto database to connect to because it is open source.
  • Free and open source
  • PHP is forgiving – Why do we say PHP is forgiving? It is forgiving meaning its learning is not so hard and therefore suitable for almost all programmers starting out.
  • PHP supports major communication protocols i.e LDAP, IMAP, and POP3.

Below is a summarised list of the new key features witnessed in the PHP 8.2 release:

  • readonly Classes
  • Allow true , false , and null as Standalone Types
  • Disjunctive Normal Form (DNF) Types
  • Redact Sensitive Parameters in Back Traces
  • New mysqli_execute_query Function and mysqli::execute_query Method
  • Fetch enum Properties in const Expressions
  • Allow Constants in Traits
  • New Random Extension
  • and Many More

This tutorial will guide you through installing PHP 8.2 on Ubuntu and setting up a local programming environment via the command line.

Install PHP 8.2 on Ubuntu 22.04

1. Run system updates

The first thing to do in a new system is to update our repositories in order to make them up to date. Run upgrade command also.

sudo apt update && apt upgrade -y

2. Add Ondrej sury PPA repository

To run PHP 8.2 on Ubuntu 22.04, we need to add Ondrej sury PPA into our system. This is the maintainer of the PHP repository at the moment. This PPA is not currently checked so installing from it will not be guaranteed 100% results.

To add this PPA use the following command on our terminal.

sudo add-apt-repository ppa:ondrej/php

After installation is complete we need to update the repositories again for the changes to take effect.

sudo apt update

3. Install PHP 8.2 on Ubuntu 22.04

We should now be able to install PHP 8.2 on Ubuntu 22.04 Linux machine. The commands to run are as shared below:

sudo apt install php8.2 -y

Check for the currently active version of PHP with the following command:

php --version

4. Install PHP 8.2 Extensions

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.2-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.2-cli php8.2-common php8.2-fpm php8.2-mysql php8.2-zip php8.2-gd php8.2-mbstring php8.2-curl php8.2-xml php8.2-bcmath

This command will install the following modules:

  • php8.2-cli — command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
  • php8.2-common — documentation, examples, and common modules for PHP
  • php8.2-mysql — for working with MySQL databases
  • php8.2-zip — for working with compressed files
  • php8.2-gd — for working with images
  • php8.2-mbstring — used to manage non-ASCII strings
  • php8.2-curl — lets you make HTTP requests in PHP
  • php8.2-xml — for working with XML data
  • php8.2-bcmath — used when working with precision floats

PHP configurations related to Apache are stored in /etc/php/8.2/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.

Thank you for reading this blog.

If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

How to Install and Run PHP 8.x on Ubuntu 20.04

Hello dev, Today we are going to learn How to Install and Run PHP 8.x on Ubuntu 20.04. This tutorial will cover on installing and running PHP 8.x on ubuntu 20.04.

This example is focused on Ubuntu 22.04 on Install and Use PHP 8.x. In this article, we will implement a Steps to Install PHP 8.x on Ubuntu 22.04. you will learn How to Install and Configure PHP 8.x on Ubuntu 22.04?. you will learn Easy Way to Install PHP 8.x on Ubuntu 22.04. you will do the following things for Using Commands to Install PHP 8.x on Ubuntu 20.04.

Keyword Researcher — SEO Software / Finds Long Tail Keywords

A software application that helps you discover Long Tail Keywords. Organize your keywords. Import CSV Files from the Google Keyword tool.

Steps on How to Install and Run PHP 8.x on Ubuntu 20.04:

  • Step 1: Update System Dependencies
  • Step 2: Add PHP PPA
  • Step 3: Install PHP 8.x for Apache
  • Step 4: Install PHP 8.x Extensions
  • Step 5: Verify PHP Version
  • Step 6: Configure PHP 8.x
  • Step 7: Conclusion

Step 1: Update System Dependencies

First we are going to update system dependencies. Run the following command at a command prompt to update the latest packages or dependencies.

sudo apt update
sudo apt upgrade

Step 2: Add PHP PPA

Now in this step we are adding PHP PPA. Using following command to install PHP 8.1.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

How To Install PHP 8.0

PHP 8

PHP 8.0 has just been released! This new major version adds several significant features and improvements to the language like attributes, match expressions, Just-In-Time compilation (JIT), and much more!

Here’s a brief guide on how to install PHP 8.0 on Linux, Windows, and Mac OS X:

Ubuntu

PHP 8.0 and commonly-used extensions can be installed by adding Ondřej Surý’s PPA:

sudo apt install -y software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update 

You can then install PHP 8.0 with all common extensions and SAPIs like so:

sudo apt install php8.0 

Or you can specify individual packages like this instead:

sudo apt install php8.0-cli php8.0-fpm php8.0-bcmath php8.0-curl php8.0-gd php8.0-intl php8.0-mbstring php8.0-mysql php8.0-opcache php8.0-sqlite3 php8.0-xml php8.0-zip 

This method is supported on:

  • Ubuntu 16.04 (Xenial)
  • Ubuntu 18.04 (Bionic)
  • Ubuntu 20.01 (Focal)
  • Ubuntu 20.10 (Groovy)

Debian

Ondřej Surý also provides PHP 8.0 packages for Debian. Add the repository with these commands:

sudo apt install -y apt-transport-https lsb-release ca-certificates wget sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list sudo apt update 

And then either install PHP 8.0 with all the defaults like this:

sudo apt install php8.0 

Or list the exact packages you want instead:

sudo apt install php8.0-cli php8.0-fpm php8.0-bcmath php8.0-curl php8.0-gd php8.0-intl php8.0-mbstring php8.0-mysql php8.0-opcache php8.0-sqlite3 php8.0-xml php8.0-zip 

This method is supported on:

  • Debian 9 (Stretch)
  • Debian 10 (Buster)

CentOS / RHEL & Fedora

Remi’s RPM repository has RPMs for the latest PHP 8.0 builds.

RHEL/Centos 8 or Fedora modular:

dnf module reset php dnf module install php:remi-8.0 

RHEL/Centos 7 or Fedora:

yum-config-manager --enable remi-php80 yum update php\* 

Or for parallel installation as a software collection, if you prefer to keep existing versions:

yum install php80 

Refer to Remi’s blog post for additional information on how to install this depending on which OS version you’re running.

Mac OS X

Windows

Windows users can find the PHP 8.0 distributions on the windows.php.net website.

Docker

As of PHP 8.0’s release date, the php:8.0 image hasn’t been tagged yet; once it is, you can use that for your base images.

Docker is also a great way to tinker with PHP 8.0 in a local interactive shell without installing it first! Simply run this in your terminal:

docker run -it --rm php:8.0 

Was this helpful?

Support my open-source work via Github or follow me on Twitter for more blog posts and other interesting articles from around the web. I’d also love to hear your thoughts on this post — simply drop a comment below!

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *