Monthly Archives: February 2013

How to install Google Chrome in Ubuntu

First add the signing key and the repository:

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update

Then to install the stable release:

sudo apt-get install google-chrome-stable

If you want to instal the unstable release:

sudo apt-get install google-chrome-unstable

And for the beta version:

sudo apt-get install google-chrome-beta

To uninstall Chromium:

sudo apt-get remove chromium-browser

Source: http://www.howopensource.com/2011/10/install-google-chrome-in-ubuntu-11-10-11-04-10-10-10-04/

How to add and delete package signing keys of apt in Ubuntu

To install a package signing key you have to use the apt-key add command. For example if you want to add the Google signing key:

wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

To remove a key you first need to know its keyid. You use apt-key list for this:

sudo apt-key list
...
pub   1024D/7FAC5991 2007-03-08
uid                  Google, Inc. Linux Package Signing Key 
sub   2048g/C07CB649 2007-03-08

Once you know the keyid you delete it with this command:

sudo apt-key del 7FAC5991

Source: Askubuntu

Run a command from a Java application dealing properly with stdin, stdout and stderr

If you want to run an external command from a Java application you have to deal properly with the input, output and error file descriptors or it won’t work. The key is to read the output and error buffers. This is a simple example on how to do this:

import java.io.*;

public class CmdExec {

  public static void main(String argv[]) {
    try {
      String line;
      OutputStream stdin = null;
      InputStream stderr = null;
      InputStream stdout = null;

      // launch the command and grab stdin/stdout and stderr
      Process process = Runtime.getRuntime().exec("ls -la");
      stdin = process.getOutputStream();
      stderr = process.getErrorStream();
      stdout = process.getInputStream();

      // You could write to sdtin too but it's useless for the ls we are doing ;)
      line = "param1" + "\n";   
      stdin.write(line.getBytes() );
      stdin.flush();

      line = "param2" + "\n";
      stdin.write(line.getBytes() );
      stdin.flush();

      stdin.close();
      
      // clean up if any output in stdout
      BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(stdout));
      while ((line = brCleanUp.readLine()) != null) {
        System.out.println ("[Stdout] " + line);
      }
      brCleanUp.close();
      
      // clean up if any output in stderr
      brCleanUp = 
        new BufferedReader (new InputStreamReader (stderr));
      while ((line = brCleanUp.readLine ()) != null) {
        //System.out.println ("[Stderr] " + line);
      }
      brCleanUp.close();
    } catch (Exception err) {
      err.printStackTrace();
    }
  }
}

Install and configure MongoDB and PHP driver with MacPorts

Installing and configuring MongoDB with MacPorts

Installing MongoDB with MacPorts is as easy as running this command:

$ sudo port install mongodb

MongoDB uses /data/db as the default path for storing its data files, but it’s likely that your won’t have that folder and running MongoDB server (mongod) will end with this error:

*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************

There are several options:

Create that folder and configure it with the correct permissions:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

Supply another path as the data storage folder with the --dbpath option:

$ mongodb --dbpath /var/lib/mongo

And the best of all, create the necessary paths for data and log files and also the configuration file to specify those paths:

$ sudo mkdir /opt/local/var/db/mongodb
$ sudo mkdir /opt/local/var/log/mongodb
$ sudo touch /opt/local/var/log/mongodb/mongodb.log
$ sudo mkdir /opt/local/etc/mongodb/

Add this to /opt/local/etc/mongodb/mongodb.conf file:

# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/var/db/mongodb

# Only accept local connections
bind_ip = 127.0.0.1

# Running as daemon
fork = true

# Take log
logpath = /opt/local/var/log/mongodb/mongodb.log
logappend = true

You can then run mongod specifying that configuration file:

$ sudo mongod -f /opt/local/etc/mongodb/mongodb.conf

An easier way is to use the load/unload option that comes with MacPorts that defines all those configuration values in /opt/local/etc/LaunchDaemons/org.macports.mongodb/org.macports.mongodb.plist file. You can load and unload MongoDB with those commands (using them will make MongoDB autoload on boot!):

$ sudo port load mongodb
$ sudo port unload mongodb

More configuration options: http://docs.mongodb.org/manual/reference/configuration-options/

Installing the PHP driver for MongoDB

Before installing the driver we need to be sure that we have pear and pecl installed. Run this commands to see if they return some value:

$ pear version
$ pecl version

Now we have to install the PHP driver with pecl:

$ sudo pecl install mongo

When it finishes the installation you will see something like this:

Build process completed successfully
Installing '/opt/local/lib/php/extensions/no-debug-non-zts-20090626/mongo.so'
install ok: channel://pecl.php.net/mongo-1.3.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

That last line means that you have to edit yourself the php.ini file, which in my case is in /opt/local/etc/php5/php.ini, and add the extension load for mongo. You can add that line at the end of the file but it is supposed to be in the “Dynamic Extensions”.

Now restart Apache and you’ll be ready to use MongoDB in PHP.

$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start

Ref: http://api.mongodb.org/wiki/current/PHP%20Language%20Center.html
http://noteit.jiaeil.com/install-mongodb-via-macports-on-mac
http://stackoverflow.com/questions/8029064/new-to-mongodb-can-not-run-command-mongo

MacPorts: Update port definitions and upgrade installed ports

Once in a while is good to update your software to get advantage of the latest bug fixes and new features. This is very easy to do with MacPorts:

$ sudo port selfupdate
$ sudo port upgrade outdated

You can then remove all the non active ports:

$ sudo port uninstall inactive

And clean all the files used to build the ports:

$ sudo port clean --all all