Ricerca nel sito web

Come installare e utilizzare NVM (Node Version Manager) su Ubuntu 22.04


Su questa pagina

  1. Prerequisiti
  2. Installa NVM
  3. Installa Node.js con NVM
  4. Utilizza NVM per gestire le versioni di Node.js
  5. Conclusione

NVM, chiamato anche \Node Version Manager\ viene utilizzato per l'installazione e la gestione di più versioni di Node.js in Linux. Fornisce un'utilità della riga di comando che aiuta gli sviluppatori a installare più versioni di Node e passare da una versione all'altra in base ai requisiti del progetto. NVM è molto utile se lavori su più progetti che richiedono diverse versioni di Node.

In questo post, ti mostreremo come installare e utilizzare NVM per gestire Node.js su Ubuntu 22.04.

Prerequisiti

  • Un server che esegue Ubuntu 22.04.
  • Sul server è configurata una password di root.

Installa NVM

Per impostazione predefinita, NVM non è incluso nel repository predefinito di Ubuntu. Quindi dovrai installarlo dallo script.

Innanzitutto, installa CURL e Gnupg2 con il seguente comando:

apt-get install curl gnupg2 -y

Successivamente, esegui il seguente comando per scaricare ed eseguire lo script di installazione di NVM:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Il comando precedente installerà NVM e creerà tutte le impostazioni dell'ambiente richieste nel file .bashrc.

=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Ora, attiva tutte le impostazioni usando il seguente comando:

source ~/.bashrc

Ora, verifica la versione NVM usando il seguente comando:

nvm --version

Dovresti vedere il seguente output:

0.39.1

Installa Node.js con NVM

NVM è ora installato nel tuo sistema. Ora puoi utilizzare la riga di comando NVM per installare qualsiasi versione di Node.js sul tuo sistema.

Ad esempio, per installare l'ultima versione di Node.js, eseguire il seguente comando:

nvm install node

Dovresti vedere il seguente output:

Downloading and installing node v18.9.1...
Downloading https://nodejs.org/dist/v18.9.1/node-v18.9.1-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.9.1 (npm v8.19.1)
Creating default alias: default -> node (-> v18.9.1)

Per verificare la versione installata di Node.js, eseguire il seguente comando:

node --version

Dovresti vedere il seguente output:

v18.9.1

Se desideri installare l'ultima versione stabile di Node.js, esegui il seguente comando:

nvm install node --lts

Dovresti vedere il seguente output:

v18.9.1 is already installed.
Now using node v18.9.1 (npm v8.19.1)

Per installare la versione specifica di Node.js (12.17.0), eseguire il seguente comando:

nvm install 12.17.0

Dovresti vedere il seguente output:

Downloading and installing node v12.17.0...
Downloading https://nodejs.org/dist/v12.17.0/node-v12.17.0-linux-x64.tar.xz...
######################################################################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.17.0 (npm v6.14.4)

Ora, verifica la versione corrente di Node.js utilizzando il seguente comando:

node --version

Dovresti vedere il seguente output:

v12.17.0

Usa NVM per gestire le versioni di Node.js

Per elencare tutte le versioni di Node.js installate nel tuo sistema, esegui il seguente comando:

nvm ls

Dovresti vedere il seguente output:

->     v12.17.0
        v18.9.1
         system
default -> node (-> v18.9.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.9.1) (default)
stable -> 18.9 (-> v18.9.1) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.20.1 (-> N/A)
lts/gallium -> v16.17.1 (-> N/A)

Puoi trovare tutte le versioni di Node.js disponibili utilizzando il seguente comando:

nvm ls-remote

Per impostare la versione Node.js predefinita su 12.17.0, esegui il seguente comando:

nvm use 12.17.0

Dovresti vedere il seguente output:

Now using node v12.17.0 (npm v6.14.4)

Per trovare la versione predefinita per l'utente corrente, eseguire il seguente comando:

nvm run default --version

Dovresti vedere il seguente output:

Running node v16.9.0 (npm v7.21.1)
v16.9.0

Puoi anche eseguire un'applicazione Node con una versione Node.js specifica utilizzando il seguente comando:

nvm run v12.17.0 app.js

Per rimuovere una versione specifica di Node.js dal tuo sistema, esegui il seguente comando:

nvm uninstall v12.17.0

Conclusione

In questa guida, abbiamo spiegato come installare e utilizzare NVM su Ubuntu 22.04. Ora puoi utilizzare la NVM per gestire più versioni di Mode.js e passare da una all'altra. Spero che ora tu possa eseguire la tua applicazione con qualsiasi versione di Node.js.