Skip to content

Instantly share code, notes, and snippets.

@Parables
Created July 16, 2022 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Parables/08b301c3e75765bd4bce788cf14e348b to your computer and use it in GitHub Desktop.
Save Parables/08b301c3e75765bd4bce788cf14e348b to your computer and use it in GitHub Desktop.
php version manager

Source: andrej 2021-02-26 13:38:56

  1. Have a directory for the user for shell scripts:
mkdir ~/bin
  1. Have this directory in the shell's PATH. Edit
~/.bash_profile
  1. and add:
[ -d ~/bin ] && { PATH="${HOME}/bin:${PATH}"; }
export PATH
  1. Then manually symlink the correct php into ~/bin/php. Or you can put the following script into
~/bin/usephp
#!/bin/sh
[ "$#" -lt "1" ] && {
  echo "Configure current shell to use a specific major PHP version.

    Usage: usephp 7"
  exit 0
}
binary="php${1}"
wanted="/usr/bin/${binary}"
[ ! -f "${wanted}" ] && {
  echo "'${binary}' not found. Using 'php'."
  wanted="/usr/bin/php"
}
ln -sf "${wanted}" "${HOME}/bin/php"
inUse=$(php -nr "echo phpversion();")
echo "Shell configured to use PHP ${inUse}."
  1. Make it executable
chmod u+x ~/bin/usephp
  1. and use it like so: use php version 8
usephp 8

Output:

php -v
PHP 8.0.2 (cli) (built: Feb  2 2021 18:26:02) ( NTS )

use php version 7

usephp 7

Output:

php -v
PHP 7.4.15 (cli) (built: Feb  2 2021 18:30:22) ( NTS )

Pages:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment