3 Methods to Install Node.js and Yarn or NPM on FreeBSD
Node.js is a popular JavaScript runtime for developing server-side applications using JavaScript. On FreeBSD, you can install Node.js and Node.js package manager (NPM or Yarn) through different methods, such as via the FreeBSD repository and ports tree.
In this guide, I will show you how to install Node.js and Node.js package manager (NPM or Yarn) using different methods on FreeBSD 14.
Prerequisites
Before you jump in, confirm that you have these:
- A FreeBSD 14 server.
Read more: FreeBSD 14: Step-by-Step Installation (With Screenshot) - A root user or non-root user with sudo privileges.
- (Optional) Bash and Ports tree are configured.
Read more: 16 Steps You Must Do to Set Up FreeBSD as Server.
Installing Node.js on FreeBSD
On FreeBSD, there are multiple Node.js versions available, including Node.js 16, 18, 20, and 21. Those can be installed easily from the official FreeBSD repository via the PKG package manager.
Proceed with the following actions to install Node.js on your FreeBSD system:
1. First, run the following command to update your FreeBSD package index.
pkg update
2. Now install Node.js using the pkg
command below. As I write this article, the default version of Node.js in FreeBSD 14 is Node.js v20. But you can also install different versions of Node.js with the following.
# install default Node.js package
# current FreeBSD 14 with Node.js 20.10
pkg install node
# Installing Node.js 21
pkg install node21
# Installing Node.js 20
pkg install node20
# Installing Node.js 18
pkg install node18
# Installing Node.js 16
pkg install node16
Input y
to proceed with the installation.
3. Once Node.js is installed, run the following command to verify the location path of the node
command and the Node.js version.
# checking path node command Node.js
which node
# checking node.js version
nodejs --version
As seen in the output below, Node.js 20.10 are installed in /usr/local/bin/node
.
Installing NPM or Yarn on FreeBSD
After installing Node.js, let’s install the Node.js package manager. You can choose between NPM or Yarn as your default Node.js package manager, and both packages are available by default on the FreeBSD repository.
Choose your preferred Node.js package manager and install it with the following steps:
1. If you prefer npm, run the following command to install it to your FreeBSD system. Be sure to select the same version as your Node.js version.
# installing default NPM
pkg install npm
# installing NPM specific version
# be sure to select the same version as your Node.js
pkg install npm-node21
pkg install npm-node20
pkg install npm-node18
pkg install npm-node16
2. After the installation is finished, verify the location of the npm
binary file and the npm version using the following command.
# checking path of npm command
which npm
# checking NPM version
npm --version
3. If you prefer Yarn as the Node.js package manager, run the following command to install it. Select the same version of Yarn as your Node.js version.
# installing default Yarn
pkg install yarn
# installing Yarn specific version
# be sure to select the same version as your Node.js
pkg install yarn-node21
pkg install yarn-node20
pkg install yarn-node18
pkg install yarn-node16
4. Then verify the yarn
binary file and its version by running the command below.
# checking path of yarn command
which yarn
# checking Yarn version
yarn --version
Here you can see Yarn 1.22 are installed in /usr/local/bin/yarn
.
Installing Node.js and NPM/Yarn on FreeBSD via Ports Tree
This section is dedicated to when you need to compile Node.js and its package manager manual via the ports tree. So be sure that you’ve ports enabled in your FreeBSD system.
Note: Compiling manually will take time, and it also depends on your system too.
Follow these steps if you need to install Node.js and NPM/Yarn via the ports tree:
1. Move to the /usr/ports/www/node
directory and run make install
to compile and install Node.js.
# compiling and installing Node.js manually
# via FreeBSD ports
cd /usr/ports/www/node && make install
# compiling and installing specific Node.js version
# via FreeBSD ports - select one version of these
cd /usr/ports/www/node20 && make install
cd /usr/ports/www/node21 && make install
cd /usr/ports/www/node18 && make install
cd /usr/ports/www/node16 && make install
2. To install NPM via ports, move to the /usr/ports/www/npm
directory and execute the make install
command to compile and install it to your system.
# compiling and installing NPM manually
# via FreeBSD ports
cd /usr/ports/www/npm && make install
# compiling and installing specific NPM version
# via FreeBSD ports - select one version match with your Node.js version
cd /usr/ports/www/npm-node20 && make install
cd /usr/ports/www/npm-node21 && make install
cd /usr/ports/www/npm-node18 && make install
cd /usr/ports/www/npm-node16 && make install
3. Lastly, if you prefer Yarn from the ports tree, then go to the /usr/ports/www/yarn
directory and run the make install
command to compile and install it.
# compiling and installing Yarn manually
# via FreeBSD ports
cd /usr/ports/www/yarn && make install
# compiling and installing specific Yarn version
# via FreeBSD ports - select one version match with your Node.js version
cd /usr/ports/www/yarn-node20 && make install
cd /usr/ports/www/yarn-node21 && make install
cd /usr/ports/www/yarn-node18 && make install
cd /usr/ports/www/yarn-node16 && make install
Installing Node.js via NVM (Node Version Manager)
Note: Unfortunately the nvm still does not support FreeBSD. You can still use nvm to install and manage Node.js versions, but it will install and compile from the source.
NVM or Node Version Manager is a bash POSIX compliant for managing multiple Node.js versions. Learn how to install and use NVM (Node Version Manager) on FreeBSD using the following steps:
Installing NVM
1. First, run the command below to install dependencies curl
, gcc
, and gmake
.
sudo pkg install -y curl gcc gmake
2. Now execute the following command to log in to your user install nvm. This will download and run the installer script install.sh
.
# login to user
su - username
# download and install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Once the process is complete, you should get the instructions to reopen your current session to apply your changes.
3. Or run the command below to reload the ~/.bashrc
script and take effect, then verify the nvm
command. If nvm is installed, you should get an out nvm
.
# reload ~/.bashrc script
source ~/.bashrc
# check if nvm command is available
command -v nvm
4. Now run the nvm
command below to show the available options in the nvm.
nvm --help
Installing and Managing Node.js via NVM
1. First, run the nvm
command below to show the list of available Node.js versions available for installation.
nvm ls-remote
2. Now run the nvm install
command below to install Node.js to your FreeBSD system. This will download the Node.js source code, compile it, and install it on your user environment.
nvm install node
nvm install --lts
nvm install v20.11.0
3. Once the process is finished, you can now set the default Node.js version by running the nvm
command below.
nvm use --lts
Conclusion
Installing Node.js on FreeBSD has never been easier than this. The FreeBSD repository offers flexibility by providing multiple Node.js versions ready to install. In addition to that, you can also choose between NPM or Yarn as your Node.js package manager.
If you need custom installation, you can install Node.js via the Ports tree, which also provides multiple versions of Node.js sources. As for nvm (Node Version Manager) on FreeBSD, you can use it to manage multiple Node.js versions on your environment, but the installation is also from sources.