'Tried npm install -g truffle but getting this gyp error

I tried npm node-gyp -g but it didn't help. I am trying to install truffle in my mac. I am using mac os Catalina, node version 16.13.1, and npm version 8.1.2

npm ERR! code 1
npm ERR! path /usr/local/lib/node_modules/truffle/node_modules/ganache/node_modules/leveldown
npm ERR! command failed
npm ERR! command sh -c node-gyp rebuild
npm ERR! CXX(target) Release/obj.target/leveldb/deps/leveldb/leveldb-1.20/db/builder.o
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | x64
npm ERR! gyp info find Python using Python version 3.8.1 found at "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3"
npm ERR! gyp info spawn /Library/Frameworks/Python.framework/Versions/3.8/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/truffle/node_modules/ganache/node_modules/leveldown/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-/c++/v1/string:505:
...
npm ERR! gyp ERR! System Darwin 19.6.0
npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /usr/local/lib/node_modules/truffle/node_modules/ganache/node_modules/leveldown
npm ERR! gyp ERR! node -v v16.13.1
npm ERR! gyp ERR! node-gyp -v v8.3.0
npm ERR! gyp ERR! not ok


Solution 1:[1]

Refer to https://github.com/trufflesuite/truffle/issues/4692 , So for workaround we have to...

For non-nvm

# Install npm 6
npm i -g npm@6

# Make sure should get npm `6.14.15`
npm --version

# Should work now
npm i -g truffle

For nvm

# In case you have 16 installed (you can skip this)
npm config delete prefix

# Get npm 6.14.16 from node 14 (or you can try install only npm)
nvm use 14

# Make sure should get npm `6.14.15`
npm --version

# Should work now
npm i -g truffle

Solution 2:[2]

Above answer did not work for me

Try this :

sudo npm install -g truffle --unsafe-perm

Source : https://github.com/nodejs/node-gyp/issues/2631

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 Mehdi LAMRANI