How to auto enable nvm / node / pm2 / yarn / npm for new users

feerdispzoo

Verified User
Joined
Jan 17, 2022
Messages
189
Currently I must everytime manual install above apps for new users. I try install also this in root, for root working but when I switch to user level still return not found.

How can I set this to apply to all new users without having to install it every time?
 
just use hooks "user_create_post_confirmed" and make script to installed on their HOME_DIR.
 
@jamgames2 please check where is issue:

I try also in this way and create script hook for create_database_post.sh

Code:
#!/bin/bash


#  'username'
username=${username}
# Install NVM as the specified user
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash


# Install Node.js version 18 using NVM
nvm install 18


# Install PM2 globally
npm install -g pm2


# Install Yarn globally
npm install -g yarn

This above bash script install but as ROOT user, and when I switch to user level then still I cant access.

So I try directly in bash script su to username and install:
Code:
#!/bin/bash


#  'username'
username=${username}
# Install NVM as the specified user
sudo -u $username bash -c 'curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash'

# Reload the terminal to apply NVM changes
#exec bash

# Install Node.js version 18 using NVM
sudo -u $username bash -c 'nvm install 18'

# Install PM2 globally
sudo -u $username bash -c 'npm install -g pm2'

# Install Yarn globally
sudo -u $username bash -c 'npm install -g yarn'

This above install correct NVM for user, when I switch to user i can check nvm -v, but the problem is this nvm require reload console to finish install nvm, pm2 and yarn. I try add after line with curl I try add
# Reload the terminal to apply NVM changes
#exec bash

but then script not working.
 
@jamgames2 please check where is issue:

I try also in this way and create script hook for create_database_post.sh

Code:
#!/bin/bash


#  'username'
username=${username}
# Install NVM as the specified user
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash


# Install Node.js version 18 using NVM
nvm install 18


# Install PM2 globally
npm install -g pm2


# Install Yarn globally
npm install -g yarn

This above bash script install but as ROOT user, and when I switch to user level then still I cant access.

So I try directly in bash script su to username and install:
Code:
#!/bin/bash


#  'username'
username=${username}
# Install NVM as the specified user
sudo -u $username bash -c 'curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash'

# Reload the terminal to apply NVM changes
#exec bash

# Install Node.js version 18 using NVM
sudo -u $username bash -c 'nvm install 18'

# Install PM2 globally
sudo -u $username bash -c 'npm install -g pm2'

# Install Yarn globally
sudo -u $username bash -c 'npm install -g yarn'
This above install correct NVM for user, when I switch to user i can check nvm -v, but the problem is this nvm require reload console to finish install node, pm2 and yarn. I try add after line with curl I try add
# Reload the terminal to apply NVM changes
#exec bash

but then script not working.
 
Code:
#!/bin/bash

#print HOME DIR
sudo -i -H -u $username bash -c 'echo $HOME'

#  'username'
username=${username}
# Install NVM as the specified user
sudo -i -H -u $username bash -c 'curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash'

# Install Node.js version 18 using NVM
sudo -i -H -u $username nvm install 18

# Install PM2 globally
sudo -i -H -u $username npm install -g pm2

# Install Yarn globally
sudo -i -H -u $username npm install -g yarn

Doesn't work ?
 
Last edited:
Above coding, it required User to have SSH Access.


Another code without required SSH Access.
Code:
#!/bin/bash

#print HOME DIR
sudo -s -H -u $username bash -c 'echo $HOME'

#  'username'
username=${username}
# Install NVM as the specified user
sudo -s -H -u $username bash -c 'curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash'

# Install Node.js version 18 using NVM
sudo -s -H -u $username bash -c 'source ~/.nvm/nvm.sh; nvm install 18'

# Install PM2 globally
sudo -s -H -u $username npm install -g pm2

# Install Yarn globally
sudo -s -H -u $username npm install -g yarn


I change "-i" flag into "-s" flag.
 
Code:
#!/bin/bash

#print HOME DIR
sudo -i -H -u $username bash -c 'echo $HOME'

#  'username'
username=${username}
# Install NVM as the specified user
sudo -i -H -u $username bash -c 'curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash'

# Install Node.js version 18 using NVM
sudo -i -H -u $username nvm install 18

# Install PM2 globally
sudo -i -H -u $username npm install -g pm2

# Install Yarn globally
sudo -i -H -u $username npm install -g yarn

Doesn't work ?
@jamgames2 thank you. This work correct!
 
Back
Top