How To Install ncurses Library on a Linux

I need to compile an application with ncurses library and header files. How do I install install ncurses libs and header files on a Linux operating system? How do I write a simple hello world program using the ncurses and compile it on a Linux?

GNU ncurses is software API for controlling writing to the console screen under Unix, Linux and other operating systems. You can create text-based user interfaces (TUI) on a Linux or Unix-like system using ncurses library. [donotprint]
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsNone
Time5m
[/donotprint]

ADVERTISEMENTS

Installing the ncurses library in Debian/Ubuntu Linux

  1. You need to install the following two packages:
    libncurses5-dev : Developer’s libraries for ncurses
    libncursesw5-dev : Developer’s libraries for ncursesw
  2. Open the Terminal application.
  3. Type the following apt-get command to install ncurses header and libs:
    sudo apt-get install libncurses5-dev libncursesw5-dev

Sample outputs:

Fig.01: Install ncurses library using apt-get

Fig.01: Install ncurses library using apt-get

Installing the ncurses library in CentOS/RHEL/Scientific Linux 6.x/7.x+ and Fedora Linux 21 or older

  1. You need to install the following package:
    ncurses-devel : Developer's libraries for ncurses
  2. Open the Terminal application.
  3. Type the following yum command to install ncurses header and libs:
    sudo yum install ncurses-devel

Sample outputs:

Fig.02: Install ncurses library using yum

Fig.02: Install ncurses library using yum

Installing the ncurses library in Fedora Linux 22.x+

  1. You need to install the following package:
    ncurses-devel : Developer's libraries for ncurses
  2. Open the Terminal application.
  3. Type the following dnf command to install ncurses header and libs:
    sudo dnf install ncurses-devel

How do compile C program and use the ncurses library?

Create a test program called hello.c as follows:

#include <ncurses.h>
 
int main(void){	
	initscr();			/* Start curses mode 		  */
	printw("Hello World !!!");	/* Print Hello World		  */
	refresh();			/* Print it on to the real screen */
	getch();			/* Wait for user input */
	endwin();			/* End curses mode		  */
	return 0;
}

First, make sure you install GNU/GCC C compiler on a Linux:

To link to the ncurses library pass the -lncurses option to gcc/cc command:
$ cc -o output input.c -lncurses
$ cc -o hello hello.c -lncurses

Run it:
$ ./hello
Sample outputs:

Hello World !!!

Here is another program:

/*
 
  CURWIN1.C
  =========
  (c) Copyright Paul Griffiths 1999
  Email: mail@paulgriffiths.net
 
  Moving windows with ncurses.
 
*/
 
 
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
 
 
int main(void) {
 
    WINDOW * mainwin, * childwin;
    int      ch;
 
 
    /*  Set the dimensions and initial
	position for our child window   */
 
    int      width = 23, height = 7;
    int      rows  = 25, cols   = 80;
    int      x = (cols - width)  / 2;
    int      y = (rows - height) / 2;
 
 
    /*  Initialize ncurses  */
 
    if ( (mainwin = initscr()) == NULL ) {
	fprintf(stderr, "Error initialising ncurses.\n");
	exit(EXIT_FAILURE);
    }
 
 
    /*  Switch of echoing and enable keypad (for arrow keys)  */
 
    noecho();
    keypad(mainwin, TRUE);
 
 
    /*  Make our child window, and add
	a border and some text to it.   */
 
    childwin = subwin(mainwin, height, width, y, x);
    box(childwin, 0, 0);
    mvwaddstr(childwin, 1, 4, "Move the window");
    mvwaddstr(childwin, 2, 2, "with the arrow keys");
    mvwaddstr(childwin, 3, 6, "or HOME/END");
    mvwaddstr(childwin, 5, 3, "Press 'q' to quit");
 
    refresh();
 
 
    /*  Loop until user hits 'q' to quit  */
 
    while ( (ch = getch()) != 'q' ) {
 
	switch ( ch ) {
 
	case KEY_UP:
	    if ( y > 0 )
		--y;
	    break;
 
	case KEY_DOWN:
	    if ( y < (rows - height) )
		++y;
	    break;
 
	case KEY_LEFT:
	    if ( x > 0 )
		--x;
	    break;
 
	case KEY_RIGHT:
	    if ( x < (cols - width) )
		++x;
	    break;
 
	case KEY_HOME:
	    x = 0;
	    y = 0;
	    break;
 
	case KEY_END:
	    x = (cols - width);
	    y = (rows - height);
	    break;
 
	}
 
	mvwin(childwin, y, x);
    }
 
 
    /*  Clean up after ourselves  */
 
    delwin(childwin);
    delwin(mainwin);
    endwin();
    refresh();
 
    return EXIT_SUCCESS;
}

Compile and run it as follows:
$ cc -o curwin1 curwin1.c -lncurses
$ ./curwin1

Sample outputs:

Fig.03: Basic window operations in action using ncurses

Fig.03: Basic window operations in action using ncurses

See this page and GNU ncurses project home page for more information.

This entry is 10 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • Bonzodog Sep 6, 2015 @ 18:25

    One of the very earliest O’Reilly books was gloriously entitled “Programming with curses”.

    As if there was any other way………………………

  • santosh kumar dash Dec 14, 2015 @ 16:12

    I have some problem about installing ncurses library on ubuntu 14.04 ,
    i am trying this below two command but no result…
    1. sudo apt-get install ncurses-dev
    2. sudo apt-get install libncurses5-dev libncursecw5-dev
    please help me

  • Tom Feb 7, 2016 @ 10:38

    I could not get this to work on Ubuntu 15. A terminal window pops up, but it’s just a terminal window. Nothing like what the tutorial suggests it should look like. I can’t do anything in the terminal, just ctrl+c to kill it.

    • bonzai Sep 26, 2016 @ 20:26

      after
      mvwin(childwin, y, x);
      put
      wrefresh(childwin);

  • yongching Nov 24, 2016 @ 10:20

    when i type , sudo apt-get install libncurses5-dev, it said couldnt find package libncurses5-dev…how to solve?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.