Switch system installed PHP versions within a local shell session.
Made for those working with multiple projects using a variety of PHP versions, this script will automatically switch to the correct PHP version after cd
ing into the project folder. It does not require sudo, so even works in IDEs.
- No sudo is required to switch versions (in contrast to update-alternatives), so it works in IDE terminals.
- No need to compile PHP (in contrast to phpenv), just use your package manager.
- No need for manual PHP installs (in contrast to php-versions), system installed php versions are detected.
A bunch of PHP versions installed named /usr/bin/php*
.
$ mkdir -p $HOME/opt/php-activate # or your place of choice
$ cd !$
# Downloads a tarball from a GitHub repository, extracts its contents, and then strips the first directory level:
$ curl -# -L https://github.com/svandragt/php-activate/tarball/main | tar -xz --strip 1
Add the following line to your shell rc script (replace 8.2 with your default php version):
# BASH shell:
source $HOME/opt/php-activate/php-activate.sh 8.2
export -f php
# ZSH Shell
source $HOME/opt/php-activate/php-activate.sh 8.2
typeset -f php >/dev/null
# Activate a non-existing version
$ php-activate 9
Error: No matching PHP version found. Use one of the following:
/usr/bin/php8.0
/usr/bin/php8.1
/usr/bin/php8.2
# System version
$ php --version
PHP 8.1.15 (cli) (built: Feb 7 2023 11:31:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.15, Copyright (c) Zend Technologies
with Zend OPcache v8.1.15, Copyright (c), by Zend Technologies
# Activate existing version
$ php-activate 8.2
Activated /usr/bin/php8.2
PHP 8.2.2 (cli) (built: Feb 7 2023 11:28:53) (NTS)
...
# Deactivate (or use a different shell)
$ php-activate
Deactivated /usr/bin/php8.2
PHP 8.1.15 (cli) (built: Feb 7 2023 11:31:36) (NTS)
...
Optionally provide a --quiet
or -q
argument to suppress successful output.
Add the following to ~/.direnvrc
:
# Usage: use php <version>
#
# Loads the specified php version into the environent
#
use_php() {
source $HOME/opt/php-activate/php-activate.sh $1
layout php
}
then add the following to the project's .envrc
:
use php 8.2
Now you can simply change into the project directory:
$ cd myproject
direnv: loading ~/dev/myproject/.envrc
direnv: using php 8.2
Activated /usr/bin/php8.2
PHP 8.2.2 (cli) (built: Feb 7 2023 11:28:53) (NTS)
direnv: export [...] ~PHP_PICKED
Please consider asdf with the PHP plugin.
Thanks to wilmoore for php-version which inspired this idea.