The first step in developing applications in Linux based computers is mostly to have a setup of your developer tools to be installed properly. This short guide aims to provide you with installation steps required to get started with some of the popular programming languages to develop fascinating software.
This post will provide steps for installing the required tools and provide a classic "Hello World" example to test your installation of the tools. Before attempting to install any of the tools, make sure whether your system already contains the tools. Chances are high, that you, most probably have it installed already.
To compile a C program in Linux based systems, the most popular tool used is the gcc compiler. This is contained in the GNU Compiler Collection (GCC). To check whether gcc is already installed enter the following command
$ gcc --version
if you see an output like the following, gcc is already installed and you are all set to write C programs.
gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Instead if you see the following or something similar, you will have to install gcc.
bash: gcc: command not found...
Though gcc can be installed separately it's recommended to install a package called 'build-essential', which contains bunch of other useful and related packages. To install it issue the following commands in Debian based Linux.
$ sudo apt-get update
This will update the package list. Now issue the following command in Debian based Linux like Ubuntu.
$ sudo apt-get install build-essential
In Linux systems such as Red Hat Linux or Fedora that has yum based package manager, you can use the following command,
$ sudo yum install gcc
This will install gcc compiler and other development libraries like glibc. You can also use the following command to install whole bunch of development packages which also includes gcc and g++ (for C++)
$ sudo yum groupinstall "Development tools"
Now you can write a sample C program as below to check that everything is working fine. Create a file named
HelloWorld.c and type in the following program text.
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0 ;
}
Compile the above program with the following command.
$ gcc -o HelloWorld HelloWorld.c
This will create an executable file named HelloWorld in your current directory. To execute the executable program type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Congratulations!! You are a C guru now.
The most popular C++ compiler in Linux is undoubtedly g++. This is also part of the GCC. If you have installed the build-essentials in the Debian or the "Development Tools" in Red hat Linux, you must already have g++. Otherwise, go ahead and install build-essentials or "Development Tools". To check whether g++ is already installed, enter the following command.
$ g++ --version
if you see an output like the following, g++ is already installed and you are all set to write C++ programs.
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Instead if you see the following or something similar, you will have to install g++ by installing build-essential package or the "Development Tools".
bash: g++: command not found...
If you didn't install "Development Tools", and want to install g++ separately using yum, use the following command,
$ sudo yum install gcc-c++
This should install g++ and other development files and libraries.
Let's now write a sample C++ program which prints Hello World! on the standard output. Type the following program in a file named
HelloWorld.cpp
#include <iostream>
using namespace std ;
int main()
{
cout << "Hello World!" << endl ;
return 0 ;
}
Compile the above program with the following command
$ g++ -o HelloWorld HelloWorld.cpp
This will create an executable file named HelloWorld in your current directory. To execute the executable program, type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Quite similar to what we did for C, right? Voila!!! Now you are a C++ expert too.
Next we shall look into setting up Java in Linux. Chances are that you already got Java installed. Most probably the OpenJDK version. If you are willing to work with OpenJDK, everything is fine, and you are ready to develop java applications. But you may have a valid reason to use Oracle java for developments. In that case we can remove OpenJDK and install Oracle Java or keep OpenJDK with Oracle Java. Though there are few ways to install and confugre Java in Linux, the best and easy way is to download the JDK zipped file from
http://www.oracle.com/technetwork/java/javase/downloads/index.html. Make sure to download the version you want, which is applicable to your system architecture. Once you have downloaded the zip file, extract it to a location of your interest. Let's say the extracted location is
/usr/local/java/jdk1.6.0_43. All you need to do is add the
JAVA_HOME (your installation directory; e.g:
/usr/local/java/jdk1.6.0_43) as a new environment variable and add the bin directory of
JAVA_HOME to the
PATH environment variable. This can easily be done by adding the following lines to your
.bashrc file in your home directory.
JAVA_HOME=/usr/local/java/jdk1.6.0_43
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH
If you add these lines to
.bashrc, the changes will be effective only to the current user. If changes need to be applied to all the users, the same set of lines can be added to
/etc/profile file. Once you added the line, source the respective file to make changes immediate on the same shell. To do that, issue the following command.
$ source ~/.bashrc
Now java is configured on your Linux box. Issue the following commands to verify your installation and environment variables. Expected outputs should be something similar to the following lines.
$ echo $JAVA_HOME
/usr/local/java/jdk1.6.0_43
$ echo $PATH
/usr/local/java/jdk1.6.0_43
$ java -version
If you see a similar output as follows you are all set to develop java applications
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
Let's write a simple java program, compile and execute it. Create a file named
HelloWorld.java and type in the following text.
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Make sure the class name match your file name. To compile this program, type in the following.
$ javac HelloWorld.java
This must generate a class file in your current directory named
HelloWorld.class. It contains the bytecode of your program. To execute your class file type
$ java HelloWorld
Make sure you do not type in the .class extension when executing. Following output should be shown in the standard output.
Hello World!
Congratulations!! you have written a simple java program.
Ok.. Have we done with everything? Ideally yes. But wait a second. We often come across new java versions. Then the question comes, whether we can have two versions of java installed in the Linux side by side? The answer, as you may have guessed, is a big "YES". So how do we go about making that. Ok.. The process is simple. Download the zip file of your interested version of java. And extract it, say to the same location (e.g.
/usr/local/java/jdk1.7.0_07). Next we got to inform the Linux that you have two versions of java. Let's collect a few more details.
$ java -version
This should still print the older java we configured earlier, like the following.
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
To find the path of the java that we currently use, issue the following command
$ which java
If you have only the java we set up earlier and followed the instructions above, you would get a similar output as follows.
/usr/local/java/jdk1.6.0_43/bin/java
Ok.. let's make the Linux aware about the two jdk versions we extracted to
/usr/local/java. To do that we issue the following commands.
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.7.0_07/bin/java 2
ok... now the Linux knows about the new java, but has set the priority to 2. Issue the following command.
$ java -version
This would print the new java version.
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
Ooops!!. Where the hell my old java went? Ok don't panic. What has happened here is that you have created a symbolic link to
/usr/local/java/jdk1.7.0_07/bin/java from
/etc/alternatives/java and another symbolic link to
/etc/alternatives/java to
/usr/bin/java. When you issue the above java command, the system path
/usr/bin/java overrides the path we earlier set. You can check this by issuing the following commands.
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Oct 5 11:14 /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 36 Oct 5 11:14 /etc/alternatives/java -> /usr/local/java/jdk1.7.0_07/bin/java
We shall now do the same to the old java we had. To do that, issue the following command
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.6.0_43/bin/java 1
If you issue the following command, we should still see the jdk7 is used.
$ java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
Ok. We shall change to the earlier version by issuing following command and selecting 1.
$ sudo update-alternatives --config java
You will be prompted as shown below.
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/java/jdk1.7.0_07/bin/java 2 auto mode
1 /usr/local/java/jdk1.6.0_43/bin/java 1 manual mode
2 /usr/local/java/jdk1.7.0_07/bin/java 2 manual mode
Press enter to keep the current choice[*], or type selection number: 1
By selecting 1, you can switch back to the earlier version. To verify, again issue the following.
$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
Further you have to do the same thing for
javac and
javaws commands for both the versions like the following.
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.7.0_07/bin/javac 2
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.7.0_07/bin/javaws 2
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.6.0_43/bin/javac 1
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.6.0_43/bin/javaws 1
Now you can select your preferred
javac version as below.
$ sudo update-alternatives --config javac
and select the appropriate number. That's it. We can now have as many java versions as we need in our system and switch back and forth easily to experiment with.
Most probably python is installed on your Linux. You can check for python version by entering following command
$ python -V
Python 2.7.4
If python is available you should see a similar output as above. If not install python with the following command, whichever is applicable, $
sudo apt-get install python or
$ sudo yum install python
Ok.. Now let's start writing a sample program. Create a file called
HelloWorld.py in your current directory. Enter the following text in it.
#!/usr/bin/python
print("Hello World!")
Save the file and execute the code using python interpreter as follows
$ python HelloWorld.py
Hello World!
Great.. Now you know how to program in Python too.
Most probably perl is also installed on your Linux. You can check for perl version by entering following command
$ perl -v
You should see a somewhat similar output as follows.
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi
(with 80 registered patches, see perl -V for more detail)
Copyright 1987-2011, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
If not, install perl using the following command, whichever applicable
$ sudo apt-get install perl or
$ sudo yum install perl
Done.. Now let's start writing a sample program. Create a file called
HelloWorld.pl in your current directory. Enter the following text in it.
#!/usr/bin/perl
print "Hello World!\n";
Save the file and execute the code using python interpreter as follows
$ perl HelloWorld.pl
Hello World!
That's great... Now you are a perl expert as well.
To check whether ruby is installed on you Linux, enter the following command.
$ ruby -v
If you have ruby installed, you should see somewhat similar output as below.
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
If you can't see an output like this, you will have to install ruby by issuing the following command, whichever is applicable
$ sudo apt-get install ruby or
$ sudo yum install ruby
Ok. Let's write the same HelloWorld program in ruby. Create a file called HelloWorld.rb and enter the following.
#!/usr/bin/ruby
puts "Hello World!"
Now you can execute the program by entering following.
$ ruby HelloWorld.rb
Hello World!
We finally have come to our last section. C# is a Microsoft developed programming language. Hence you should typically be programming in Windows using the .Net framework. But if you still want to develop C# or any .Net application in Linux, you are not to be made worry. You can install the 'mono', which is an open source implementation of the Microsoft .Net framework. To install mono on your Linux type in the following commands. whichever is applicable.
$ sudo apt-get install mono-complete or
$ sudo yum install mono-core.x86_64
When this is done, you are ready to develop .Net applications in Linux. To see the mono version installed, type in the following.
$ mono -V
you should see something similar to the following.
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
LLVM: supported, not enabled.
GC: Included Boehm (with typed GC and Parallel Mark)
Let's write a small C# program. Create a file called
HelloWorld.cs and type in the following.
using System;
public class HelloWorld
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
We can compile the program by issuing the following command.
$ gmcs HelloWorld.cs
This should create a file called HelloWorld.exe in you current directory. (Note : In Microsoft Visual C# in Windows, we use 'csc' command in the command prompt to compile a C# program). To execute the executable we can type in the following.
$ mono HelloWorld.exe
Hello World!
Voila!!! Now we can develop any .Net application in Linux as well.
Here we are. This post explained you to set up development tools (or rather to see whether it's installed) in your Linux box and walked you in creating sample programs to get started. I hope the article has been easy to follow and fun to read. Flourish in the development world!! Good luck.