Works with any Ubuntu version and other Ubuntu-based versions. Ubuntu 14.04, Ubuntu 16.04, Ubuntu 18.04 or even Ubuntu 19.10.
Before you begin
PHP has different versions and distributions that you can use. The current PHP official website supports the oldest version (PHP 7.2) to PHP 7.3 and the latest version (PHP 7.4).
This blog provides instructions for PHP 7.4, PHP 7.2 (the default in Ubuntu 18.04), and the default PHP version (PHP 7) in the Ubuntu 16.04 repository.
It is recommended that you install PHP 7.3 because it is stable and has many improvements and new features. If you are still using PHP 7.1, you will definitely need to upgrade as soon as possible, as its security support will end at the end of 2019.
Install PHP 7.4 on Ubuntu 18.04 or 16.04
PHP 7.4 is the latest version of PHP with many improvements. These instructions are very similar to PHP 7.3.
Update Ubuntu
First, update your Ubuntu server:
apt-get update && apt-get upgrade
Add PHP Storage Repository
To install PHP 7.4, you need to use a third-party repository. We will use the repository of Ondřej Surý that we have used again before.
First, make sure you have the following packages installed so you can add the repository:
apt-get install software-properties-common
Next, add the PHP repository from Ondřej:
add-apt-repository ppa:ondrej/php
Finally, update the installation Package:
apt-get update
Install PHP 7.4
After adding the repository, you can install PHP 7.4 using the following command:
apt-get install php7.4
This command will install additional packages:
libapache2-mod-php7.4
libaprutil1-dbd-sqlite3
php7.4-cli
php7.4-common
php7.4-json
php7.4-opcache
php7.4-readline
Wait...
That’s it . To check if PHP 7.4 is installed on your server, run the following command:
php -v
Install PHP 7.4 Modules
Depending on your application, you may need additional packages and modules. The most commonly used modules can be installed using the following command:
apt-get install php-pear php7.4-curl php7.4-dev php7.4-gd php7.4-mbstring php7.4-zip php7.4-mysql php7.4-xml
Just like that, you can start using PHP on your Ubuntu server.
Installing PHP 7.2 on Ubuntu 16.04
PHP 7.2 is the stable version of PHP with many new features, improvements, and bug fixes. If you want a better and faster website/app, you should definitely use it.
Update Ubuntu
Of course, as always, update Ubuntu first:
apt-get update && apt-get upgrade
Add PHP Storage Repositories
You can use third-party repositories to install the latest version of PHP. We will use Ondřej Surý’s repository.
First, make sure you have the following packages installed so you can add the repository:
apt-get install software-properties-common
Next, add the PHP repository from Ondřej:
add-apt-repository ppa:ondrej/php
Finally, update the installation Package:
apt-get update
Install PHP 7.2
After adding the repository, you can install PHP 7.2 using the following command:
apt-get install php7.2
This command will install additional packages:
ibapache2-mod-php7.2
libargon2-0
libsodium23
libssl1.1
php7.2-cli
php -v
Install PHP 7.2 ModuleAccording to your application, You may need other packages and modules. The most commonly used modules can be installed using the following command:
apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml
Just like that, you can start using PHP on your Ubuntu server.
Install PHP 7.2 on Ubuntu 18.04
Update UbuntuAgain, before doing anything, you should update your server:
apt-get update && apt-get upgrade
Install PHP 7.2Next, on Ubuntu 18.04 To install PHP 7.2, just run the following command:
apt-get install php
This command will install PHP 7.2 along with some other dependencies.
To verify that PHP is installed, run the following command:
php -v
You should get a response similar to the following:
PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )
That’s it. PHP 7.2 is installed on Ubuntu 18.04 server.
Install PHP 7.2 ModulesThese are the most common PHP 7.2 modules that are often used by php applications. You may need more or less, so check the requirements for the software you plan to use:
apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php
To check all available PHP modules in Ubuntu, run:
apt-cache search --names-only ^php
Install PHP 7.3 on Ubuntu 18.04 or 16.04 PHP 7.3是一个稳定的版本,可以在服务器上安全使用。 更新Ubuntu 首先,更新你的Ubuntu服务器: 添加PHP存储库 要安装PHP 7.3,您需要使用第三方存储库。我们将使用以前使用的OndřejSurý的存储库。 首先,请确保已安装以下软件包,以便可以添加存储库: 接下来,从Ondřej添加PHP存储库: 最后,更新安装包 安装PHP 7.3 添加存储库后,可以使用以下命令安装PHP 7.3: 此命令将安装其他软件包: libapache2-mod-php7.3 libaprutil1-dbd-sqlite3 php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline 等等其他…… 安装PHP 7.3模块 就这样。现在,你可以在Ubuntu服务器上开始使用PHP。 如何更改你使用的PHP版本 如果你在Ubuntu服务器上安装了多个PHP版本,则可以更改默认版本。 要将PHP 7.2设置为默认值,请运行: 要将PHP 7.3设置为默认值,请运行: 要将PHP 7.4设置为默认值,请运行: The above is the detailed content of How to install php7.2, php7.3, php7.4 in Ubuntu? (Detailed explanation of method). For more information, please follow other related articles on the PHP Chinese website!apt-get update && apt-get upgrade
apt-get install software-properties-common
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php7.3
就是这样。要检查服务器上是否安装了PHP 7.3,请运行以下命令:php -v
根据你的应用程序,你可能需要其他软件包和模块。可以使用以下命令安装最常用的模块:apt-get install php-pear php7.3-curl php7.3-dev php7.3-gd php7.3-mbstring php7.3-zip php7.3-mysql php7.3-xml
update-alternatives --set php /usr/bin/php7.2
update-alternatives --set php /usr/bin/php7.3
update-alternatives --set php /usr/bin/php7.4