10

Unfortunately the official Ubuntu package for MongoDB is still stuck on 2.6, which is ancient at this point (this bug didn't get picked up for vivid, wily or xenial).

MongoDB has not yet released a package for 16.04 (Xenial), so how can I use the 14.04 (Trusty) packages to install MongoDB 3.2+ on 16.04?

  • Yes you can. If you're upgrading, it should be easy. If you fresh installed 16.04, simply follow the steps here: askubuntu.com/questions/724749/… – Rexford Apr 17 '16 at 12:44
  • @Rexford As you'll see in the answer below it's not quite that easy in 16.04 with the old packages (the service won't get installed properly with a fresh install) – Adam C Jun 10 '16 at 14:01
14

It's a bit of a hack but yes, this can be done. There is an undocumented step needed to actually start the service and (weirdly enough) unless you want to futz around with upstart job conversion manually, you need to install the Ubuntu mongodb package first, then remove it so that you have a usable service. A straight install of the MongoDB packages will not give you a working service on a fresh 16.04.

Update - July 2016: The official fix for this (adding support for 16.04 to the packages) has been completed in the 3.3 dev branch as part of SERVER-23043 - and has been released as a backport in the 3.2 series as part of 3.2.7. Hence, if you are using 3.2.7+ you do not need to do this and the official MongoDB package install should just work.

In any case, after doing the add/remove, follow the usual instructions for 14.04, summarized here:

# install the Ubuntu package (to get the service set up correctly)
sudo apt-get install mongodb
# Now remove it and continue with the MongoDB instructions
sudo apt-get remove mongodb
sudo apt-get autoremove
# import key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
# add trusty repos
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# update apt
sudo apt-get update
# install the MongoDB package
sudo apt-get install -y mongodb-org

The install succeeds, and thanks to the Ubuntu package we have all the pieces needed for a service, but attempting to start the service will initially fail:

$ sudo service mongodb start
Failed to start mongodb.service: Unit mongodb.service is masked.

Fixing this is not too hard:

$ sudo systemctl unmask mongodb
Removed symlink /etc/systemd/system/mongodb.service.

Now we can enable/start the service, get its status etc.

$ sudo service mongodb enable
$ sudo service mongodb start
$ sudo service mongodb status
● mongodb.service - LSB: An object/document-oriented database
   Loaded: loaded (/etc/init.d/mongodb; bad; vendor preset: enabled)
   Active: active (running) since Thu 2016-04-14 16:40:35 IST; 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2849 ExecStart=/etc/init.d/mongodb start (code=exited, status=0/SUCCESS)
 Main PID: 1593 (code=exited, status=0/SUCCESS)
    Tasks: 13 (limit: 512)
   Memory: 51.9M
      CPU: 100ms
   CGroup: /system.slice/mongodb.service
           └─2861 /usr/bin/mongod --config /etc/mongodb.conf

Success - and we get a nice pointer to the active config file (which is in the old key=value format, see here for examples in the newer recommended YAML format).

Note: There will actually be two config files /etc/mongod.conf and /etc/mongodb.conf thanks to slight differences between the two packages we have installed. The now-active service points to the old-format one by default /etc/mongodb.conf - you can switch by moving/copying the mongod.conf to mongodb.conf if you so wish, or simply edit the file directly.

Once started, to test, connect to the database with the mongo shell:

$ mongo
MongoDB shell version: 3.2.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] 
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] 
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] 
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] 
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-14T16:40:34.962+0100 I CONTROL  [initandlisten] 

If you wish to get rid of the THP warnings, check out this answer.

  • 1
    After your first block of code above, running sudo service mongodb start returns Failed to start mongodb.service: Unit mongodb.service not found. – user1063287 Apr 21 '16 at 6:07
  • 1
    I tried to resolve the above by creating a service file for systemd (as per askubuntu.com/a/694226/367134). This is the result: sudo service mongodb start returns nothing. sudo service mongodb status returns a failed message with status=2. To fix this, I changed path in newly created service file from /etc/mongodb.conf to /etc/mongod.conf. Building on your original answer, how do you get mongodb to start on system start up? – user1063287 Apr 21 '16 at 7:01
  • 1
    The following additional action enabled mongodb to start on system startup for me: sudo systemctl enable mongodb. See: digitalocean.com/community/tutorials/… – user1063287 Apr 21 '16 at 7:22
  • 1
    Ah, pretty sure I just left that line out when copy/pasting my instructions, or I would have failed similarly, thanks for the heads up – Adam C Apr 21 '16 at 10:14
  • 1
    Definitely not fixed in 3.2.8. See the last comment. Should be tackled in 3.2.9. – Goran Miskovic Jul 30 '16 at 8:46
-1

If you have already installed it, remove mongodb by:

sudo apt-get remove mongodb
sudo apt-get autoremove

Try with recommendation on digitalocean https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04

  • 3
    While your answer may be valid, it would be better to provide the steps listed in your link here. Links and their content can change over time which would render your answer useless. – Videonauth May 17 '16 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.