Wednesday, October 28, 2009

Sync Firefox profiles across computers with Dropbox

Dropbox is a fantastic service that will sync data between multiple computers and keep it backed up on their server. Since I use multiple computers on an almost daily basis I love Dropbox because I can work on the same files on either computer and they remain synced. I recently started working on a a project involving Mozilla Firefox. I wanted to created a second Firefox profile to keep my project preferences, add-ons, and history separate from my regular profile while at the same time being synced between my laptop and desktop.

I started on my desktop, which runs Ubuntu Linux. In order to create a new Firefox profile without damaging the current one you have to start the Firefox Profile Manager:
$ /usr/bin/firefox --profilemanager
If you are working on a Mac you can start it like this:
$ open -a firefox --args --profilemanager
If you are working in Windows you can start it like this:
c:\>"Program Files"\"Mozilla Firefox"\firefox.exe --profilemanager
The Profile Manager is a GUI tool so once you create a new profile, I called mine "dev", just click the exit button. Firefox will still start with the default profile. In order to use the new profile you need to throw an argument to the Firefox binary to tell it which profile you want to use. In Linux, to load up a new instance of Firefox using your new profile called "dev", even if one is already running, run this command:
$ /usr/bin/firefox -no-remote -P dev
Doing this, however, will keep that instance of Firefox tied to your terminal. To get around this I created a new launcher that sits on my panel at the top of my screen. To do that, right-click the panel, choose "Add to Panel...", then "Custom Application Launcher". Fill out the box that comes up so it looks like this:

On a Mac, to open Firefox with a new profile its easiest to run this command from the Terminal:
$ open -a firefox --args -no-remote -P dev
On Windows it would look like this:
c:\>"Program Files"\"Mozilla Firefox"\firefox.exe -no-remote -P dev
In Windows you can also create a shortcut to firefox and add the arguments at the end of the path in the properties window so it looks like this:


The way I got syncing to work uses symbolic links. I've written about Unix symbolic links before and they rock. After I made sure the new Firefox profile worked on my Linux desktop shutdown all instances of Firefox. In Linux, your Firefox profiles reside in this folder:
~/.mozilla/firefox/
Each profile has a unique name which follows a pattern of 8 seemingly random alphanumeric characters, followed by a period (.), followed by the name of the profile. If you have two Firefox profiles whose names are "default" and "dev" their names might look like this, respectively: "5e97feg7.default" and "ou4e9v2y.dev". To continue with those examples, I moved the "dev" profile to my Dropbox folder, located at ~/Dropbox/:
$ mv ~/.mozilla/firefox/ou4e9v2y.dev ~/Dropbox/
Then I created a symbolic link from the new location going back to the old one:
$ ln -s ~/Dropbox/ou4e9v2y.dev ~/.mozilla/firefox/
Now, if you did everything correctly, you should be able to open Firefox using your new profile, the one you just moved, and it should work like before. Next I moved to my Mac to get everything setup there. I created a symbolic link from the profile in my Dropbox directory into the directory where Firefox profiles live, located here:
~/Library/Application Support/Firefox/Profiles/
To create the new symbolic link:
$ ln -s ~/Dropbox/ou4e9v2y.dev ~/Library/Application Support/Firefox/Profiles/
Now you need to edit the config file that tells Firefox about the new profile, located here:
~/Library/Application Support/Firefox/Profiles/profiles.ini
Start by closing all instances of Firefox. Then, make a backup copy of the original:
$ cp ~/Library/Application\ Support/Firefox/Profiles/profiles.ini ~/Library/Application\ Support/Firefox/Profiles/profiles.ini.orig
You need to modify that profiles.ini file so it looks like this:
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=Profiles/5e97feg7.default
Default=1

[Profile1]
Name=dev
IsRelative=1
Path=Profiles/ou4e9v2y.dev

Now you should have a working Firefox profile that syncs between two different computers. I wouldn't recommend trying to run both at the same time though, as you are likely to fun into locked file issues and develop conflicting versions of files.

There was one other issue I had to deal with; one of the Firefox add-ons I am working with is Greasemonkey. When you edit your Greasemonkey scripts, the first time you are asked to pick out the text editor you want to use. Well on my Mac I use Smultron and in Linux I use gedit. Each time I would switch operating systems I would again be asked which text editor I wanted to use. In OS X, the path to Smultron is /Applications/Smultron.app. So on my Linux machine I created a directory called /Applications and put inside it a hard link to gedit that I renamed. I did it like this:
$ sudo mkdir /Applications
$ sudo ln /usr/bin/gedit /Applications/Smultron.app
Now no matter which operating system I use, Greasemonkey looks for /Applications/Smultron.app when I want to edit scripts.

Set system-wide environment variables in Linux

You can set environment variables at any time or from any location in Linux. If you want to set a variable that will be persistent, that is to say will be effective for all users and remain even if the system is rebooted, you have to put them in /etc/environment. For example, if you want to set a variable called JAVA_HOME, you would do it like this:
$ sudo echo -e "JAVA_HOME=\"/usr/lib/jvm/java-6-sun\"" >> /etc/environment
This assumes that you want to be using the "java-6-sun" Java package and not one of the others. Also, make sure you create a backup of the original /etc/environment file if this is your first time editing it:
$ sudo cp /etc/environment /etc/environment.orig
And also make sure you use two angle brakets (>>) and not just one. Using only one (>) will overwrite the file instead of concatenating to the end of it.

Saturday, October 24, 2009

Set java version in Linux

