Installing Node
There are many ways to install Node such as nvm
, nodeenv
, nodenv
. nvm is by far the most popular.
Installing NVM
NVM handles installing and managing node versions on the server.
Installation Script
Run the following command to download and install NVM version 0.39.7 into ~/.nvm/.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Source .bashrc
NVM adds itself to the $PATH by modifying ~/.bashrc
. Log out
and in again so that the `nvm` command works. You should be able to
check its version.
$ source ~/.nvm/nvm.sh
$ nvm --version
0.39.7
$
Installing Node
Now that nvm is installed, installing node is easy.
Choosing A Version
Go to https://nodejs.org/ and make a note of the version number of the latest LTS node version.

Install the specific node version with the command below. Replace 20.11.0 with the latest LTS version number.
nvm install 20.11.0
nvm install 16.20.2
Verify node is installed by checking the version.
$ node --version
20.11.0
Node is now installed in ~/.nvm/versions/node/v20.11.0/bin/node, where 20.11.0 is the version number installed.
Verify the install location with the `which` command.
$ which node
~/.nvm/versions/node/v20.11.0/bin/node
Testing Node
Let's try out a node script.
The Hello Node Script
Run a simple node test.
Create hello-world.js
#!/usr/bin/env node
console.log('Hello Node!')
Make It Executable
$ chmod +x hello-world.js
Execute It
$ ./hello-world.js
Hello Node!
$
Node & CGI
With node installed we can now generate dynamic web pages using the Common Gateway Interface (CGI).
The Hello CGI Script
Create the CGI script in the ~/cgi-bin/ folder.
~/cgi-bin/hello-world.js
#!/home/example_user/.nvm/versions/node/v20.11.0/bin/node
process.stdout.write('Content-Type: text/plain\n\n');
process.stdout.write('Hello World!\n);
Make It Executable
$ chmod +x ~/cgi-bin/hello-world.js
View The Webpage
http://example.com/cgi-bin/hello-world.js should show "Hello World".Explanation
Notice that the CGI script uses the full path to the node binary in the shebang (#!). That is because the server has a different PATH environment variable that doesn't include the path to node. Try adding console.log(process.env);
to the CGI script and looking at the PATH environment variable to see why #!/usr/bin/env node
won't work in the CGI script.
The first lines written out are the HTTP headers. In this basic example we set one header, the content type, to text/plain.
The HTTP body comes after two newlines. The body is set to "Hello World!"
There is a lot to HTTP and I wouldn't suggest trying to implement everything yourself in a CGI script. There are many packages available that will help to parse requests and generate responses.