I you're running Truffle on Linux or MacOS X, you should not install it with sudo, otherwise you might encounter some permission issues. It is recommended to install truffle as a normal user. Before we can install truffle as a normal user we have to cleanup the system. It is also recommended to install nodejs and npm with nvm (node version manager). An advantage of nvm is that you can run different node versions on the same machine.
First we want to find out which packages have been installed with sudo npm install -g
.
Get a list of all installed packages:
$ sudo npm list -g --depth=0
/usr/lib
|[email protected]
|[email protected]
[email protected]
Now we want to remove them
$ sudo npm remove -g anache-cli
$ sudo npm remove -g truffle
$ sudo npm remove -g npm
If you have additional packages in the tree, you can also remove them.
If you installed node with a the package manager of your Linux distro you can delete the package and skip Remove node and Remove npm. If you installed it manually you have to delete it manually.
We have to find out where node is installed before we can delete it.
$ whereis node
in my case I get the following output
node: /usr/bin/node /usr/share/man/man1/node.1.gz
find the location of node_modules node and delete it
$ sudo rm /usr/bin/node /usr/share/man/man1/node.1.gz
We have to find out where npm is installed before we can delete it.
$ sudo whereis npm
in my case I get the follwing output:
npm: /usr/bin/npm /usr/share/man/man1/npm.1.gz /usr/share/man/man1/npm.1
$ sudo ls -l /usr/bin/npm
lrwxrwxrwx 1 root root 38 Feb 4 08:47 /usr/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
$ sudo rm -rf /usr/bin/npm /usr/lib/node_modules/npm/bin/npm
$ sudo rm -rf /usr/share/man/man1/npm.1.gz /usr/share/man/man1/npm.1
Depending on the linux distribution you might have different paths
Now that we have cleaned up everything we can start with the installation of nvm and node
See: https://github.com/creationix/nvm
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
In order to load the new environment you have to close your console and open a new one
$ nvm install node # "node" is an alias for the latest version
$ npm install -g npm
See: https://stackoverflow.com/questions/34718528/nvm-is-not-compatible-with-the-npm-config-prefix-option
$ npm config delete prefix
$ npm config set prefix $NVM_DIR/versions/node/v11.10.0
$ npm install -g truffle
$ whereis truffle
If everything went as expected truffle should be installed in:
~/.nvm/versions/node/v11.10.0/bin/truffle
Install the other packages which you had installed as sudo, such as ganache-cli or create-react-app.