An Ubuntu Environment with Node.js

Trying to install node.js and its utility npm in Ubuntu can be challenging. Naively installing through apt-get can lead to complex problems with no clear solution such as the “Missing required argument”:

npm install error example

Yes, I am writing a full article just to install node.js. Not the first time I got such issues so I better document them. Thus to minimize the number of problems, install node.js using one of the following approaches. Also, don’t forget to read the “useful tip section”.

TL;DR

Prebuilt images:

docker run -it --net=host nodejs14

change “14” by the version you want: 14, 15, 16, or 17.

Using the Command Line Directly

The first version is to install directly using the command line. It is highly recommended to run them inside a docker. You can fire a fresh Ubuntu docker by running this command:

docker run -it ubuntu:latest

Here are the commands to install node.js version 14 using 3 steps (source):

# prepare
apt-get update
apt-get install -y curl
# install
curl -sL https://deb.nodesource.com/setup_14.x |bash
apt-get install -y nodejs
# confirm
node -v
npm -v

This first approach is useful to test the right node.js for your packages. Indeed sometimes installing the latest version of node.js does not work and you need to downgrade to an earlier version of node.js.

Using a Dockerfile

Once you know the node.js version that works with your package, you can write a Dockerfile that will contain the following lines:

FROM ubuntu:latest
RUN apt-get update -yq \
&& apt-get install curl -yq \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash \
&& apt-get install nodejs -yq
# The packages you want to install !
RUN npm install -g <packages>

Replace <packages> with the one you want to install. Put the file inside an empty folder then run the following command:

docker build -t mydocker .

replace mydocker with the docker’s name you want. Then start your environment using the following command.

Useful Tips

When starting a docker you will probably want to add one of these options or both:
1) “--net=host” when running the docker. This option allows the docker to access the network of the machine. Thus you can access your website through a browser when you run it.

docker run -it --net=host ubuntu:latest

2) -v /home/$USER:/home/$USER. This option allows the docker to access the files in your home repository.

docker run -it -v /home/$USER:/home/$USER --net=host ubuntu:latest

I wrote this small tutorial while installing truffle. You can find the Dockerfile that I used to build truffle here. I had to do the steps twice since I first installed the latest version of node.js (16) and encountered this problem. Downgrading solved the issue.

Will have more articles coming about using truffle. Until then, enjoy, feel free to leave a comment, and don’t forget to follow/like ;)

--

--

--

Big fan of technology with a recent interest in finance following the rise of cryptocurrencies.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Value of Map

Quell is Back with Faster and More Intuitive Caching.

How to handle more than one async/await

A Test Runner for Broccolijs Plugins

7 Must-Know VS Code Extensions for a React Developer

Milestone-1 | Day-6 | OOP | JavaScript

Writing a Single Page Application with Vanilla JS

A flowering vanilla plant in Florida Southern College’s greenhouses

Join the Dark Side — Implementing Dark Mode in React Native

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amine

Amine

Big fan of technology with a recent interest in finance following the rise of cryptocurrencies.

More from Medium

Simple Web Server (NodeJs + Express) + Apache2

Introduction to NodeJS

Application Framework with JavaScript

A Gaze Into the salted World of Docker