This error:
-bash: /usr/bin/nodejs: cannot execute binary file: Exec format error
indicates that the nodejs executable installed in the linux distribution is not supported.
In my case I have had this error when installing a 18.15.0 LTS version of nodejs on Ubuntu 22.04.
To solve it, you must uninstall the version of node that gives the error with the following command in the case of Ubuntu:
sudo apt remove nodejs -y
sudo apt purge nodejs -y
And once we have uninstalled nodejs we proceed to run a compatible version, which in the case of Ubuntu 22.04 is a version 19 of node running:
curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash -
This command at the end tells us that we must execute to complete the installation of nodejs 19:
## Installing the NodeSource Node.js 19.x repo...
## Populating apt-get cache...
+ apt-get update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:2 https://packages.microsoft.com/repos/azure-cli jammy InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
Get:6 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [693 kB]
Get:7 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [143 kB]
Get:8 http://security.ubuntu.com/ubuntu jammy-security/main amd64 c-n-f Metadata [9016 B]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [645 kB]
Get:10 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [100 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 c-n-f Metadata [588 B]
Get:12 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [714 kB]
Get:13 http://security.ubuntu.com/ubuntu jammy-security/universe Translation-en [118 kB]
Get:14 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 c-n-f Metadata [14.1 kB]
Get:15 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [19.4 kB]
Get:16 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 c-n-f Metadata [228 B]
Get:17 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [949 kB]
Get:18 http://archive.ubuntu.com/ubuntu jammy-updates/main Translation-en [205 kB]
Get:19 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 c-n-f Metadata [13.8 kB]
Get:20 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [895 kB]
Get:21 http://archive.ubuntu.com/ubuntu jammy-updates/universe Translation-en [179 kB]
Get:22 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 c-n-f Metadata [18.4 kB]
Get:23 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [19.5 kB]
Get:24 http://archive.ubuntu.com/ubuntu jammy-backports/universe Translation-en [14.0 kB]
Get:25 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 c-n-f Metadata [392 B]
Fetched 5086 kB in 3s (1509 kB/s)
Reading package lists... Done
## Confirming "jammy" is supported...
+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_19.x/dists/jammy/Release'
## Adding the NodeSource signing key to your keyring...
+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null
## Creating apt sources list file for the NodeSource Node.js 19.x repo...
+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_19.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_19.x jammy main' >> /etc/apt/sources.list.d/nodesource.list
## Running `apt-get update` for you...
+ apt-get update
Get:1 https://deb.nodesource.com/node_19.x jammy InRelease [4563 B]
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 https://packages.microsoft.com/repos/azure-cli jammy InRelease
Hit:6 http://security.ubuntu.com/ubuntu jammy-security InRelease
Get:7 https://deb.nodesource.com/node_19.x jammy/main amd64 Packages [778 B]
Fetched 5341 B in 3s (1782 B/s)
Reading package lists... Done
## Run `sudo apt-get install -y nodejs` to install Node.js 19.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
We do what they say by running:
sudo apt-get install -y nodejs
And as it informs us, we install possibly necessary packages:
sudo apt-get install gcc g++ make
And we install Yarn by running:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
Once we finish this installation we verify by executing:
node -v
which returns that I have a version 19.8.1, and problem solved.