Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
29
Q:

How can I switch between different PHP versions on macOS using the command line?

Hi everyone,

Recently, I've been working on a project that requires me to switch between different PHP versions on my macOS. I'm wondering if anyone here can guide me on how to do that using the command line. I have already installed multiple PHP versions using Homebrew and now I need to be able to switch between them depending on the project I am working on.

I have already tried searching online for a solution, but I couldn't find a clear and concise guide. It would be really helpful if someone could provide step-by-step instructions on how to switch between PHP versions using the command line on macOS.

Thank you in advance for your help!

All Replies

kunde.emmanuelle

Hey there!

Switching between different PHP versions on macOS using the command line can be a bit tricky, but I'll share my personal experience with you.

Instead of using Homebrew, I prefer using a tool called phpbrew. Phpbrew allows you to easily install and manage multiple PHP versions on macOS. Here's what worked for me:

1. First, make sure you have some prerequisites installed. You'll need Xcode command line tools, autoconf, and some other dependencies. You can install them using Homebrew by running:


brew install autoconf automake libxml2 pkg-config


2. Next, you can install phpbrew by running the following commands in your terminal:

curl -L -O https://github.com/phpbrew/phpbrew/releases/latest/download/phpbrew.phar
chmod +x phpbrew.phar
sudo mv phpbrew.phar /usr/local/bin/phpbrew
phpbrew init


3. Once phpbrew is installed and initialized, you can proceed to install your desired PHP versions. For example, if you want to install PHP 7.4, you can use the following command:

phpbrew install 7.4 +default


4. After the installation completes, you can switch between PHP versions using:

phpbrew switch <version>

Replace `<version>` with the PHP version you want to switch to. For instance, `phpbrew switch 7.4` will switch to PHP 7.4.

5. To confirm that the switch was successful, you can run:

php -v

This should display the selected PHP version.

Remember to reload your shell configuration or open a new terminal window after switching versions for the changes to take effect.

I hope this alternative method using phpbrew works well for you! Let me know if you need any further assistance or have any questions.

qmorar

Hey all,

I've had my fair share of experience switching PHP versions on macOS, and I'd like to offer my approach. Instead of relying on Homebrew or phpbrew, I prefer using a solution called "PHPBrew" alongside a PHP version manager called "Fisherman".

Here are the steps I followed:

1. Install Fisherman if you haven't already. Fisherman is a package manager for the Fish shell. You can install it by running the following commands:


brew install fish
fish
curl -Lo ~/.config/fish/functions/fisher.fish --create-dirs git.io/fisher


2. Next, you'll need to install PHPBrew using Fisherman. Open your terminal and run these commands:

fish
fisher install phpbrew


3. After the installation, load PHPBrew by running:

phpbrew init


4. Now, we can install specific PHP versions using PHPBrew. For example, to install PHP 7.4, use:

phpbrew install 7.4 +default


5. Once the installation completes, you can switch between PHP versions using:

phpbrew switch <version>

Replace `<version>` with the desired PHP version. For instance, `phpbrew switch 7.4` will switch to PHP 7.4.

6. To ensure that the correct PHP version is being used, you need to set the PATH variable. Add the following line to your Fish shell configuration file (`~/.config/fish/config.fish`):

set -x PATH /Users/<username>/.phpbrew/php/php-<version>/bin $PATH

Replace `<username>` with your macOS username and `<version>` with the desired PHP version.

7. Finally, reload the Fish shell configuration or open a new terminal window for the changes to take effect.

Using this method, I've been able to effortlessly switch between different PHP versions on macOS. Let me know if you have any questions or need further guidance.

ali99

Hey there,

I had a similar issue with switching between PHP versions on macOS a while ago. I found a solution using the command line, so I'm happy to share my experience with you.

First, you'll need to install a package manager like Homebrew if you haven't already. Once it's installed, you can use it to manage different PHP versions.

To install a specific PHP version, you can run the following command in the terminal:


brew install php@<version>

Replace `<version>` with the desired PHP version, like `7.4` or `8.0`. After the installation is complete, you'll have that PHP version available on your system.

To ensure that you can switch between PHP versions easily, I suggest using the `php-switch` package from Homebrew. You can install it by running:

brew install php-switch


Once `php-switch` is installed, you can list the available PHP versions by running:

php-switch --list

This will display all the installed PHP versions on your system. Note the version number you want to switch to.

To switch to a specific PHP version, use the following command:

php-switch <version>

Replace `<version>` with the desired PHP version. For example, if you want to switch to version 7.4, you would run `php-switch 7.4`.

After switching versions, you may need to restart your terminal or reload your shell configuration for the changes to take effect. Once done, you should be able to use the new PHP version.

I hope this helps you out! Let me know if you have any questions or need further assistance.

New to LearnPHP.org Community?

Join the community