Home  >  Article  >  Web Front-end  >  How to install and configure Node.js on Linux

How to install and configure Node.js on Linux

PHPz
PHPzOriginal
2023-04-05 09:09:223581browse

With the continuous development of front-end technology, Node.js has become one of the most popular server-side development languages. As a server operating system, Linux is also essential in the process of using Node.js. In this article, we will introduce in detail how to install and configure Node.js on Linux.

1. Environment preparation
Before installing Node.js, we need to prepare the following conditions:

  1. Linux system with administrator rights.
  2. Requires a certain basic knowledge of Linux command line operations.
  3. Can ensure smooth network connection.

2. Install Node.js

  1. Use apt-get to install
    Debain-based Linux systems can install nodejs through apt-get, but this installation The nodejs version is relatively old and is not recommended.

$sudo apt-get install nodejs

If you need to upgrade, you can use the following command:

$sudo apt-get install npm
$sudo npm install n –g
$sudo n stable

  1. Use nvm installation
    It is recommended to use nvm (Node Version Manager) for Node.js installation, because it can easily switch different versions of Node .js. We need to install nvm first, and then install Node.js. Enter the following command in the terminal to install nvm:

$curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

After the installation is complete, use the following command to install Node.js:

$nvm install 6.2.2

  1. Verify the installation
    Enter the command line command node -v, If the node version number is successfully output, the installation is successful.

3. Configure Node.js

  1. Modify npm registry
    Due to the domestic network environment, we may need to use Taobao's npm image to speed up npm installation. You can use the following command:

$npm config set registry https://registry.npm.taobao.org

  1. Set environment variables
    For convenience in the terminal Using node and npm commands, we need to add the Node.js installation path to the system environment variable. We need to first find the installation path of Node.js and then add it to the environment variables. The installation path can be found using the following command:

$nvm which 6.2.2

If you want to add it to the environment variables, you can add it using the following command:

$echo "export PATH=$PATH:/usr/local/bin" >> ~/.bashrc
$source ~/.bashrc

  1. Configure npm global installation path
    Globally installed npm modules can be shared and used by all projects. For convenience, we need to set the npm global installation path. This can be set using the following command:

$mkdir ~/.npm-packages
$echo "prefix=$HOME/.npm-packages" >> ~/.npmrc
$echo "NPM_PACKAGES=$HOME/.npm-packages" >> ~/.bashrc
$echo "export PATH=$PATH:$NPM_PACKAGES/bin" >> ~/.bashrc
$source ~/.bashrc

4. Summary
This article introduces the process of installing and configuring Node.js on a Linux system. When using Node.js for server-side development, a stable and efficient operating system provides great convenience for our development work. Through the introduction of this article, I believe that everyone has a certain understanding and understanding of installing Node.js on Linux.

The above is the detailed content of How to install and configure Node.js on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn