local dev

Local PHP and NodeJS Version Control

by

in

When working on projects with your local computer, it’s essential to be able to test new versions of the languages you’re using. Browser JavaScript is fairly straightforward: use the browser, make sure it’s updated, etc.

However, with server-side languages (like PHP, NodeJS, Python, Go, etc.), there’s a little more nuance. Some systems come with languages installed on them (I tend to work on a Mac), but they usually come with a single version, and there isn’t a UI or straightforward way to update them.

We tend to have vertualized environments for this kind of thing, but if you’re looking to install and easily switch between local versions versions of a language you’re running, these two repositories might be helpful.

Note: I’m on a Mac, so these are what I use.

PHP

I’ve been using Homebrew PHP for a bit now and really like how easy it is to get, use, and upgrade different PHP versions.

There are installation instructions in the Github repo, as well as commands for downloading, upgrading, and changing PHP versions. Since those commands are hard to remember, I’ve created a few bash functions and put them in my .bash_aliases file:

# PHP version management for Homebrew PHP
# https://github.com/shivammathur/homebrew-php
phpget () { brew install shivammathur/php/php@"$1" }
phpswitch () { brew link --overwrite --force shivammathur/php/php@"$1" }
phpupgrade () { brew upgrade shivammathur/php/php@"$1" }

Now if I want to switch to a different PHP version, I just type phpswitch 8.2 into my terminal and I’m up and running with 8.2.x.

This has made testing new PHP features and staying up to date with package requirements easy.

NodeJS

Similarly, using Node Version Manager has been a breeze for switching between versions of NodeJS.

Installation instructions and commands are available in the repo.

Here are some examples of commands you can use (from the repo):

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)

$ node -v
v16.9.1

$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)

$ nvm ls
       v10.23.0
       v12.20.0
       v14.18.1
->     v16.15.0
         system
default -> 12.20.0 (-> v12.20.0)
node -> stable (-> v16.15.0) (default)
stable -> 16.15 (-> v16.15.0) (default)

$ nvm ls-remote
(will output all the versions available to install... too many to list here 🤓)

Easy as that… and pie 🥧 😋.


Photo by Ilya Pavlov on Unsplash