So installieren Sie Node.js auf Ubuntu 22.04
Node.js auf Ubuntu 22.04 installieren
Ziel
Node.js ist eine der bekanntesten asynchronen ereignisgesteuerten JavaScript-Laufzeitumgebungen. In den letzten Jahren hat sie sich so weit verbreitet, dass sie heute eine unverzichtbare Plattform für die Softwareentwicklung ist. Weitere Informationen zu den Funktionen der Node.js-Plattform finden Sie in der offiziellen Dokumentation.
In diesem Tutorial erfahren Sie, wie Sie eine Node.js-Plattform auf der Linux-Distribution Ubuntu 22.04 installieren.
Voraussetzungen
Dieses Tutorial setzt eine OVHcloud Public Cloud Compute Instanz, einen VPS oder einen Bare-Metal-Server mit Ubuntu 22.04 voraus, sowie Grundkenntnisse im Umgang mit der Kommandozeile. In diesem Tutorial verwenden wir eine Public Cloud Compute Instanz. Sie brauchen Hilfe bei der Einrichtung einer Public Cloud Instanz mit Ubuntu 22.04? Dann lesen Sie folgende Anleitung: Erste Public Cloud Instanz erstellen und auf dieser einloggen.
Um den Node.js Manager zu installieren, muss zuerst das make-Tool installiert werden.
Anleitung
In diesem Tutorial installieren und verwenden Sie eine Node.js-Plattform und lernen, wie Sie zwischen verschiedenen installierten Versionen wechseln.
Zum Zeitpunkt der Verfassung dieses Tutorials war die neueste Node.js-Version 16.15.x und die neueste GA-Version 18.1.x.
Ein Node Manager für alle
Vor der Installation von Node.js muss ein Tool installiert werden, um mehrere Node.js-Installationen zu verwalten. Die beiden am häufigsten verwendeten Tools sind nvm und n. Im vorliegenden Beispiel verwenden wir n.
Um n auf Ubuntu zu installieren verwenden Sie das folgende Bash-Skript:
curl -L https://bit.ly/n-install | bash
Output:
$ 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. ===
Überprüfen Sie die Installation:
n --version
Output:
$ n --version v8.2.0
Node.js mit n installieren
Um die LTS-Version von Node.js zu installieren, geben Sie folgenden Befehl ein:
n lts
Output:
$ n lts copying : node/16.15.0 installed : v16.15.0 (with npm 8.5.5)
Überprüfen Sie die neue Installation:
npm --version node --version
Output:
$ npm --version 8.5.5 $ node --version v16.15.0
Um die neuste GA-Version von Node.js zu installieren:
n current
Output:
$ 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)
Node.js-Installation testen
Sie testen Ihre Node.js-Installation, indem Sie eine Hello World-Anwendung schreiben. Erstellen Sie eine HelloWorld.js-Datei und fügen Sie den folgenden Code in diese Datei ein:
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}/`); });
Speichern Sie die Datei und führen Sie sie aus:
node HelloWorld.js
Output:
$ node HelloWorld.js Server running at http://127.0.0.1:3000/
Um Ihr Beispiel zu testen, führen Sie folgenden Befehl aus:
curl http://127.0.0.1:3000/
Output:
$ curl http://127.0.0.1:3000/ 👋 Hello World
Geschafft! Sie haben erfolgreich Node.js auf Ubuntu 22.04 installiert und konfiguriert.