Installing PHP on Ubuntu

Standard

How to install PHP 7 on Ubuntu 16.04

Currently, as of January 2018, the default PHP release in the Ubuntu 16.04 repositories is PHP 7.0. We’ll show you how to install it using Ubuntu’s repository.

Update Ubuntu

First, before you do anything else, you should update your Ubuntu server:

apt-get update && apt-get upgrade

Install PHP

Next, to install PHP, just run the following command:

apt-get install php

This command will install PHP 7.0, as well as some other dependencies:

  • php-common
  • php7.0
  • php7.0-cli
  • php7.0-common
  • php7.0-fpm
  • php7.0-json
  • php7.0-opcache
  • php7.0-readline

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

php -v

And that’s it. PHP is installed on your Ubuntu server.

Install PHP 7.0 modules

You may need some additional packages and PHP modules in order for PHP to work with your applications. You can install the most commonly needed modules with:

apt-get install php-pear php7.0-dev php7.0-zip php7.0-curl php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-xml libapache2-mod-php7.0

Depending on how and what you’re going to use, you may need additional PHP modules and packages. To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

You can tweak the command to only show ^php7.0- packages etc.

If you want to use the latest PHP version, follow the next instructions instead.

How to Install PHP 7.2 on Ubuntu 16.04

PHP 7.2 is the latest stable version of PHP and has many new features, improvements, and bug fixes. You should definitely use it if you want a better, faster website/application.

Update Ubuntu

Of course, as always, first update Ubuntu:

apt-get update && apt-get upgrade

Add the PHP repository

You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 7.2

After you’ve added the repository, you can install PHP 7.2 with the following command:

apt-get install php7.2

This command will install additional packages:

  • libapache2-mod-php7.2
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.2-cli
  • php7.2-common
  • php7.2-json
  • php7.2-opcache
  • php7.2-readline

And that’s it. To check if PHP 7.2 is installed on your server, run the following command:

php -v

Install PHP 7.2 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with 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

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to Install PHP 7.2 on Ubuntu 18.04

PHP 7.2 is included by default in Ubuntu’s repositories since version 18.04. So the instructions are pretty similar to PHP 7 for 16.04.

Update Ubuntu

Again, before doing anything, you should update your server:

apt-get update && apt-get upgrade

Install PHP 7.2

Next, to install PHP 7.2 on Ubuntu 18.04, just run the following command:

apt-get install php

This command will install PHP 7.2, as well as some other dependencies.

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )

And that’s it. PHP 7.2 is installed on your Ubuntu 18.04 server.

Install PHP 7.2 modules

These are the most common PHP 7.2 modules often used by php applications. You may need more or less, so check the requirements of the software you’re planning 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 the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

How to install PHP 7.3 on Ubuntu 18.04 or 16.04

PHP 7.3 is still under development, but a stable release should be available at the end of 2018. You can install it now if you want to test things out.

Update Ubuntu

First, update your Ubuntu server:

apt-get update && apt-get upgrade

Add the PHP repository

To install PHP 7.3 you’ll need to use a third-party repository. We’ll use the repository by Ondřej Surý that we previously used.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 7.3

After you’ve added the repository, you can install PHP 7.3 with the following command:

apt-get install php7.3

This command will install additional packages:

  • libapache2-mod-php7.3
  • libaprutil1-dbd-sqlite3
  • php7.3-cli
  • php7.3-common
  • php7.3-json
  • php7.3-opcache
  • php7.3-readline
  • …and others.

And that’s it. To check if PHP 7.3 is installed on your server, run the following command:

php -v

Install PHP 7.3 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

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

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to change the PHP version you’re using

If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.

To set PHP 7.0 as the default, run:

update-alternatives --set php /usr/bin/php7.0

To set PHP 7.2 as the default, run:

update-alternatives --set php /usr/bin/php7.2

To set PHP 7.3 as the default, run:

update-alternatives --set php /usr/bin/php7.3

If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 7.2 with the following command:

a2enmod php7.2

And then restart Apache for the changes to take effect:

systemctl restart apache2

How to upgrade to PHP 7.2 or 7.3 on Ubuntu

If you’re already using an older version of PHP with some of your applications, you can upgrade by:

  1. Backup everything.
  2. Install the newest PHP and required modules.
  3. Change the default version you’re using.
  4. (Optionally) Remove the older PHP
  5. (Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.

Speed up PHP by using an opcode cache

You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.

If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:

pecl install apcu

There are also other ways you can install APCu, including using a package.

To start using APCu, you should run the following command for PHP 7.2:

echo "extension=apcu.so" | tee -a /etc/php/7.2/mods-available/cache.ini

Or this command for PHP 7.3:

echo "extension=apcu.so" | tee -a /etc/php/7.3/mods-available/cache.ini

And the following command for PHP 7.0:

echo "extension=apcu.so" | tee -a /etc/php/7.0/mods-available/cache.ini

If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.

For PHP 7.2:

ln -s /etc/php/7.2/mods-available/cache.ini /etc/php/7.2/apache2/conf.d/30-cache.ini

For PHP 7.3:

ln -s /etc/php/7.3/mods-available/cache.ini /etc/php/7.3/apache2/conf.d/30-cache.ini

For PHP 7.0:

ln -s /etc/php/7.0/mods-available/cache.ini /etc/php/7.0/apache2/conf.d/30-cache.ini

And finally, reload Apache for the changes to take effect:

systemctl restart apache2

To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.

That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.