Ubuntu 22.10に最新版Node.jsをインストールする方法

環境
OSのバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.10
DISTRIB_CODENAME=kinetic
DISTRIB_DESCRIPTION="Ubuntu 22.10"
# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.10 DISTRIB_CODENAME=kinetic DISTRIB_DESCRIPTION="Ubuntu 22.10"
# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.10
DISTRIB_CODENAME=kinetic
DISTRIB_DESCRIPTION="Ubuntu 22.10"

操作方法
1.node.js 12 のインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# apt -y install nodejs npm
# apt -y install nodejs npm
# apt -y install nodejs npm

2.nodeバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# node -v
v18.7.0
# node -v v18.7.0
# node -v
v18.7.0

3.テストスクリプトを作成します
# nano test.js
以下のコードを記述します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var http = require('http');
var server = http.createServer(function(req, res) {
res.write("Hello world, study Node.js Simple Web Server!\n");
res.end();
}).listen(8080);
var http = require('http'); var server = http.createServer(function(req, res) { res.write("Hello world, study Node.js Simple Web Server!\n"); res.end(); }).listen(8080);
var http = require('http');
var server = http.createServer(function(req, res) {
  res.write("Hello world, study Node.js Simple Web Server!\n");
  res.end();
}).listen(8080);

4.動作確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# node test.js &
[1] 4673
# node test.js & [1] 4673
# node test.js &
[1] 4673

curlをインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# apt install curl
# apt install curl
# apt install curl

# curl localhost:8080
Hello world, study Node.js Simple Web Server!

プロセスを終了します
# kill 4673

Ubuntu 22.10

Posted by arkgame