custom background image

Hoe installeer je Node.js op Ubuntu 22.04?


Installeer Node.js op Ubuntu 22.04

Doel

Node.js is één van de bekendste asynchrone event-gedreven runtimes voor JavaScript. Doordat het de afgelopen jaren veel is toegepast, kun je in development niet om het platform heen. Voor meer informatie en de mogelijkheden van het Node.js-platform verwijzen we naar de officiële documentatie.

In deze tutorial leert u hoe u een Node.js-platform installeert op een Ubuntu 22.04 Linux-distributie.

 

Vereisten

In deze tutorial wordt ervan uitgegaan dat u Ubuntu 22.04 heeft, dat bijvoorbeeld in een OVHcloud Compute-instance draait, en dat u enige basiskennis heeft van hoe u dit moet gebruiken. Als u geen actieve Ubuntu 22.04 heeft, volg dan de handleiding om een OVHcloud Compute Instance te gebruiken.

Om de Node.js-manager te installeren, moet u de make tool installeren.

 

Instructies

In deze tutorial zult u eerst een Node.js-platform installeren. Daarna zult u het gebruiken en uiteindelijk leert u hoe u tussen verschillende geïnstalleerde versies kunt schakelen.

Bij het schrijven van deze tutorial is de laatste LTS-versie van Node.js 16.15.x en de laatste GA-versie 18.1.x.

 

Een node manager om alle te regeren

Alvorens Node.js te installeren, moet u een tool installeren om meerdere Node.js-installaties te managen. De twee meest gebruikte tools zijn nvm en n. In deze tutorial zult u n gebruiken.

Om n op Ubuntu te installeren gaat het eenvoudigst door dit bash-script te gebruiken:

curl -L https://bit.ly/n-install | bash

De output zou als volgt moeten zijn:

$ curl -L https://bit.ly/n-install | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   161  100   161    0     0    718      0 --:--:-- --:--:-- --:--:--   718
100 43367  100 43367    0     0  83076      0 --:--:-- --:--:-- --:--:--  225k
===
You are ABOUT TO INSTALL n, the Node.js VERSION MANAGER, in:

  /home/ubuntu/n

Afterwards, THE FOLLOWING Node.js VERSION(S) WILL BE INSTALLED,
and the first one listed will be made active; 
  'lts' refers to the LTS (long-term support) version, 
  'latest' to the latest available version.
  '-' means that *no* version will be installed:

  lts
 
If your shell is bash, bsh, zsh, fish, or pwsh (PowerShell), the relevant 
initialization file will be modified in order to:
 - export environment variable $N_PREFIX.
 - ensure that $N_PREFIX/bin is in the $PATH

Your shell, bash, IS supported, and the following initialization
file will be updated:

   /home/ubuntu/.bashrc

For more information, see https://bit.ly/n-install-repo
===
CONTINUE (y/N)? y
-- Cloning https://github.com/tj/n to '/home/ubuntu/n/n/.repo'...
-- Running local n installation to '/home/ubuntu/n/bin'...
-- Shell initialization file '/home/ubuntu/.bashrc' updated.
-- Installing helper scripts in '/home/ubuntu/n/bin'...
-- Installing the requested Node.js version(s)...
   1 of 1: lts...
  installing : node-v16.15.0
       mkdir : /home/ubuntu/n/n/versions/node/16.15.0
       fetch : https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz
     copying : node/16.15.0
   installed : v16.15.0 (with npm 8.5.5)
=== n successfully installed.
  The active Node.js version is: v16.15.0

  Run `n -h` for help.
  To update n later, run `n-update`.
  To uninstall, run `n-uninstall`.

  IMPORTANT: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/ubuntu/.bashrc`
             before using n and Node.js.
===

Daarna kunt u uw nieuwe installatie testen:

n --version

De output zou als volgt moeten zijn:

$ n --version
v8.2.0

 

Node.js met n installeren

Typ het volgende commando om de LTS-versie van Node.js te installeren:

n lts

De output zou als volgt moeten zijn:

$ n lts
   copying : node/16.15.0
   installed : v16.15.0 (with npm 8.5.5)

Daarna kunt u uw nieuwe installatie testen:

npm --version node --version

De output zou als volgt moeten zijn:

$ npm --version
8.5.5

$ node --version
v16.15.0

Als u de nieuwste GA-versie van Node.js wilt installeren:

n current

De output zou als volgt moeten zijn:

$ n current
  installing : node-v18.1.0
  mkdir : /home/ubuntu/n/n/versions/node/18.1.0
  fetch : https://nodejs.org/dist/v18.1.0/node-v18.1.0-linux-x64.tar.xz
  copying : node/18.1.0
  installed : v18.1.0 (with npm 8.8.0)

 

Test de Node.js-installatie

Om uw Node.js-installatie te testen, kunt u een applicatie als Hello World schrijven. Maak een HelloWorld.js-bestand aan en plak daarin de volgende code:

const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

Sla het bestand op en voer het uit:

node HelloWorld.js

De output zou als volgt moeten zijn:

$ node HelloWorld.js
Server running at http://127.0.0.1:3000/

Om uw probeersel te testen, voert u het volgende commando uit:

curl http://127.0.0.1:3000/

De output zou als volgt moeten zijn:

$ curl http://127.0.0.1:3000/
👋 Hello World

Dat is alles. U heeft met succes Node.js op Ubuntu 22.04 geïnstalleerd en geconfigureerd.

 

Verdere stappen

Bekijk de producten voor Public Cloud-instances op OVHcloud.