How do I change the PHP version my shell uses?

When you run the php command on the server, it uses the system version (which changes over time). You can specify a version of PHP for your shell to use. This way, all PHP commands will use that specific version.

Which shell user's file to 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 current 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 command as shown below before they can be added to the .tcshrc profile:

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

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

Changing your shell user's PHP version

To change the default version of PHP under your Shell user:

  1. Log into your user via SSH.
  2. Make sure you’re in your users directory by typing in ‘pwd’ to confirm:
    [server]$ pwd 
    /home/example_username
  3. Run the following command to open your .bash_profile file.
    [server]$ nano .bash_profile
  4. Add the following line to force PHP 5.6.
    export PATH=/usr/local/php56/bin:$PATH 
    
  5. When finished, press CTRL + O on your keyboard.
    • You are prompted to save the file.
  6. Click the Enter button to save.
  7. Finally, click Ctrl  + X to exit 'nano' and return to your shell.
  8. Update that file by running the following command:
    [server]$ . ~/.bash_profile
  9. Using 'nano' again, add the following to your .bashrc file:
    source .bash_profile

If you now run php -v you’ll see the new version being used.

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/php56/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 .