MongoDB installation macOS Catalina - node.js

I recently started developing with NodeJS. In the course I attend, they gave me steps to install on Mac but it's giving me problems. After a some googling, I understood that macOS Catalina has blocked writing access to the root folder. After that, I spent a day searching on Google and Stack Overflow for solutions to this problem. There were many different solutions, but none of them worked. Some installed, but the mongod command wouldn't work. Even the installation process on MongoDB itself didn't work.
At the end, I decided to use MongoDB Atlas. So in my cluster0, I went to Connect > Connect with mongo shell > I do not have MongoDB installed. They gave some steps to install it with Homebrew. Now, when I type mongo "mongodb+srv://cluster0.hflrg.mongodb.net/<dbname>" --username <myUserName>, it connects to MongoDB Atlas. The mongod command still doesn't work.
Can you tell me a way to install MongoDB on Mac. I have macOS Catalina Version 10.15.5.
Note: I don't have much experience with the terminal, so if possible, please avoid using terms like "Add so and so the you $PATH variable", and try to explain in detail. A huge thanks in advance.

I usually and like to install by downloading .tgz package from MongoDB Community Server, and setting up via command lines.
Download .tgz package from MongoDB Community Server.
Extract the files from the downloaded archive:
tar -zxvf mongodb-macos-*.tgz
Copy binaries in your PATH environment variable:
sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/
Create the data directory:
sudo mkdir -p /usr/local/var/mongodb
IMPORTANT
Starting with macOS 10.15 Catalina, Apple restricts access to the MongoDB default data directory of /data/db. On macOS 10.15 Catalina, you must use a different data directory, such as /usr/local/var/mongodb.
Create the log directory:
sudo mkdir -p /usr/local/var/log/mongodb
Run MongoDB:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log
To run mongod in the background, use --fork at the end of command line:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
Helpful links and references:
Installing MongoDB on macOS tarball
MongoDB on macOS Catalina v10.15+
Hope these may help! Have a great one!

Use Homebrew, it uses /usr/local by default.
brew tap mongodb/brew
brew install mongodb/brew/mongodb-community
brew services start mongodb/brew/mongodb-community
mongo --quiet --eval 'db.version()'
4.2.8

