How to install PHP 8 from source on Debian
Tags: php, programming
Published: 2021-03-24 18:17:08

I've seen a lot of tutorials on installing PHP 8 on Ubuntu, Debian, but none of them seem to work anymore maybe they do work on Ubuntu itself but other Debian distros I've tried didn't work. So I ended up compiling it myself and that's what I'm going to show you in this guide.

## Setting up the environment
Make sure you have `gcc` installed which comes pre-installed in almost any Linux distro out there. The next thing is to install build tools.

```sh
sudo apt-get install build-essential autoconf re2c bison
```

This may not be everything we are going to need but if you find an error that says `package 'package-name' not found` just install it by running `apt-get install lib<package-name>-dev`. If you're unsure of the package name type `lib` followed by incomplete package name, then hit tab to autocomplete.

## Getting PHP
Go to [php.net](https://www.php.net/downloads) and download the latest release. Once downloaded extract by

```sh
tar -xzf php-8.0.3.tar.gz
```

```sh
cd php-8.0.3
```

## Configure

The next step is to configure, tell gcc which extension we want to include

```sh
./configure --with-openssl\
--enable-mbstring\
--with-curl\
--enable-calendar\
--enable-bcmath\
--enable-exif\
--enable-gd\
--enable-ftp\
--enable-pcntl\
--enable-sockets\
--with-sodium\
--with-zip\
--with-pear\
--enable-mysqlnd\
--with-pdo-mysql\
--with-pgsql=/usr/bin/pg_config\
--with-pdo-pgsql=/usr/bin/pg_config\
--with-config-file-scan-dir=/etc/php/8.0\
--with-config-file-path=/etc/php/8.0/cli/php.ini\
--with-zlib
```

If all goes well you should see this 

```sh
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
```
If not, it's probably because you have a library missing which you can install by running `apt-get install lib<package-name>-dev`


## Building

Sometimes you may want to check how many cores your CPU has

```sh
nproc
```
And then you can use that number to make builds faster with the `-j` flag.

```sh
make -j2
```

This will use 2 cores to build. Be patient and after you see `build complete` on the console install the binaries in `--prefix` destination (provided during configuration)

```sh
sudo make install
```

After it's done confirm by

```sh
php -v
PHP 8.0.3 (cli) (built: Mar  24 2021 15:37:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
```

Almost done, now depending on the environment copy the php.ini-production or php.ini-development to the `--with-config-file-path` you provided during configuration. 

Now you have the latest version of PHP. I'll write another article on how to upgrade but in the meantime Happy Coding!