The static side of TuxArena
This is a refined, updated version of the article I initially published here.

This tutorial targets especially beginners in Linux and particularly Ubuntu, users who have just recently switched from Windows to Ubuntu and are facing this question: "how can I compile and run my C or C++ programs in Ubuntu?". Most of these users study C or C++ at school and are usually used from Windows with an IDE (Integrated Development Environment - a code editor which offers many other functions, including compiling and running the source code) like Dev-C++ or Code::Blocks.

To start with, I'd like to point out that Code::Blocks is also available for Ubuntu, and Dev-C++ can be ran through Wine, a program which allows most Windows applications to be run on Linux (see this Wine guide I put up a while ago).

If you want to quick start with Code::Blocks, then go ahead, open a terminal and type:
sudo apt-get install codeblocks gcc
The command above installed Code::Blocks and gcc, the GNU C Compiler used to compile C and C++ code. Now you can go to Applications->Programming->Code::Blocks IDE to launch it and get going.

However, I'd like to briefly explain how this works, and I'll introduce two methods: the command-line mode, where all you need is a terminal and an editor which works in a shell, and the other method, which takes into account graphical IDEs with advanced features.

Compiling C in Ubuntu

For this example we will use Nano, a popular and fast text editor which gets the job done quickly. Two other powerful, but a little harder to master when starting in Linux are Vim and Emacs. First make sure you have all the tools installed. Open up a terminal and type:
sudo apt-get install build-essential nano
Nano is almost surely already installed. build-essential is a meta package, that means a package which only depends on other packages, and when it is installed it will automatically install several tools like gcc, the C compiler, or make.

Now let's create the first C source file, say hello.c:
nano hello.c
Nano will fire up, and let's proceed to write our program. I'll use a simple, hello world example here:

int main () {
  printf("Hello, world!\n");
  return 0;
Now press ^O (that is Ctrl+O) followed by Enter to save your program, and then press ^X (Ctrl+X) to exit Nano and return to the terminal.


This program will print the message Hello, world! to the standard output, the screen. However, first we have to compile it. To do so, type:
gcc hello.c
Now if you type ls to list the files in the current working directory, you'll see a file called a.out. That's our program, and to run it type:

You can also specify a different filename for the output program instead of a.out, e.g.:
gcc hello.c -o hello
IDEs (Integrated Development Environments)

Actually for starters I'd like to point out that a simple text editor with basic developing facilities like syntax highlighting and indentation is a better start than a full-fledged IDE. For terminal use, I'd recommend Nano. For GUI, I'd go with Gedit, Kate or Geany.

There are so many text editors and development applications available it wouldn't make no sense to list all of them here. There are powerful ones like Emacs or Vim which take a little time to master, graphical ones like KDevelop or Anjuta. Some prefer text editors with enough capabilities like Kate, or Geany, or the one included by default in GNOME, Gedit. I recommend reading this comprehensive list of no less than 20 text editors overviewed by me a while ago, so try them and see which fits. I really advice against using a full-fledged IDE like KDevelop for example, which really serves other purposes.

Emacs was initially developed by Richard Stallman, the founder of GNU, and it is not only an IDE, but also an email client, web browser, file browser, and much, much more. To install it type in a terminal:
sudo apt-get install emacs
Which will install Emacs 23.3 in Ubuntu 11.10 Oneiric Ocelot.


Learning Emacs or Vim can be hard when starting, but in time it will prove a time-saver. Emacs differs from Vim completely when it comes to keyboard shortcuts and way of moving around and manipulating text, so I'd recommnd learning one and sticking to it. To open a file in Emacs, type ^X-F then write the path to the filename in the bottom bar. To save a file, type ^X-S (Ctrl+X followed by Ctrl+S). To quit Emacs, use ^X-C. In Vim, you would press I for insert text mode and Escape to enter commands. In command mode, :q will quit Vim, while :w will write to file.


However, if you're not happy with a text editor with development capabilities, let me suggest several dedicated IDEs which you can try.

A feature-rich IDE targeted towards developing applications in Qt and for the KDE desktop environment.
sudo apt-get install kdevelop

It's written in GTK, so it blends well in GNOME. Anjuta is a powerful IDE for C and C++.
sudo apt-get install anjuta

An advanced IDE written in Java, and although aimed towards developing Java applications it can be used for developing in C/C++ too. You will have to download it manually from the official website. Once you've downloaded it, install the Java Runtime Environment to be able to run it:
sudo apt-get install openjdk-6-jre
Alternately you can download and install the Java Runtime Environment from Oracle at this address.

Eclipse CDT
This is the Eclipse IDE with C and C++ development plugins.
sudo apt-get install eclipse-cdt
Another powerful C/C++ IDE (Homepage).
sudo apt-get install codelite


To round it up, here would be my recommendations for a user who is starting development in Linux:
  • GUI (graphical user interface) with enough development features: Kate, Gedit, Geany
  • simple editor for the terminal: Nano
  • advanced, powerful IDEs: Emacs, Vim
  • other GUI, powerful IDEs: Code::Blocks, Codelite, Anjuta, Eclipse, KDevelop, Netbeans etc
  • an overview of 20 text editors for Linux is available here

Updated: Dec 2, 2011 | v1.0