Install Latest MongoDB in Ubuntu

A couple projects I work on use MongoDB as the database, and I’m generally not satisfied to use the (often outdated) version that ships with Ubuntu.

As a result, I wrote this script to automate fetching, extracting and linking the latest version.

To configure the script, just replace the PKG information with whatever value is most relevant for your configuration at http://www.mongodb.org/downloads.

#!/bin/bash

PKG=mongodb-linux-x86_64-2.2.0 URL=http://fastdl.mongodb.org/linux

if [ -f /etc/init.d/mongod ] then echo "mongod already installed … stopping" sudo service mongod stop else echo "installing mongod script" wget https://raw.github.com/gist/3516078/713ad981715236473999f30636bca2ee3dcc5f24/mongod sudo mv mongod /etc/init.d/mongod sudo chmod +x /etc/init.d/mongod echo "creating mongodb user" sudo useradd mongodb echo "creating directories" sudo mkdir -p /var/lib/mongodb sudo mkdir -p /var/log/mongodb sudo chown mongodb.mongodb /var/lib/mongodb sudo chown mongodb.mongodb /var/log/mongodb sudo update-rc.d mongod defaults fi

wget $URL/$PKG.tgz sudo tar -xvzf $PKG.tgz -C /usr/local/

if [ -d /usr/local/mongodb ] then echo "removing existing mongodb symlink" sudo rm -rf /usr/local/mongodb sudo ln -s /usr/local/$PKG /usr/local/mongodb else echo "creating symlinks" sudo ln -s /usr/local/$PKG /usr/local/mongodb sudo ln -s /usr/local/mongodb/bin/bsondump /usr/local/bin/bsondump sudo ln -s /usr/local/mongodb/bin/mongo /usr/local/bin/mongo sudo ln -s /usr/local/mongodb/bin/mongod /usr/local/bin/mongod sudo ln -s /usr/local/mongodb/bin/mongodump /usr/local/bin/mongodump sudo ln -s /usr/local/mongodb/bin/mongoexport /usr/local/bin/mongoexport sudo ln -s /usr/local/mongodb/bin/mongofiles /usr/local/bin/mongofiles sudo ln -s /usr/local/mongodb/bin/mongoimport /usr/local/bin/mongoimport sudo ln -s /usr/local/mongodb/bin/mongorestore /usr/local/bin/mongorestore sudo ln -s /usr/local/mongodb/bin/mongos /usr/local/bin/mongos sudo ln -s /usr/local/mongodb/bin/mongosniff /usr/local/bin/mongosniff sudo ln -s /usr/local/mongodb/bin/mongostat /usr/local/bin/mongostat fi

echo "cleaning up" rm $PKG.tgz

echo "starting mongod service" sudo service mongod start

Note that this script pulls mongod from a gist which I created. This script is originally from Ijonas Kisselbach’s dotfiles.

Comments