This guide will help you install multiple versions of PHP on your macOS system using Homebrew. You can switch between these versions easily when needed. Additionally, we will set up aliases for convenient version switching.
To install multiple PHP versions, open your terminal and run the following commands:
brew install shivammathur/php/php@5.6
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0
Edit your ~/.zshrc
file to add paths and create aliases for each PHP version. You can do this by running the following commands in your terminal:
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
# PHP 5.6
echo 'export PATH="/opt/homebrew/opt/php@5.6/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@5.6/sbin:$PATH"' >> ~/.zshrc
# PHP 7.0
echo 'export PATH="/opt/homebrew/opt/php@7.0/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.0/sbin:$PATH"' >> ~/.zshrc
# PHP 7.1
echo 'export PATH="/opt/homebrew/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
# PHP 7.2
echo 'export PATH="/opt/homebrew/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc
# PHP 7.3
echo 'export PATH="/opt/homebrew/opt/php@7.3/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.3/sbin:$PATH"' >> ~/.zshrc
# PHP 7.4
echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
# PHP 8.0
echo 'export PATH="/opt/homebrew/opt/php@8.0/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@8.0/sbin:$PATH"' >> ~/.zshrc
# Define a function for easy PHP version switching
usephp() {
desired_version="$1"
case "$desired_version" in
"5.6" | "7.0" | "7.1" | "7.2" | "7.3" | "7.4" | "8.0")
export PATH="/opt/homebrew/opt/php@$desired_version/bin:$PATH"
export PATH="/opt/homebrew/opt/php@$desired_version/sbin:$PATH"
echo "Switched to PHP $desired_version"
php -v
;;
*)
echo "Unsupported PHP version: $desired_version"
;;
esac
}
# Create aliases for easy version switching
alias usephp56='usephp 5.6'
alias usephp70='usephp 7.0'
alias usephp71='usephp 7.1'
alias usephp72='usephp 7.2'
alias usephp73='usephp 7.3'
alias usephp74='usephp 7.4'
alias usephp80='usephp 8.0'
After adding the paths and aliases, make sure to source your ~/.zshrc
file to apply the changes:
source ~/.zshrc
You can now switch between PHP versions using the defined aliases. For example, to switch to PHP 7.4, you can use the command usephp74
or usephp 7.4
.
You can also use Homebrew to install various GUI applications. Here is a list of commonly used applications you can install:
brew install --cask \
google-chrome \
firefox \
iterm2 \
visual-studio-code \
sublime-text \
docker \
rectangle \
slack \
discord \
figma \
imageoptim \
maccy \
zoom \
skype
To install essential terminal applications, run the following commands:
brew install \
wget \
exa \
git \
nvm \
pnpm \
graphicsmagick \
commitzen \
vips
You can install some additional tools with the following commands:
- Warp:
brew install --cask warp
- Display Link:
brew install --cask displaylink
Feel free to modify the lists of applications based on your specific requirements.
That's it! You've successfully set up multiple PHP versions and installed various useful applications on your macOS system. Enjoy coding and using your new tools!