본문 바로가기

리눅스

우분투에 node.js와 npm을 설치하는 방법

반응형

우분투에 node.js와 npm을 설치하는 방법

  • NVM(Node Version Manager)
  • NPM(Node Package Manager)

테스트 환경

  • 운영체제 버전 정보
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"

1. NVM 설치

NVM 설치 스크립트를 다운로드하고 실행합니다.

curl -s -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
$ curl -s -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 358, done.
remote: Counting objects: 100% (358/358), done.
remote: Compressing objects: 100% (304/304), done.
remote: Total 358 (delta 41), reused 162 (delta 28), pack-reused 0
Receiving objects: 100% (358/358), 218.72 KiB | 3.98 MiB/s, done.
Resolving deltas: 100% (41/41), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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

NVM 환경 변수 설정

.bashrc 파일에 nvm 환경 변수 적용

$ cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

PS1=$PS1A

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

.nvm 디렉터리 확인

$ ls -al | egrep .nvm
drwxr-xr-x 2 root root 4096 Jul 21 23:51 .nvm
source ~/.bashrc

NVM 버전 정보 확인

nvm --version
$ nvm --version
0.39.3

2. NVM을 사용하여 Node.js 설치

Node.js의 LTS(장기 지원) 버전을 설치하려면 다음 명령을 사용합니다.

release(node) 된 전체 버전 목록 출력(node 버전 확인)

nvm ls-remote

LTS(Long Term Support) 버전만 목록 출력

nvm ls-remote --lts
$ nvm ls-remote | egrep Latest
         v4.9.1   (Latest LTS: Argon)
        v6.17.1   (Latest LTS: Boron)
        v8.17.0   (Latest LTS: Carbon)
       v10.24.1   (Latest LTS: Dubnium)
      v12.22.12   (Latest LTS: Erbium)
       v14.20.0   (Latest LTS: Fermium)
       v16.16.0   (Latest LTS: Gallium)

Node.js 설치

nvm install 16.16.0
$ nvm install 16.16.0
Downloading and installing node v16.16.0...
Downloading https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.16.0 (npm v8.11.0)
Creating default alias: default -> 16.16.0 (-> v16.16.0)

Node.js 버전을 사용할 수 있도록 설정

nvm use 16

로컬에 설치되어 있는 Node.js 목록 출력

$ nvm ls
       v16.16.0
default -> 16.16.0 (-> v16.16.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.16.0) (default)
stable -> 16.16 (-> v16.16.0) (default)
lts/* -> lts/gallium (-> v16.16.0)
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.0 (-> N/A)
lts/gallium -> v16.16.0

Node.js 버전 확인

node --version
$ node --version
v16.16.0

npm 버전 확인

npm -v
$ npm -v
8.11.0
728x90

node 12 버전 추가 설치

  • minor와 patch 버전은 생략 가능함
  • 마지막으로 설치한 버전으로 사용할 버전이 지정됨

Node.js 12 설치

nvm install 12
$ nvm install 12
Downloading and installing node v12.22.12...
Downloading https://nodejs.org/dist/v12.22.12/node-v12.22.12-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.12 (npm v6.14.16)
nvm ls
$ nvm ls
      v12.22.12
       v16.16.0
default -> 16.16.0 (-> v16.16.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.16.0) (default)
stable -> 16.16 (-> v16.16.0) (default)
lts/* -> lts/gallium (-> v16.16.0)
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
lts/fermium -> v14.20.0 (-> N/A)
lts/gallium -> v16.16.0

Node.js 버전을 사용할 수 있도록 설정(임의로 node 16으로 변경하고 다시 변경하였음)

$ nvm use 12
Now using node v12.22.12 (npm v6.14.16)

Node.js 및 npm 버전 확인

$ node -v
v12.22.12
$ npm -v
6.14.16

npm 목록 확인

npm ls
$ npm ls
/root
`-- (empty)

3. node.js 기본 웹 올리기

프로젝트 디렉터리 생성

mkdir node-www
cd node-www/

npm 초기화

npm init
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (www)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /root/node-www/package.json:

{
  "name": "www",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) yes
npm notice
npm notice New minor version of npm available! 8.11.0 -> 8.15.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.15.0
npm notice Run npm install -g npm@8.15.0 to update!
npm notice
npm ls
$ npm ls
www@1.0.0 /root/node-www
`-- (empty)
$ ls -l | grep package.json
-rw-r--r-- 1 root root 199 Jul 22 00:47 package.json

index.js 파일 편집(생성)

$ vi index.js
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}/`);
});

index.js code >> https://nodejs.org/en/docs/guides/getting-started-guide/

node.js  서버 시작

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

curl 접속 테스트

docker exec centos7 sh -c "curl -s localhost:3000"
$ docker exec centos7 sh -c "curl -s localhost:3000"
Hello World

 

728x90
반응형