This worked for me, but is based on the answer by #Erni_Souza
Download the .tgz package from here MongoDB Community Server.
Extract the files on your machine using tar -zxvf mongodb-macos-*.tgz or just use the default Archive Utility.app on your machine.
Copy the binaries into your /usr/local/bin folder with:
sudo cp /path/to/the/mongodb-directory/bin/* /usr/local/bin/ just as instructed or you can resort to using the ./mongod command via Terminal.app from the folder you extracted into.
Create the data directory mkdir -p ~/data/db
Run mongodb
mongod --dbpath ~/data/db
or with
./mongod --dbpath ~/data/db
if the binaries are still in the directory you extracted them to.
You can check out this link MongoDB on macOS Catalina v10.15+ as given in the answer by #Erni_Souza for more information.

Related

In WSL2: Ubuntu 20.04 for Windows 10 nodejs is installed but npm is not working

I am using WSL2: Ubuntu 20.04 in my Windows 10 operating system. I have installed nodejs using the command sudo apt-get install -y nodejs when I do node -v command I get v12.18.3
mrd#DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ node -v
v12.18.3
but when I do npm -v command I get this below command
mrd#DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ npm -v
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
I also do whereis command. Hope this will help to find solution.
mrd#DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ whereis node
node: /usr/bin/node /usr/include/node /mnt/c/Program Files/nodejs/node.exe /usr/share/man/man1/node.1.gz
mrd#DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ whereis npm
npm: /usr/bin/npm /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd /usr/share/man/man1/npm.1
I have tried almost all the stackoverflow solutions and github issues but nothing is worked for me.
Hope any kind soul has the solution to this problem. Thanks in advance.
Try this
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sudo apt install npm
Solution for following error:
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
Edit ~/.bashrc
Append at end of file:
# strip out problematic Windows %PATH%
PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g')
Now npm init will work.
A better way is configuring /etc/wsl.conf in your Windows User directory.
Adding this into the /etc/wsl.conf, so Windows Path will not take the precedence
[interop]
appendWindowsPath=false
For more config details check the Microsoft Dev Blog here.
To install nodejs in WSL don't use apt follow Microsoft's guidance:
https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl
See also how to remove nodejs if you installed it via apt:
https://askubuntu.com/questions/786015/how-to-remove-nodejs-from-ubuntu-16-04
For npm to work under WSL1:
You may also need to disable ipv6 (or configure to prefer ipv4) - if you are hitting these issues.
Under WSL1 I disabled ipv6 on my main NIC & npm install immediately began working.
WSL2 Notes:
NB: if you use a VPN your container connectivity may be broken under WSL2 (e.g with Cisco AnyConnect) - the fix works but may no longer be needed under AnyConnect (WSL2 on a VPN now works for me after a recent update # end of July 2022)
I thought my WSL containers were running under WSL2 (I upgraded the WSL kernel with wsl --update) - while setting up Visual Studio with WSL I saw a WSL1 warning. You also have to upgrade containers:
wsl --set-version ubuntu-22.04 2
wsl --set-default-version 2
To get Visual Studio integration working properly with Ubuntu 22.04 in WSL you also currently have to upgrade gzip to install VS Code Server for x64 in WSL (code .: in the Linux terminal):
wget http://ftp.debian.org/debian/pool/main/g/gzip/gzip_1.12-1_amd64.deb
sudo dpkg -i ./gzip_1.12-1_amd64.deb
Finally I upgraded npm & everything works (choose one of the following commands):
nvm install-latest-npm
npm install -g npm#latest
Azure AD / CLI Notes
If you use nodejs with Azure Active Directory there seems to be an issue with the azure-cli forgetting credentials under WSL1 / WSL2 & persistently telling you to az login. In this case you need to run your local node development instances on Windows.
For all Unix/Linux/MacOS operating systems, I would always rather go with the "Node Version Manager". It normally works flawlessly on Linux and MacOS (and there's a Windows port for it as well) and enables a very simple way of installing node and npm correctly without the need of being root.
See here: https://github.com/nvm-sh/nvm
I can confirm here on my machine that it also works on Ubuntu 20.04 on WSL2.

sudo: npm: command not found in WSL

I'm trying to install globally some packages for my Unix environment on Windows with WSL. I use nvm to manage the different versions of Node.js.
The problem is while using the sudo command before a global npm install :
sudo npm install --global prompt-pure
I get an error: sudo: npm: command not found !
Doing a simple npm install --global pure-prompt will work, but as I'm not super user, the global installation ends up with a permission error.
How can I fix this annoying problem and keep nvm ?
Thanks by advance
When you try to run sudo npm, it tries to run the npm binary file /usr/bin/npm but your binary is located in a different place, which can be found running which npm.
Example: /home/damo/.nvm/versions/node/v8.11.1/bin/npm
The solution is to create a link in /usr/bin/ pointing to the actual binary:
sudo ln -s "$(which npm)" /usr/bin/npm
You can also add the following link so you can run sudo node
sudo ln -s "$(which node)" /usr/bin/node
For me I needed to actually cheat and run as root before installing node as root. For this I ran sudo su in a Ubuntu WSL term and then installed node.
Once I did that I could sudo su then npm run special-script.
I don't know a better way to get a script to attach to restricted ports like 443 for testing https connections but it works.
As you will find in the man file for sudo man sudo sudo will execute a command as another user. That other user has a different home directory to you and access to different commands
When i run sudo which node i get nothing, but which node returns /home/damo/.nvm/versions/node/v8.11.1/bin/node
Lets look at your actual goal. You say you are trying to install pure-prompt, i know this does not ask your explicit question but given you have zsh installed have you tried oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) i use this on every install of linux i ever have to work with (VMs, WSL, docker). Very customizable and looks great out of the box.

Installing MongoDB on a Raspberry Pi - -bash: mongo: command not found

I'm using a tutorial to install MongoDB on the raspberry pi: http://notejs.com/?p=252
I have followed the instructions with complete success up till the line:
Connect to an instance:
mongo
I then get an error like this:
-bash: mongo: command not found
I have followed this tutorial step by step, the only difference is that I downloaded, compiled and installed MongoDB in a folder that isn't the home folder, could this be the problem?
edit: MongoDB is apparently running already when I enter the command:
sudo /etc/init.d/mongod start
So it's not the case that it's too much for my PI
The instructions have this section for making the mongod binary part of your path, which is why that is working:
sudo ln -s /opt/mongo/bin/mongod /usr/bin/mongod
But they do not do the same for the mongo shell binary. Hence you just need to do the same thing for the that binary, that is:
sudo ln -s /opt/mongo/bin/mongo /usr/bin/mongo
Alternatively you could add /opt/mongo/bin to your $PATH variable, or just call /opt/mongo/bin/mongodirectly.

MongoDB - Xubuntu crouton - can't start service

I've tried following the installation instructions laid out here: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
But I run into a problem with, I'm guessing, Upstart/initctl and I can't get the mongod service to start other than by running it manually as sudo in a terminal, which is a Bad Idea IMO. (Even if this is merely for a classroom, single-user setup.)
sudo /etc/init.d/mongod start
Yields this error:
initctl: Unknown job: mongod
I've tried sudo initctl reload-configuration and running the above start command again, to no avail.
Any ideas or suggestions on what to do next?
Installing mongodb using what the official documentation doesn't seem to work in crouton. Below are the instructions I followed to get it working on my machine (crouton running elementaryOS)
Download the physical mongodb package for linux (can be obtained from mongodb.org)
Extract the mongodb package using tar -xvzf mongodb-linux-x86_64-2.6.7.tgz
Make the installation directory (sudo mkdir /usr/lib/mongodb)
Copy the extracted mongodb into to /usr/lib/mongodb (sudo mv mongodb-linux-x86_64-2.6.7 /usr/lib/mongodb)
Make the data directory (sudo mkdir -p /data/db)
Start mongodb (sudo /usr/lib/mongodb/mongodb-linux-x84_64-2.6.7/bin/mongod &)

Postgresql -bash: psql: command not found

I have installed PostgreSQL and it is working ok. However, when I went to restore a backup I got the error -bash: psql: command not found:
[root#server1 ~]# su postgres
[postgres#server1 root]$ psql -f all.sql
bash: psql: command not found
[postgres#server1 root]$
What have I done wrong?
export PATH=/usr/pgsql-9.2/bin:$PATH
The program executable psql is in the directory /usr/pgsql-9.2/bin, and that directory is not included in the path by default, so we have to tell our shell (terminal) program where to find psql. When most packages are installed, they are added to an existing path, such as /usr/local/bin, but not this program.
So we have to add the program's path to the shell PATH variable if we do not want to have to type the complete path to the program every time we execute it.
This line should typically be added to theshell startup script, which for the bash shell will be in the file ~/.bashrc.
perhaps psql isn't in the PATH of the postgres user. Use the locate command to find where psql is and ensure that it's path is in the PATH for the postgres user.
The question is for linux but I had the same issue with git bash on my Windows machine.
My pqsql is installed here:
C:\Program Files\PostgreSQL\10\bin\psql.exe
You can add the location of psql.exe to your Path environment variable as
described in this other answer, and shown in the screenshot below:
After changing the above, please close all cmd and/or bash windows, and re-open them (as mentioned in the comments #Ayush Shankar). If you are using an IDE like Visual Studio Code, please close and re-open the entire IDE (as mentioned in the comments #Somraj Chowdhury)
You might need to change default logging user using below command.
psql -U postgres
Here postgres is the username. Without -U, it will pick the windows loggedin user.
It can be due to psql not being in PATH
$ locate psql
/usr/lib/postgresql/9.6/bin/psql
Then create a link in /usr/bin
ln -s /usr/lib/postgresql/9.6/bin/psql /usr/bin/psql
Then try to execute psql it should work.
In case you are running it on Fedora or CentOS, this is what worked for me (PostgreSQL 9.6):
In terminal:
$ sudo visudo -f /etc/sudoers
modify the following text from:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
to
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.6/bin
exit, then:
$ printenv PATH
$ sudo su postgres
$ psql
To exit postgreSQL terminal, you need to digit:
$ \q
Source: https://serverfault.com/questions/541847/why-doesnt-sudo-know-where-psql-is#comment623883_541880
If you are using the Postgres Mac app (by Heroku) and Bundler, you can add the pg_config directly inside the app, to your bundle.
bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
...then run bundle again.
Note: check the version first using the following.
ls /Applications/Postgres.app/Contents/Versions/
Check if PostgreSQL is installed or not.
If not you can do the same in ubuntu using this command.
sudo apt update
sudo apt install postgresql postgresql-contrib
there must be two reasons for this either the package is not install or psql is not defined in the PATH
the simple way is to create a link within the /usr/bin or /usr/local/sbin/
First find the the file
sudo find / -name psql
then create soft link
sudo ln -sf /opt/pgpro/1c-14/bin/psql /usr/local/sbin/psql
Sometimes we face this issue when the gem installation command doesn't find the pg client library for various reasons, such as if psql is not in the path.
In those cases, providing the command with the path to pg_config may fix the issue.
gem install pg -v 1.3.5 -- --with-pg-config=/path/to/pg_config
In my case, I faced a similar issue when I installed postgresql#12 with Homebrew in the Rosetta environment.
Following command solved the issue in my case.
gem install pg -v 1.3.5 -- --with-pg-config=/usr/local/Homebrew/Cellar/postgresql#12/12.13/bin/pg_config

Resources