Issue
I built NodeJS from source on an ubuntu-slim image. When you do this it requires a lot of package installs in the process, such as (for example. below is Node16 dependencies)
&& apt-get install --yes \
libstdc++6 \
...
...
&& apt-get install --yes \
binutils-gold \
g++ \
gcc \
gnupg \
libgcc-7-dev \
linux-headers-generic \
make \
python3 \
Once I have completed installing everything and configuring it, can I simply remove these build dependencies? The image has grown extensively and I need to try to clean as much fat as possible. Of course, I still want NodeJS to run my node apps.
The same kind of process happens for NodeJS 14, and I am guessing NodeJS 18.. Can we remove these build dependencies once the NodeJS is built?
Solution
After completing my building of Node14, I have found that:
- Yes, you can remove the build dependencies after node is completely
installed. I proved this specifically on Ubuntu20 OS platform with
installing Node14. The one caveat on this is that you are using Node
without needing to install native packages (native node packages
require binary compilation against the OS when the package is
npm install
ed- for example node-sass) - these are not common, so most uses of Node do not require this. - How to remove packages. I have found that uninstalling the
apk-get
packages that were installed is as simple as usingapk-get purge
. The example below (building node inside a Dockerfile using shell script) gives some specifics.
echo "Building from source" \
&& NODE_BUILD_PACKAGES="binutils-gold g++ gcc libgcc-7-dev linux-headers-generic make python3" \
&& apt-get install --yes ${NODE_BUILD_PACKAGES} \
...
...
[code to build node]
...
&& apt-get purge --yes ${NODE_BUILD_PACKAGES} \
&& apt-get clean \
&& apt-get autoremove --yes \
I used those three commands to clear out the build machine after building Node from source. For clarity, here is what each of those commands does:
- The command
apt-get purge
removes the packages that I installed so that I could build Node. - The command
apt-get clean
clears the local repository of retrieved package files that are left in /var/cache. - The command
apt-get autoremove
removes packages that were automatically installed because some other package required them.
Answered By - Kim Gentes Answer Checked By - Robin (WPSolving Admin)