Sunday, May 26, 2024

Set up Node.js on Ubuntu — SitePoint

Must read


Node.js has revolutionized the way in which we construct net functions by enabling builders to create scalable community functions utilizing JavaScript on the server aspect. To harness the total potential of Node.js, you first want to put in it in your system. On this information, we’ll stroll you thru the method of set up on Ubuntu, a well-liked Linux distribution, utilizing three totally different strategies. We’ll additionally cowl stipulations, organising your Node.js surroundings, and uninstalling Node.js if wanted.

Desk of Contents

Key Takeaways

  • Learn to set up Node.js and npm on Ubuntu utilizing the default repository, NodeSource repository or NVM (Node Model Supervisor).
  • Arrange your surroundings by making a mission listing, initializing a mission and working easy applications.
  • Uninstall Node.js from Ubuntu with both apt bundle supervisor or NVM relying on set up technique used.

Understanding Node.js

Node.js is a server-side JavaScript runtime surroundings that enables builders to construct scalable net functions and APIs utilizing the identical codebase for each server and shopper sides. Its event-driven structure and non-blocking enter/output API make it excellent for real-time net functions. Putting in Node.js in your net server lets you leverage these options, key to creating state-of-the-art net functions.

Furthermore, Node.js gives quite a few advantages, corresponding to scalability, fast growth price, and a strong neighborhood. With an ever-growing variety of packages and modules out there, Node.js has change into an indispensable instrument for net builders worldwide.

Stipulations for Putting in Node.js on Ubuntu

Make sure you meet the stipulations for putting in Node.js on Ubuntu earlier than initiating the set up course of. Listed below are the stipulations:

  1. You want a suitable Ubuntu model.
  2. You will need to have sudo privileges, which you’ll be able to confirm by executing the command sudo whoami and coming into your password when prompted.
  3. A fundamental understanding of JavaScript and command-line utilization is important.

There are three distinct strategies for putting in Node.js on Ubuntu:

  1. Utilizing the default Ubuntu repositories
  2. Using the NodeSource repository
  3. Implementing NVM (Node Model Supervisor)

NVM is really helpful for customers who require frequent switching between Node.js variations, because it lets you handle a number of variations simply.

Methodology 1: Putting in Node.js and npm from Default Ubuntu Repositories

The primary technique to put in Node.js and npm entails utilizing the default Ubuntu repositories. This method is easy and appropriate for many customers. Nevertheless, it could present an older model of Node.js in comparison with different strategies.

The next sections will information you thru the steps to replace the bundle lists, set up Node.js and npm, and confirm the set up.

Replace bundle lists

Be certain to replace the bundle lists within the terminal previous to putting in Node.js and npm. This ensures that you simply get the most recent JS variations put in in your system. To replace the bundle lists on Ubuntu, execute the next command within the terminal: sudo apt replace. As soon as the bundle lists are up to date, you’ll be able to proceed with the set up course of.

Set up Node.js and npm

To put in Node.js and npm utilizing the apt bundle supervisor, execute the command sudo apt set up nodejs within the terminal. Press Y when prompted to substantiate the set up. This command will set up Node.js from the default Ubuntu repositories, making the method easy and safe.

Subsequent, you want to set up npm, the bundle supervisor for Node.js, which lets you simply handle and share your code. To put in npm, first set up node by executing the command sudo apt set up npm.

With Node.js and npm put in, now you can begin working your JavaScript code and creating net functions.

Confirm set up

As soon as Node.js and npm are put in, it’s essential to substantiate the success of the set up. To verify if Node.js and npm are put in on the system, execute the instructions node -v and npm -v within the terminal. If the model numbers of each Node.js and npm are displayed, they’ve been put in accurately, and you can begin working your JavaScript code.

Methodology 2: Putting in Node.js and npm from NodeSource Repository

The second technique entails putting in Node.js and npm from the NodeSource repository. This technique is appropriate for customers who need entry to the most recent model of Node.js and npm or want particular releases.

The NodeSource repository is a Private Bundle Archive (PPA) that provides extra variations of Node.js than the default Ubuntu repository. The next sections will information you thru the steps so as to add the NodeSource repository, set up Node.js and npm, and make sure the set up.

Add the NodeSource repository

So as to add the NodeSource repository to your system, you should utilize curl or wget. First, open a terminal by urgent Ctrl + Alt + T. Then, execute the suitable command to your Ubuntu model, which may be discovered on the NodeSource repository’s GitHub web page. This can obtain and run the set up script, incorporating the NodeSource repository into your system.

