DEV Community

Cover image for Installing and using multiple PHP versions with Debian/Ubuntu
Rafael Beraldo
Rafael Beraldo

Posted on

Installing and using multiple PHP versions with Debian/Ubuntu

Ever wondered if there is a nvm-like for PHP?

Not exactly, but we can have multiple PHP versions in our system and switch between them easily, let's see.

Installing multiple versions

The default apt repository only have one PHP version availbale, usually the last stable version. To install others we have to add Ondřej's repository.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Enter fullscreen mode Exit fullscreen mode

We can now install individual versions.

PHP 7.4

# add any other extension you may need
sudo apt install -y php7.4-cli php7.4-common php7.4-mysql php7.4-zip php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath php8.1-sqlite3
Enter fullscreen mode Exit fullscreen mode

PHP 8.1

# add any other extension you may need
sudo apt install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-sqlite3
Enter fullscreen mode Exit fullscreen mode

Switching between versions

Now use the following command to switch between versions.

sudo update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode

Other distros

Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

If you are a macOS user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!