Installing Mercurial on Linux

Update: I’ve updated this post to focus on the steps to take in order to get mercurial working on your system, and some rough guidelines.

To install, use your installation/package manager. In Ubuntu, run:

sudo apt-get install mercurial

Now you need to set permissions. Inside your home directory, ~/, you can set up your “hgrc” (mercurial configuration file) with your username and default email address. Note that you must create and edit this file as the owner of ~/, not as root, because mercurial will only trust files that you created.  So open or create the file ~/.hgrc. Then enter the following lines:


username = My Name <>

with your name and email. Now you should be able to check out and use repositories.

Note that if you get errors about untrusted users (for example, mercurial ignores anything from “untrusted user root”, the only fix may be to edit your systems hgrc file to trust those users. The file is called /etc/mercurial/hgrc and you should add these lines to it:


users = root

Or any other users you wish to trust.

Using Mercurial

I won’t cover how to set up a Mercurial repository, because I didn’t — I just used bitbucket. To start out with a repository located at some url, use cd to navigate to the directory you want to use, then use hg clone to get a copy of a project. For example:

cd ~/my-documents/my-projects/
hg clone

Or wherever it is. Now you’re ready to start developing!  If you create a new file or folder, you must add it to the project:

hg add *

Sending and receiving updates is a two-step process. To finalize all the changes and additions you’ve made, you must commit the changes. To send that final version to the repository, you must push it.

hg commit -m 'Made some changes'
hg push

Conversely, to get the newest version from the repository, you must pull it. To change your working copy to incorporate this new version, you must updateit.

hg pull
hg update

That’s the gist of it.

Seven Reasons I Prefer Linux

We all like rants, right? Especially rants directed mainly against Microsoft (public enemy #1) and/or Apple (public enemy #2)? Of course we do. This is not a list of technical features. It has nothing to do with security or the free software movement. This is just a list of cosmetic, user-friendly features that Linux systems (or at least Ubuntu) get right — and certain big-name OSes get wrong.

1. In-Place Software Updates

With Ubuntu, updating is a piece of cake. There’s a single app, the Update Manager, that pops up with a list of all updates for my system and programs. When I click install, it doesn’t force me to quit any of my applications or (shudder) restart my system. It installs easily and quickly. This is directed at you too, Apple. I have a laptop so I can use it on the go. You think I want to restart my Macbook while I’m trying to take notes or look up directions? There never seems to be a good time to stop everything, quit everything, install, and wait to restart.

And there’s nothing worse than stopping what you’re doing, installing updates, restarting your machine, and immediately seeing this little guy pop up again:

2. Not Having To Babysit: Boot, Shutdowns, Installations

It’s bad enough when you set something to install, click through all the menus, and go take a break, only to come back and find out it’s still waiting for you to confirm that the program can make changes to your computer. What’s worse is when you have to remember to sit and watch your computer screen while it’s booting, in case it decides to try to “recover a drive – press a button to cancel”. (And what does that even mean?) Equally annoying is when you think you’ve shut your computer down, only to find out that it’s “Installing Important Updates (2 of 23)”. On Linux, when you start up the computer, it starts up. When you shut it down, it shuts down. When you hit install, the program is installed.

Also in this category: interruptions. Having some window pop up while you’re trying to watch a movie or edit a file can be anything from annoying (watching a movie, browsing reddit) to disastrous (giving a presentation, accidentally hitting the wrong button as it appears). It’s just a dumb idea, and Linux doesn’t do it.

3. Software is Free and Easy to Install

Ubuntu really shines here. If I need an application, it’s one click to the Ubuntu Software Center, a quick search, and then one more click to download and install. If the program I need is slightly more obscure, I can go to the command line and type “sudo apt-get install software“. Installed and done. Doesn’t ask me “Where do you want to save this file?” Doesn’t make me unzip, run the installer, select a directory, and configure options. Doesn’t make me restart my computer. One step.

4. Doesn’t Ask Me Dumb Questions

This goes along with the “allow this program to make changes to your computer” thing. For example, when I connect to my school’s wireless network, I don’t want to bother classifying it as Home, Public, or Work. In fact, I consider myself a moderately advanced user and I don’t even know the difference, much less care. This passes the level of minor annoyance when Windows recognizes the same network four times in a row as “Wifi 2”, “Wifi 3”, etc and presents me with a popup for each one repeatedly.

Similarly, I already know that my application is a “program you downloaded from the Internet” and yes, I’m “sure that you want to run it”. There’s probably a way to turn off all these idiotic questions and notifications, except…

5. Rational, Consistent Organization

If I want to change a system setting on Linux, I don’t need to have ever done it before. The menus and applications are organized logically (and everything is an application, so system settings are organized cleanly next to everything else).

On Windows, things aren’t so clear. I know I probably have to go into the Control Panel, but beyond that…. For instance, did you know that display settings are under “Hardware and Sound”, but disk partitioning is under “System and Security”? Who’d have guessed?

Meanwhile, folder setup and navigation are obfuscated in Windows, but consistent and logical in Linux. On Windows 7, if I download a pdf from my browser and just hit “save” without looking, it’ll take me 5 minutes of shuffling through folders and hierarchies to have a clue where it was saved. (C:\Windows\Downloaded Program Files? C:\ProgramData? C:\Users\myname\Downloads?) Plus, there are places (like the Control Panel) that can’t even be accessed from Windows Explorer. On Linux, it’s pretty clear where things belong, or at least it’s possible to narrow it down.

And the little things. I’m navigating through Windows Explorer, and I want to get the properties of the current folder. Turns out, that’s in the “Organize” menu. What? And here’s the one that made me decide to write this post: When I found myself googling the phrase “how to eject an external hard drive in windows 7.” No, I could not figure out how to do it on my own. This may be because I’m stupid, but stupid people use computers too; interfaces should be designed to be intuitive. It’s not hard to figure out how to hit the eject button in Ubuntu.

6. Sometimes, Windows is Just … Aaaargh

I don’t even know how to classify some of these. But let’s say I want to create my own wireless network. Here’s what happens to me in Windows 7. Hit the Network icon at the bottom right. Click on “Open Network and Sharing Center.” Click “Set up a new connection or network.” I’m faced with several odd options, but I’m pretty sure I want “Set up a new network (configure a new router or access point)”. Click it, hit next. It tells me to wait up to 90 seconds while it searches for unconfigured devices. Hmmm. Nothing appears. Nevermind, go back. Scroll down. Oh, ok: Click “Set up a wireless ad-hoc (computer-to-computer) network”. Hit next. Type a name, select “No authentication”, and hit next. Done! That’s 6 clicks (not counting where I messed up).

Ubuntu: Click on the little wireless icon. In the dropdown, select Create New Wireless Network. Type a name and hit Create. 2 clicks.

What about setting up sharing of files between computers? In Ubuntu, I right click on a folder and select “Sharing Options”. (It’s also not hard to figure out on a Mac.) On Windows, I try the same thing, but am stopped short — they want me to join a “Homegroup”? What is this, a network system designed by George Bush? “To create or join a homegroup, your computer’s network connection must be set to Home.” Thanks for the information, but I have no idea what you’re talking about. The options it gives me to click on are “What is a network location?”, “Tell me more about homegroups”, “Change advanced sharing settings…”, or “Start the HomeGroup troubleshooter”. None of these things is what I want to do (I want to share my files and folders). Sigh and give up.

7. Hack and Customize … At Your Own Pace

Linux is known, perhaps, for users coding their own scripts — the implication being that you need to be fluent in bash to install a Linux distro. In fact, especially with user-friendly distributions, you don’t need to know a thing about the command line. And anything you do end up needing to learn, you can pick up along the way, one step at a time. “Type ‘make install’ … hmm, I guess I can do that.” “Use ‘ls’ to find out the contents of a directory. Ok, let me try it.”

But once you start learning, you can find more and more useful things to do on the command line. If you want, you can learn to write scripts and so on. (If you don’t care to, you don’t have to.) The more you use it, the more you learn about your computer and how to get more out of it. You feel like you’re in charge of the computer, because you know your way around. In Windows, I always felt like I was poking around a few well-lit paths, while most of the system was dark “computer space” that belonged to the operating system and I had no place being there.


Question: How did Windows get the reputation of being for non-computer-savvy novices while Linux is supposedly for advanced users only? Holy Crap! No wonder people think computers are so complicated — they’ve been using Windows this whole time!

If you’re on Windows, and think that computers are complicated, tedious, and annoying, do yourself a favor and try out Ubuntu. (It’s easy — download Ubuntu to a CD or USB stick, then boot from it. You’ll be able to try out the OS without having to install it.)

Really, Macs score highly in these categories as well. Young, trendy Apple fans might hate me for saying this, but if you know someone older who has trouble with computers, do both of you a favor and get them a Mac. It’s so much simpler, won’t ask them confusing questions, and has great support.

But for general uses (except for specific programs and games that work only on Windows), I’m sticking to Linux from here on out. And I really think the average Windows user would be amazed at how much simpler it is.


After originally publishing this article, I’m just going to keep adding brief bullet points as they occur.

  • Memory footprint on the hard drive. As a general rule, my Ubuntu install takes around 1/10th the hard drive space of Windows to do the same task (better).

Large Primes, The Beatles, and $100,000

Prime numbers have fascinated mankind for thousands of years.

Or, rather, they’ve fascinated about 1/11th of mankind, and bored the rest.

But big primes are incredibly important in cryptography.  Every day, we rely on 512-bit primes — numbers about 150 decimal digits long. Our Internet data is secure, relatively, because it’s so hard to factor numbers this size.

But for the adventurous, 150 digits is nothing. What if you want to be cutting-edge? I’m talking the Lady Gaga of primes. If so, then you probably want to be the largest known prime: currently, 243,112,609 − 1.

How big is this number? It’s about 13 million digits long. (Think small bookshelf, full.) If you like, you can see part of the number here or even view the whole number here (the page could take a while to load!).

And like pop music, finding large primes can be lucrative (not as lucrative as being a record label, but still). The Electronic Frontier Foundation gave away a $100,000 prize to the first folks to find a 10-million-digit prime, and $150,000 is on the line for the first 100-million-digit prime.

Where do The Beatles come in?

They’re no longer as hip as Gaga, sure, but they still have plenty of devoted fans.

Now, you could argue that they were more into perfect squares than primes, with songs like “Revolution 9” and “WhenI’m 64”. But nevertheless, there’s a statistical chance that the Beatles created a “prime song” without even knowing it.

Here’s what I mean. If you take the largest known prime, 243,112,609 − 1, and store it on your hard drive, it’ll take up about 5.4 MB.

Now consider the Beatle’s song A Day in the Life, encoded as an MP3 at a nice average 128 kbps (kilobits per second).  That takes up somewhere in the neighborhood of five megabytes as well.

But here’s where the beauty of computers come in. A 5 MB file is just a whole lot of zeros and ones. If you give them to iTunes with the extension ‘.mp3’, then those zeros and ones will be interpreted as a music file and they’ll be played. On the other hand, you can choose to interpret them as a number. Looking at it this way, there’s a chance that A Day in the Life could be the largest prime ever discovered!  (If the Beatles aren’t really your thing, you could try the songs Wake Up, Where It’s At, or Californication.)

Sign me Up. I want to find a prime in a song.

Not so fast. You’ll need an incredibly well-trained ear and years of experience. OK, no you won’t. But it’s probably not too likely that many primes will sound aesthetically pleasing, or that many pop songs are represented as primes.

Let’s say you acquire the necessary skills and sit down with your music library to find a prime song. What are your odds? Luckily, there’s a theorem that tells us: The Prime Number Theorem. It tells us how often primes tend to occur: specifically, if we’re close to the number N, then we should find a prime number about once every log(N) numbers. (log(N), as you may or may not recall, is roughly a measure of the number of digits it takes to write down N.)

Applying the Theorem, we see that, on average, one in 30 million 5-minute songs, in a normal MP3 format, should be prime numbers. (Perhaps I’ll write a program to check all the songs in someone’s music library … then again, maybe not.)

Best of all, Gracenote’s media database contains 100 million songs, which means there’s a pretty decent chance that one or more of those is a prime — possibly a bigger prime than any yet discovered! Get listening! And if you win a cash prize, be sure to send me some.

Dual-Boot Windows 7 and Linux (Ubuntu)

For today, a quick (but hopefully thorough) walkthrough on how to dual-boot Windows 7 and a Linux distribution. Whether you’re starting from a clean machine or an existing Windows 7 computer, desktop or laptop, these general instructions should work for you. Note that this is just one way to do it. Also, others may recommend some changes or tweaks to this approach. But it should be useful if you’d like an example of how to set up a dual-boot system. (Disclaimer: I’m not responsible for anything at all, ever. Try this at your own risk. BACK UP YOUR DATA FIRST!)

I’ve done this on a home-built, clean desktop, and on a Lenovo x220 laptop where I kept the existing Windows 7 install. So I will draw on these as examples. Both times, I installed Ubuntu, which is commonly known as a user-friendly distribution of Linux that is good for first-timers. These instructions, however, should work in general.

What you’ll need: A computer, a way to back up your data, some disks/USB drives for installation. You’ll also need to be willing to Google some things if you haven’t done them before — in the interests of brevity, I won’t explain how to click through each menu or every step in detail. If you’re stuck on a part, leave a comment and I’ll try to explain; there are also lots of resources out there on the web! Finally, expect to do a little troubleshooting after installation to get Windows and Linux to work together with your Data partition, etc. It may take a bit of setup work, but should be worth it!

Before we head out onto the field, here’s the general game plan. We’re going to create three main partitions: one with our Windows 7 installation, programs, etc; one with our Linux installation, applications, etc; and one for all of our data. The two operating systems will both be able to access the data partition, so they can both read and write to the same folders and files.

Ready? Let’s go.

Step 1: Back Up Your Data/Computer

If you have any data stored on your computer, you need to back it up first. You should hopefully already have an external hard drive or some other backup method; if not, get one!

You’ll also want a way to reinstall Windows 7 if it’s pre-installed. (You may not end up using it, if you are able to simply “shrink” the Windows 7 install, but you will want it down the road if your computer crashes.) My laptop, for example, had an option of creating backup disks which could be used to return it to its factory state if something went wrong. I used this feature to create a backup on a 16 GB USB drive.

Step 2: Get Install Disks/USB

If you already have Windows 7 installed, you may be able to shrink the install to fit its partition without needing to reinstall Windows. If you want to reinstall Windows or have not yet installed it, you’ll need a Windows install CD or some other bootable device, like a USB drive, that allows you to install Windows 7.

You’ll also need a bootable CD or USB drive for your Linux distribution (I created mine using Ideally, it would come with the application GParted, which we will use to partition our hard drive. If not, you’ll need a bootable drive that does. (Worst-case scenario: create a bootable Ubuntu CD, use it in Step 3 to use GParted, then get rid of it and use your favorite Linux distro when it’s time to install.)

Step 3: Partition

Now we’re going to partition the hard drive. First, let’s figure out how many gigs each partition will need. Remember, all of our data (documents, music, everything) will go in the Data partition. But programs and applications will go in the partitions of the respective hard drives. Modern video games, for example, take up a lot of space.

  • Windows partition. Windows 7 alone is over 20 GB, so 30 GB is probably a bare minimum. I recommend at least 40 GB, possibly much more if you expect to install large programs.
  • Linux partition. The memory used varies by distribution but is very likely under 5 gigs. Add in the programs you’re running and 10 GB is probably a bare minimum, but I’ll recommend at least 20. If you have a large hard drive, it probably can’t hurt to leave extra space on these.
  • Data partition. This will be all the space that you have left.

Now, it’s time to actually make the partitions. First, a quick primer on partitions. Partitions are ways to divide up your hard drive. You can split a hard drive into partitions of two types: primary partitions and extended partitions. A primary partition is a chunk of the hard drive, plain and simple. And extended partition is also a chunk of the hard drive space, but an extended partition must itself be partitioned up into one or more separate chunks. These sub-chunks are called logical partitions.

You may only have up to four total primary/extended partitions on your hard drive. However, you can subdivide an extended partition into a number of logical partitions, enabling you to sidestep this limit.

Now, insert your Linux boot media (CD or USB drive) that has GParted on it. Shut down the computer. We will need to boot from the media. To do so, you may have to go into your BIOS menu and change the boot order. For my laptop, I was able to press F12 on startup, giving me an option of which device I wanted to boot from.

Once it’s booted up, find and run the program GParted. (Note: GParted asks you to select sizes in terms of megabytes, but here we’ll mainly be talking in terms of gigabytes. So be aware of the conversion!)

If You Already Have Windows Installed

Your disk will have a small partition at the very beginning (created by Windows for booting and/or system recovery; we don’t have to worry about it), then the rest of the drive is your Windows installation. (You may also have another partition at the end; my laptop, for instance, has a ~10GB partition at the end of the hard drive containing system recovery tools. If so, leave that one alone as well.)

The large Windows install is the partition we want to shrink. (It is a primary partition.) Click on it and select “Shrink Partition.” Hopefully, you will be able to shrink it to the size you want. If not, you may have too much data or it may be too fragmented. You can try defragmenting the drive and coming back to this step, or you can format this partition, erasing Windows. If you end up formatting the partition, follow the instructions for not having Windows installed. If you did shrink the Windows install successfully, you can skip to the section “Create Linux and Data Partitions”.

If You Don’t Already Have Windows Installed

It’s always nice to start from a clean slate. If there are any partitions on your hard drive, use GParted to delete them. Then create a primary partition at the beginning of the drive for Windows 7. Make it the size you decided on above and format it as ntfs. Then move on to the next step.

Create Linux and Data Partitions

Now, we’ll take the remaining space (located after our Windows partition) and allocate it as one big extended partition; then, we’ll divide it up into logical partitions for Linux and for our data.

In GParted, select the space after the Windows partition and choose to create an extended partition. Now we’re ready to create logical partitions within that big extended partition.

First, we’ll create what’s called the “swap”. Basically, this is space on the hard drive that Linux uses as extra RAM if your RAM is used up. If you don’t know about swap space, I recommend making your swap partition the same size as your RAM (if you have 4 GB of RAM, you can make the swap partition 4 GB). To create it, select the extended partition and create a new logical partition of the appropriate size. Format this partition as “swap” or “linux-swap”.

After that (but still within our extended partition), we’ll create a logical partition to hold our Linux operating system. Do so in the area after the swap space, within the extended partition. Format this as ext4, which is a standard format for Linux.

Use the remaining space in the extended partition to create a logical partition for Data. We’ll format this as ntfs, so that Windows and Linux can both read/write to it. Once that’s done, we’re done creating partitions and ready to install!

Step 4: Install Windows (if not already installed)

If you don’t have Windows installed, we need to install Windows first. Boot from the Windows CD (or USB drive if you have it on that disk image) and do a fresh install. Make sure to install it onto the partition you created specifically for Windows.

Step 5: Install Linux Distro

To install Linux, boot from your Linux installation media. You’ll want to do an advanced install (in Ubuntu, select the “Manually specify partitions” option). You will choose which partitions to use for installation. There are two partitions to worry about in this step. Set your swap area as “swap”. (Duh.) Set your Linux area (located right after your swap) as /. This is where the Linux install will go. For mount point, choose the entire device (probably called “/dev/sda”).

Now you should be able to go ahead with the installation!

Interlude: Working Machine

You should now have a fully functioning dual-boot system! When you start up the computer, your Linux boot manager screen should come up, allowing you to manually select which operating system to boot, or selecting Linux after a few seconds of inactivity. You can boot each OS to make sure they were installed correctly. Then, let’s finish the job with Step 6.

Step 6: Configure Each OS to use Data Partition

We’ll start with Linux, since it can be a bit trickier. What we want is for Linux to automatically mount the Data partition upon startup, and we want to be able to read and write the partition. In Ubuntu, I was able to successfully do so by following instructions from I will list the steps below; note that your mileage may vary on other versions of Linux.

First, you need to know the name of the Data partition. For example, mine was called /dev/sda7. You can figure it out with GParted, or by opening a Terminal and running “sudo fdisk -l”.

Now, we’ll make a folder to be the mount point for the partition. (“Mounting” the partition just means loading it up so the system can access the partition.) When the partition is mounted, it will appear in this folder. For this step, I created a folder named “Data” in the /media folder, since /media is where drives are usually mounted. To do this from the Terminal, you can run “sudo mkdir /media/Data”.

Now we need to change how the drive is loaded on startup. The file that controls this is called fstab and is located in the /etc folder. First, you should create a backup copy of fstab in case you mess something up. You can do that on the command line by running “sudo cp /etc/fstab /etc/fstab-backup.bak”. This copies fstab into a file named fstab-backup.bak.

To edit fstab, we can open it up in Gedit (a text editor): “sudo gedit /etc/fstab”.

There will be one or two lines describing how to load up your Data partition. For example, if it is called /dev/sda7, you might have a line that looks like:

/dev/sda7  auto nouser,atime,noauto,rw,nodev,noexec,nosuid 0 0

Comment it out by placing a # at the beginning of the line (if there are two, comment both out):

#/dev/sda7  auto nouser,atime,noauto,rw,nodev,noexec,nosuid 0 0

Now, we will add a line that mounts the partition to our chosen folder with read/write permissions. Add the line:

/dev/sda7 /media/Data ntfs-3g quiet,defaults,rw 0 0

where /dev/sda7 is the name of your Data partition and /media/Data is the folder where you’d like it to mount.

Now, reboot your system and boot back into Linux. The Data partition should be mounted and you should be able to read and write to it. To test it out, you can navigate to /media/Data (or wherever your mount point is) and create some folders. I personally put the following folders in my Data space: Documents, Media, Other, Temp, and Trash.

Last thing on Linux. On a normal Linux install, your own personal files are stored in “/home/username/”, where username is, well, your user name. To make things easier, we can make links (shortcuts) from inside this folder to our Data. I created a link to each of the above folders: Documents, Media, Other, Temp, and Trash.

Now, reboot into Windows. Windows should automatically mount the Data partition as a drive (on my system, it is D:\). You should be able to open it and see any folders and files you created on Linux. To finish things off, I recommend making similar shortcuts on Windows: In your C:\Users\username folder, delete the “Documents”-style folders and replace them with shortcuts pointing to D:\Documents and so on. You can also drag these folders into your Favorites and Library bars in Windows Explorer (on the left side) for easy access.


I hope these instructions were clear and worked well. If not, let me know!

Easily Open a File in Java

Say you wanted to open a file in Java, either for reading or writing. But it’s not clear how to do it, and whether you need a GUI, and blah blah blah, wouldn’t it be nice to just find a minimal example?  Well then, here you go.

import javax.swing.*;   // for JFileChooser
import*;       // for File
import java.util.*;     // for Scanner (if you want to read it in)

class FileOpener {

    public static void main(String[] args) {
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            // it's in your hands now...

Solid. Now say you want to read from the file:

            try {
                // read in the file
                Scanner s = new Scanner(f);
                String str = s.nextLine();
                // ...
            } catch(FileNotFoundException e) {
                // Couldn't find the file for some reason

Or writing to the file:

            try {
                FileWriter fw = new FileWriter(f);
                fw.write("Hello, World!");
                // ...
            } catch(IOException e) {
                // didn't work

Ta-da! Protip: If you’re using this as part of a GUI with a Component “comp” showing your main GUI screen, set “comp” to be the parent of the filechooser dialog by calling fc.showOpenDialog(comp);.

Other protip: if you really want, you can subject yourself to the official tutorial here.

Will Computers Beat Us at Our Own Game?

This article from Time raises some interesting questions: How long until computers are “smarter” than humans? (Especially considering Watson’s recent performance on Jeopardy!) What about machines with creativity, intuition, and generalized intelligence — the historical province of humans alone? And if that’s not scary enough, how long until we can achieve immortality by downloading our brains into computers and preserving ourselves as thinking machines indefinitely?

Their article on the subject from a social point of view is quite interesting. However, it seems that the issue could benefit from a computer science perspective as well. What kind of technology would be required to achieve these things, and how tough will it be to get there? Let’s find out, or at least make some guesses.

Continue reading

Build Your Own Computer Part 4: Put It Together

Putting together a computer should be easy. In fact, it is easy. So let’s do it.

If you’re interested in the entire process of building your own computer, starting from scratch, then I encourage you to check out Part 1, Part 2, and Part 3. They detail the steps of understanding and choosing your components. But if you’ve got all the components and are ready to install, then it’s time for…

Continue reading