LearnHow to Install Node.js and NPM on Linux

Andrew Chalkley
writes on January 15, 2015

JavaScript is everywhere. From its birthplace in the Netscape browser in the mid-nineties, it’s made its way into all modern web browsers, onto mobile phones, and into electronics to make full-fledged web applications on the server-side with Node.js.

Node.js is non-blocking, which means it’s ideal for creating real-time web applications such as chat servers, analytics, collaboration tools, and interactive games.

In order to create such applications, you need NPM a package manager for Node. It allows you to download and installed open source JavaScript libraries that help you make awesome applications.

Prerequisites

  • You should have some familiarity with the Linux terminal since you’ll need to use it to install and test Node and NPM. You’ll also need the terminal to use Node.js and NPM.
  • Dependencies. You need to install a number of dependancies before you can install Node.js and NPM.
    1. Ruby and GCC. You’ll need Ruby 1.8.6 or newer and GCC 4.2 or newer.
      • For Ubuntu or Debian-based Linux distributions, run the following command in your terminal: sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev Then select Y to continue and wait for the packages to be installed.
      • For Fedora based Linux distributions run the following command in your terminal application: sudo yum groupinstall 'Development Tools' && sudo yum install curl git m4 ruby texinfo bzip2-devel curl-devel expat-devel ncurses-devel zlib-devel Then select Y to continue and wait for the packages to be installed.
    2. Homebrew. Homebrew is a package manager originally for the Mac, but it’s been ported to Linux as Linuxbrew, making installing most open-source software (like Node) as simple as writing: brew install node You can learn more about Homebrew at the Homebrew website and Linuxbrew at the Linuxbrew website. To install Homebrew for Linux, open your terminal application and paste in the command: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)" Follow the instructions in the terminal to complete the installation process.

Once Linuxbrew is installed, you’ll need add the following 3 lines to your .bashrc or .zshrc file:

  export PATH="$HOME/.linuxbrew/bin:$PATH"
  export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"
  export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"

Why Homebrew/Linuxbrew?

You may be asking why you should use a third-party package manager. Here are some advantages:

  • Can install software to a home directory and so does not require sudo
  • Install software not packaged by the native distribution
  • Install up-to-date versions of software when the native distribution is old
  • Use the same package manager to manage both your Mac and Linux machines

Installation

Installing Node.js and NPM is pretty straightforward using Linuxbrew, the Linux port of Homebrew. It handles downloading, unpacking, compiling, and installing Node and NPM on your system. After you have Linuxbrew installed, the whole process should only take you a few minutes.

  1. Open up your terminal and type brew install node.
  2. Sit back and wait. Homebrew has to download some files, compile and install them. But that’s it.

Testing it out.

Make sure you have Node and NPM installed by running simple commands to see what version of each is installed:

  • Test Node.js. To see if Node.js is installed, type node -v in the terminal. This should print the version number, so you’ll see something like this: v0.10.35.
  • Test NPM. To see if NPM is installed, type npm -v in the terminal. This should print the version number, so you’ll see something like this: 2.1.17

node-install-check

How to Update Node and NPM

New versions of Node and NPM are released frequently. You can use Homebrew to update the software it installs.

  1. Make sure Homebrew has the latest version of the Node package. In Terminal type:
    brew update
  2. Upgrade Node: brew upgrade node

How to Uninstall Node and NPM

You can also use Homebrew to uninstall packages that it installed:

brew uninstall node

Other Platforms

Thanks to Dave McFarland, as he wrote some installation guides to install Node.js and NPM on Mac (which this guide heavily borrows from) and Windows.

Conclusion

Now that you have Node.js and NPM installed, you can build an awesome application with Node.js, or use one of the Node packages like Grunt or Gulp to improve your front-end workflow.

If you’re new to Node.js you may want to check out the Node.js Basics course at Treehouse!

19 Responses to “How to Install Node.js and NPM on Linux”

  1. Really good article! Just an update, the linux homebrew repo has recently moved to http://linuxbrew.sh/

  2. This article presents a heavyweight solution that didn’t work for me.

    After hours downloading everything, Linuxbrew didn’t work to install Node, finally giving some error about not being able to use/locate Perl, even though it is definitely installed on the Ubuntu 16 system.

    I eventually found NVM to install NPM which was so much faster and easier: https://stackoverflow.com/a/39981888

  3. Please add in this to the instructions between adding the 3 export PATHS and brew install node:

    brew update

    from:
    https://github.com/Homebrew/homebrew-core/issues/2494
    (bottom of page)

    Nice tutorial man, (for those who want to do JS development but still wont get a mac!)

    Thanks

  4. maybe check out this :
    https://gist.github.com/GracjanZwr/112de571d939e5a4dff5ab97a400afae
    let me know if everything is working and in right order 😉
    peace !

  5. Thanks a lot buddy, really helpfull

  6. Good post. I tested with Fedora 23 and works fine!.
    However, I think you should add one line. Always, before try to install node, run the command.
    brew update
    Otherwise you will have errors or inconsistencies in your installation.

    • Brew is “The missing package manager for Mac OS” , there is a Linux version but I would recommend using apt, apt-get or rpm depending on your Linux distribution instead of brew 😉 .

  7. Well using the above list of dependencies, i looked at installing it on OpenSuse 13.1 but it wants to install 1476 NEW packages, most of which are called texlive – not gonna happen – thats foolish. There is no way that ANY package can require that much in dependencies.

  8. Mouhanad Chebib on November 21, 2015 at 10:13 am said:

    On GoDaddy hosting:
    – I had both Ruby and GCC, with the minimum versions installed.
    – I installed Homebrew (linuxbrew) at `~/.linuxbrew` and it is installed with version 0.9.5.

    When trying to install NodeJS with `brew install node`, I am getting an error:
    “`
    nice: /usr/local/bin/ruby: No such file or directory
    Error: Suspicious failure
    “`

    When checking my ruby installation, `which ruby` it is here /usr/bin/ruby.

    Please advise.

  9. “Any reason this is called Linux but more specific to Mac?”

    No. gcc and (home)brew is running w/o any problem on my osx too.

  10. I was able to successfully install these items, however now when I try to check for a version of node (node -v in terminal) or when I try a brew command (brew install….) I get a command not found “brew” error message. Any idea what happened to my installation?

    Thanks for your help ~Alex

  11. Any reason this is called Linux but more specific to Mac?

  12. On vagrant (trusty32 – Ubuntu 14.04), i use something like:
    apt-get -y install curl
    curl -sL https://deb.nodesource.com/setup | sudo bash –
    apt-get -y install nodejs

    Just my 0.02 🙂

  13. Complicated thing like node.js made easy, Thanks.

  14. If you’re using at least Ubuntu 14.04 (not sure about earlier versions), **nodejs** resides within the default repositories. You can just run `sudo apt-get install nodejs` and `sudo apt-get install npm`.

  15. Great image choice for an article about Linux 😉

  16. Finally a post about node.js, its really complex but you managed to make it sound easy, thanks a lot.

Leave a Reply

Want to learn more about Javascript?

Learn how to use JavaScript to add interactivity to websites.

Learn more