phpenv
Manage multiple PHP versions
TLDR
Install a PHP version globally
Refresh shim files for all PHP binaries known to phpenv
List all installed PHP versions
Display the currently active PHP version
Set the global PHP version
Set the local PHP version, which overrides the global version
Unset the local PHP version
SYNOPSIS
phpenv <command> [ <args>... ]
PARAMETERS
versions
List all PHP versions installed by phpenv, indicating the currently active version.
version
Display the currently active PHP version for the current shell or project.
global <version>
Set the default (global) PHP version for all shells. This updates the `~/.phpenv/version` file.
local <version>
Set the PHP version for the current directory and its subdirectories. This creates a `.phpenv-version` file in the current directory.
shell <version>
Set the PHP version for the current shell session only. This overrides global and local settings temporarily.
rehash
Rebuild the phpenv shims. Run this command after installing a new PHP version or any PHP-specific executable (e.g., Composer, Pecl) to make them available.
root
Display the root directory where phpenv is installed (e.g., `~/.phpenv`).
prefix <version>
Display the installation path for a specific PHP version.
which <command>
Show the full path to the executable that phpenv will run for a given command (e.g., php, composer).
exec <command> [ <args>... ]
Execute a command against the currently selected PHP version, bypassing shims.
init
Configure the shell environment for phpenv. This command typically outputs shell initialization code that should be `eval`uated in your shell's startup files (e.g., `.bashrc`, `.zshrc`).
DESCRIPTION
phpenv is a lightweight command-line tool designed to manage multiple PHP versions on a single development machine. It provides a simple way to switch between different PHP versions globally, per-user, or on a per-project basis. Unlike system-wide installations or package managers, phpenv works by injecting itself into your PATH and using "shims" to intercept calls to PHP executables (like php, composer, pear, pecl). This allows phpenv to direct the command to the appropriate PHP version installed through it, without interfering with system-wide PHP installations. It's particularly useful for developers who need to test their applications against different PHP versions, ensuring compatibility and facilitating development across varied environments. phpenv itself does not install PHP; it often relies on companion tools like php-build or manual compilation for the actual PHP version installation.
CAVEATS
phpenv itself does not include functionality to install PHP versions; it relies on companion tools like php-build or manual compilation. Users must ensure phpenv is correctly initialized in their shell's startup files (e.g., `eval "$(phpenv init -)"`) for shims and version switching to work properly. It is primarily designed for local development environments and is generally not recommended for managing PHP on production servers. Care must be taken to avoid conflicts with existing system-wide PHP installations or other PATH configurations.
INSTALLATION LOCATION
phpenv is typically installed by cloning its Git repository into `~/.phpenv`. PHP versions are then installed into `~/.phpenv/versions/`.
HOW SHIMS WORK
phpenv functions by creating 'shims' for PHP executables (e.g., php, composer) in `~/.phpenv/shims`. When a command is executed, the shim intercepts it, and phpenv determines the correct PHP version to use based on global settings, local project files (`.phpenv-version`), or shell-specific environment variables, directing the command to the appropriate installed PHP binary.
HISTORY
phpenv emerged as a direct response to the growing need for flexible PHP version management in development environments, mirroring the success of tools like `rbenv` for Ruby and `pyenv` for Python. It was developed to provide a lightweight, non-intrusive method for developers to seamlessly switch between multiple PHP versions for different projects without conflicts or complex system configurations. Since its inception, its focus on simplicity and the use of shims has made it a popular choice for local development, enabling developers to easily test compatibility and manage diverse project requirements.