Change the PHP version the shell uses

 

Overview

The following describes how to change the PHP version in the shell.

Background

When you run a php command on the server, it uses the system version. However, since this can change over time, you can specify a version of PHP for your shell to use so that all PHP commands use that specific version instead.

Which shell user's file can I update?

The examples in this article use the bash shell. There are also other shells you can use at DreamHost. The profile file you update is different depending on the shell type you're using. For example, the currently supported default shells are Bash, Korn, Tcsh, and Zsh. Below are the profile files for each shell type:

  • .bash_profile
  • .kshrc
  • .tcshrc
  • .zshrc

If you’re using Tcsh, you must modify the lines as shown in the following before they can be added to the .tcshrc profile:

alias php /usr/local/php83/bin/php
set path=(/usr/local/php83/bin $path)

You can also modify this accordingly using the path to any of the PHP-CLI binaries.

How to change the PHP version

The following sections explain how to use the .bash_profile to change the PHP version.

Changing the PHP version under your shell user

To change the default version of PHP:

  1. Log into your user via SSH.
  2. Make sure you’re in your user's directory by running the following:
    [server]$ cd ~
  3. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. Add the following line to force PHP 8.3:
    export PATH=/usr/local/php83/bin:$PATH 
    
  4. Save and close the file and return to your shell.
  5. Run the following command to update the file:
    [server]$ . ~/.bash_profile

You can then run php -v to see the version currently in use:

[server]$ php -v

Using an alias to change the PHP version

You can also use an alias instead. Add the following to your .bash_profile file:

alias php='/usr/local/php83/bin/php'

This works the same as export PATH when added to your .bash_profile.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?