Install Node and Angular on Linux and Docker

Setup your Angular development environment on Linux and Docker.

Install nodejs and Angular on Linux Ubuntu / Mint

I'm setting up an Ubuntu (22.x) / Linux Mint laptop for personal development. Here some notes about the setup of node.js and Angular.

Nodejs ... don't apt install

If I try to type node in the terminal, I receive the suggestion to install the package nodejs, this is an ultra old package. The current stable node version is 20, the version offered by the system is 12!

marco@x11-6:~$ apt show nodejs
Package: nodejs
Version: 12.22.9~dfsg-1ubuntu3.2

Nvm to the rescue

nvm is a node package manager, this is the recommended solution to manage node not only on Linux but on MacOs and Windows too.

A great feature of nvm is that you can install multiple versions of node and easily switch between them.

To install nvm you can follow the instructions on GitHub or curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

If your machine or container doesn't have curl you can use wget: wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

To install the version 20 of node we can simply type:

nvm install 20

``` bash /# nvm install 20 Downloading and installing node v20.10.0... Downloading https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.gz... ############################################################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v20.10.0 (npm v10.2.3) Creating default alias: default -> 20 (-> v20.10.0)


You can check their documentation to see the other features.

## Install Angular

Now that we have node and npm we can install Angular. If you have only to build a simple project you don't have to install Angular globally.

In the most common cases the commands that you will use more frequently are `npm install` and `ng build`. _npm_ comes with the install of _node_, to install _ng_ (globally) you have to `npm install -g @angular/cli`

To install `ng` we need to install `@angular/cli` with the _global_ flag. `npm install -g @angular/cli`

## Installing NVM inside an Ubuntu Docker

I tried to reproduce the installation of node and nvm inside Ubuntu. It has been more challenging that initally thought because Docker doesn't maintaint the context between commands.

``` dockerfile
# We use the official Ubuntu base image to install NVM
FROM ubuntu:latest

# Set the Node.js version to be installed
ENV NODE_VERSION 20

# Set the environment variable for NVM directory
ENV NVM_DIR /usr/local/nvm

# Update the package lists and install curl
RUN apt-get update \
    && apt-get install -y curl \
    && apt-get -y autoclean

# Create a directory for NVM
RUN mkdir /usr/local/nvm

# Download and run the NVM installation script
RUN curl https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
    # Activate NVM
    && . ${NVM_DIR}/nvm.sh \  
    # Install Node.js
    && nvm install ${NODE_VERSION} \
    # Set Node.js as the default version
    && nvm alias default ${NODE_VERSION} \ 
    && nvm use default

# Set environment variables for Node.js
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# Set the default command to run when the container starts
CMD ["bash"]

After building and running the container you can test that node 20 is correctly installed.