Java is a great cross-platform programming language that produces code that will work across operating systems and hardware architectures, provided the other computer also has Java installed. There are, however, some different versions of Java out there and not all software always plays nicely with each different version. Right now the main releases of Java that are in use are Java 5 (aka 1.5) and Java 6 (aka 1.6). To see which version of Java you are running, use this command:
$ java -version
To see all of the version of Java you have installed that you could be using, use this command:
$ update-java-alternatives -l
This will list each version of Java you can use. On one of my systems, for example, when I run that command I get this output:
java-1.5.0-sun 53 /usr/lib/jvm/java-1.5.0-sun
java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk
java-6-sun 63 /usr/lib/jvm/java-6-sun
Here I have three version of Java installed: Sun's version of Java 5 (aka 1.5), version 6 of the OpenJDK, and Sun's version of Java 6. If I wanted to switch to using Sun's version Java 6, I would enter this command:
$ sudo update-java-alternatives -s java-6-sun
To switch version, you enter the name of the Java version you want to use after the -s flag. For more information check out the man page with this command:
$ man update-java-alternatives

Saturday, October 10, 2009

Install subversion in Ubuntu

Subversion is a software solution for group collaboration on writing code. The idea is that an individual will "check out" a chunk of code to work on. This way other users won't also work on the same bit of code and the project will run into version problems. To get this configured in Ubuntu is an easy process.

First, install subversion:
$ sudo aptitude update && sudo aptitude -y install subversion
Create a group to use with your project:
$ sudo groupadd [groupname]
Now you'll have to add each user to that group you just created:
$ sudo usermod -aG [groupname] [username]
Create a directory to house the code:
$ sudo mkdir /home/svn/[project]
Set ownership of the code directory:
$ sudo chgrp -R [groupname] /home/svn/[project]
Set permission for the code directory:
$ sudo chmod -R g+rws home/svn/[project]
Tell the subversion controller about the project directory:
$ sudo svnadmin create /home/svn/[project]
For more information of subversion, check out this page from the Ubuntu documentation.

Build Linux installer packages with checkinstall

Compiling from source code is not my preferred way of installing software. One of the wonderful things about Linux is the ability to use prebuilt packages available through the software repositories. However, in the event that a particular software package in not available in a repository or you want a custom build of one, compiling from source may be the only way to go. Compiling from source carries with it the inherent problem that the package manager won't know the software package in question is installed. This can result in an update that breaks the program you installed from source. The way to alleviate this concern is with checkinstall, a convenient tool used to build installer packages for your distribution.

First, make sure you have checkinstall and your development tools installed. Use this command to install them in Ubuntu:
$ sudo aptitude update && sudo aptitude -y install build-essential checkinstall
Now there is where it gets a bit tricky. You will have to follow the build instructions that accompany the code you want to compile. Some times this is easy and sometimes it is difficult. Sometimes the process is very straightforward, like this:
$ ./configure
$ make
Usually the next step here would be to use make install to install the newly compiled code. Instead, run this command:
$ sudo checkinstall
If you want you can work through the options to document the installer package. Now you can install the new installer package using the package manager:
$ sudo dpkg -i [filename].deb
Now the package manager will be aware of the code you just installed.

Friday, October 2, 2009

Install Tomcat in Ubuntu

Tomcat is a java application server. The latest version is version 6, but version 5.5 is still in wide use also. I found some information suggesting that the version in the Ubuntu repositories is broken so here is how to install it manually, no compiling required.

First you need to make sure you have Java installed. I recommend going with the development version:
$ sudo aptitude update && sudo aptitude install sun-java6-jdk
The latest version of Tomcat as of this writing is version 6.0.20. You can head over to the downloads page to get a copy of it or download using wget:
$ wget http://www.gtlib.gatech.edu/pub/apache/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz
Extract the files from the tarball:
$ tar xvzf apache-tomcat-6.0.20.tar.gz
Now you can run Tomcat from anywhere, but you probably will want to put it somewhere other than where you downloaded it to. Here is an example of somewhere to put it:
$ sudo cp -r apache-tomcat-6.0.20 /usr/local/tomcat
This command will also rename the folder from "apache-tomcat-6.0.20" to "tomcat". Next you'll need to make sure all of the binary files are executable:
$ sudo chmod +x /usr/local/tomcat/bin/*
Before you can run Tomcat, you have to specify a variable to tell it where Java is installed. If you are going to start it up manually you can put the variable in the Tomcat startup script located at "tomcat/bin/startup.sh". If you are going to make a startup script in /etc/init.d/ you can put the variable in there. The variable declaration will look something like this:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
Make sure this is correct on your system before you use it. I recommend creating a startup script in /etc/init.d/ for Tomcat because even if you only want to start it manually its easier to remember /etc/init.d/ because thats where most startup scripts are located. Use a text editor to save the following to "/etc/init.d/tomcat":
# /bin/sh

# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

export JAVA_HOME=/usr/lib/jvm/java-6-sun

case $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0
Make the script executable:
$ sudo chmod 755 /etc/init.d/tomcat
Set the script to run automatically:
$ sudo update-rc.d -f tomcat defaults
Now you can start Tomcat by running:
$ sudo /etc/init.d/tomcat start
Or stop it by typing:
$ sudo /etc/init.d/tomcat stop
Tomcat by default listens on port 8080. You will need to make a rule in your firewall to allow this. If you are using Ubuntu's ufw, enter this command to allow connections to Tomcat:
$ sudo ufw allow 8080
You should see a Tomcat test page by navigating to:
http://192.168.1.xxx:8080
Just replace 192.168.1.xxx with the IP address of the computer you just installed Tomcat onto.