An Ubuntu Environment with Node.js
A stable one using Docker
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”:
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”.
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):
apt-get install -y curl
curl -sL https://deb.nodesource.com/setup_14.x |bash
apt-get install -y nodejs
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:
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.
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 ;)