Come installare Node.js su Ubuntu 22.04
Installare Node.js su Ubuntu 22.04
Obiettivo:
Node.js è uno degli ambienti di runtime JavaScript asincroni event-driven più diffusi sul mercato. Negli ultimi anni si è diffuso al punto da diventare una piattaforma imprescindibile nel mondo dello sviluppo software. Per ulteriori informazioni sulle funzionalità della piattaforma Node.js, consulta la documentazione ufficiale.
Questa guida spiega come installare una piattaforma Node.js su una distribuzione Ubuntu 22.04 Linux.
Requisiti
Per seguire gli step indicati in questa guida è necessario disporre di un’istanza Compute di OVHcloud, di un VPS o di un server Bare Metal su cui è in esecuzione Ubuntu 22.04 e di conoscenze di base sull’uso della riga di comando. In questa guida abbiamo utilizzato un’istanza Public Cloud Compute. Se hai bisogno di aiuto per configurare un’istanza Public Cloud con Ubuntu 22.04, consulta questa guida: Creare e connettersi a un’istanza Public Cloud.
Per installare il gestore Node.js è necessario disporre dello strumento make.
Procedura
Questa guida ti mostra come installare una piattaforma Node.js, come utilizzarla e come passare da una versione all’altra.
Al momento della redazione di questa guida, l'ultima versione di Node.js è 16.15.x e l’ultima versione di GA è 18.1.x.
Un Node Manager per gestire tutti i nodi
Prima di installare Node.js è necessario installare uno strumento per gestire più installazioni di Node.js. I due strumenti più utilizzati sono nvm e n. In questo esempio, utilizziamo n.
Per installare n su Ubuntu, utilizza il seguente script bash:
curl -L https://bit.ly/n-install | bash
Il risultato dovrebbe essere simile a questo:
$ 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 Per ulteriori informazioni, vedere 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`. IMPORTANTE: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/ubuntu/.bashrc` before using n and Node.js. ===
Verifica l'installazione:
n --version
Il risultato dovrebbe essere simile a questo:
$ n --version v8.2.0
Installazione di Node.js con il comando n
Per installare la versione LTS di Node.js, digita il seguente comando:
n lts
Il risultato dovrebbe essere simile a questo:
$ n lts copying : node/16.15.0 installed : v16.15.0 (with npm 8.5.5)
Verifica la nuova installazione:
npm --version node --version
Il risultato dovrebbe essere simile a questo:
$ npm --version 8.5.5 $ node --version v16.15.0
Per installare l'ultima versione GA di Node.js scrivi:
n current
Il risultato dovrebbe essere simile a questo:
$ 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 dell'installazione di Node.js
Per testare l’installazione di Node.js, crea un'applicazione Hello World. Crea un file HelloWorld.js e incolla il seguente codice:
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}/`); });
Salva ed esegui.
node HelloWorld.js
Il risultato dovrebbe essere simile a questo:
$ node HelloWorld.js Server running at http://127.0.0.1:3000/
Per verificare l’esempio, esegui il seguente comando:
curl http://127.0.0.1:3000/
Il risultato dovrebbe essere simile a questo:
$ curl http://127.0.0.1:3000/ 👋 Hello World
Congratulazioni! Node.js è stato installato e configurato con successo su Ubuntu 22.04.