As soon as the repository is added, you’ll be able to proceed with the set up of Node.js and npm.

Set up Node.js and npm

With the NodeSource repository added to your system, now you can set up Node.js and npm. To do that, execute the command sudo apt set up nodejs within the terminal. Press Y when prompted to substantiate the set up. This command will set up Node.js and npm from the NodeSource repository, offering you with the most recent model or a selected launch, relying in your wants.

Verify set up

Upon putting in Node.js and npm from the NodeSource repository, make sure to validate the set up. To do that, execute the instructions node -v and npm -v within the terminal. If the model numbers of each Node.js and npm are displayed, they’ve been put in efficiently, and you can begin working your JavaScript code.

Methodology 3: Putting in Node.js and npm utilizing NVM (Node Model Supervisor)

NVM is a flexible instrument for Node.js model administration, permitting you to put in and handle a number of Node.js variations simply. This technique is right for customers who want to modify between Node.js variations usually or wish to take a look at their functions with totally different variations. We have now a whole information to utilizing NVM right here.

The next sections will information you thru the steps to put in NVM, choose and set up a Node.js model, and swap between put in variations.

Set up NVM

To put in NVM, you want to observe these steps:

  1. Open a terminal by urgent Ctrl + Alt + T.
  2. Obtain the NVM set up script utilizing both curl or wget. You could find the suitable command within the NVM repository’s README file on GitHub.
  3. Run the NVM set up script. This can incorporate NVM into your system.

After putting in NVM, you want to replace your shell’s configuration file (corresponding to `.bashrc`) to load NVM routinely when opening a brand new terminal session. To do that, add the next strains to the tip of your configuration file, or create a brand new file if it doesn’t exist: export NVM_DIR=”$HOME/.nvm” [ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh”

Save the file and restart your terminal. Now you can use NVM to handle your Node.js variations.

Choose and set up Node.js model

With NVM put in, now you can choose and set up a selected Node.js model. To put in the most recent LTS model of Node.js, execute the command nvm set up –lts within the terminal.

To set the default model of Node.js for brand new terminal classes, run the command nvm alias default <model>. This ensures that the specified model is used everytime you open a brand new terminal session.

Swap between put in variations

NVM lets you swap between totally different put in Node.js variations simply. To change to a selected Node.js model, execute the command nvm use <model> within the terminal.

For instance, to modify to model 14.19.0, sort nvm use 14.19.0. This command will activate the desired model, permitting you to check your functions with totally different Node.js variations or use the options of a selected model.

Uninstalling Node.js from Ubuntu

If you happen to ever must take away Node.js out of your Ubuntu system, the method will rely on the set up technique you used. If you happen to put in Node.js utilizing the apt bundle supervisor (utilizing Methodology 1 or Methodology 2 described above), you’ll be able to uninstall it by executing the command sudo apt take away nodejs within the terminal. This command will successfully take away the Node.js bundle out of your system.

If you happen to put in Node.js utilizing NVM (Methodology 3), you’ll be able to uninstall a selected model by executing the command nvm uninstall <model>. Earlier than uninstalling the at present energetic model, you want to deactivate it by executing the command nvm deactivate. This can will let you uninstall the energetic model with none points.

Abstract

On this information, we lined three totally different strategies for putting in Node.js on Ubuntu: utilizing the default Ubuntu repositories, the NodeSource repository, and NVM (Node Model Supervisor). Every technique has its benefits and is appropriate for various eventualities.

FAQs About Putting in Node on Ubuntu

How do I set up Node.js on Ubuntu 20.04 LTS?

To put in Node.js 20 on Ubuntu 20.04 LTS, run the next instructions in a terminal window: sudo apt replace, sudo apt improve, sudo apt set up -y curl, curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -, and sudo apt set up -y nodejs. Confirm the model with node --version.

How can I confirm if I’ve sudo privileges?

To confirm sudo privileges, run the command sudo whoami and enter your password when prompted.

How can I swap between totally different Node.js variations put in utilizing NVM?

To change between Node.js variations put in utilizing NVM, merely execute the command nvm use <model> within the terminal. See our information to utilizing NVM right here.

How do I uninstall Node.js from my Ubuntu system?

To uninstall Node.js from Ubuntu, you should utilize both the apt bundle supervisor or NVM.





Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article