Recently, I needed to install a specific version of Node.js on a server that was locked down behind a strict firewall. The server only allowed access to GitHub and had the default apt build tools installed.
I couldn’t use version managers like nvm or add external repositories to install Node.js via apt. Additionally, the default Node.js version available through apt was outdated and didn’t meet my requirements.
Installing Node.js from source allows you to build the latest version or customize your build according to your needs. This guide provides step-by-step instructions for downloading, compiling, and installing Node.js from its GitHub repository.
Prerequisites
Before you begin, ensure you have the following installed:
- Git: To clone the repository.
- Python 3: Required for the build process.
- C/C++ Compiler: Such as GCC or Clang on Unix systems, or Visual Studio on Windows.
- Build Tools: make or ninja for Unix systems.
Steps
1. Install Build Dependencies
On macOS:
- Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-Install dependencies:
brew install git python3 gcc make
On Linux (Debian/Ubuntu):
sudo apt-get update
sudo apt-get install -y git python3 build-essential
On Windows:
- Install Git for Windows.
- Install Python 3.
- Install Visual Studio Community with C++ development tools.
2. Clone the Node.js Repository
git clone https://github.com/nodejs/node.git
cd node
3. Checkout a Specific Version (Optional)
List available versions:
git tag
Checkout a specific version (replace v16.6.0 with your desired version):
git checkout v20.18.0
4. Configure the Build
./configure
For customization options, run:
./configure --help
5. Compile the Source Code
On Unix Systems (macOS/Linux):
Use make to build:
make -j4
Replace 4 with the number of cores in your CPU for faster compilation.
On Windows:
Use the provided build script:
vcbuild.bat
For a 64-bit build:
vcbuild.bat x64
6. Install Node.js
On Unix Systems:
sudo make install
On Windows:
The build process creates an executable in the Release directory. You can add this directory to your PATH or move the executable to a preferred location.
7. Verify the Installation
Check the installed Node.js and npm versions:
node -v
npm -v
Additional Information
Updating the Source Code
To update your local repository with the latest changes:
git pull origin master
Cleaning Up Build Files
Before rebuilding, you may want to clean up previous build files:
make clean
Troubleshooting:
- Ensure all dependencies are correctly installed.
- Refer to the Node.js Building Guide for more detailed instructions.
Conclusion
You’ve successfully installed Node.js from the source code on GitHub. This approach gives you the flexibility to work with the latest features or customize your Node.js build.
Top comments (0)