This post will guide you how to install ncurse library in Unix-like operating systems. How to use the ncurses library in your C program and then compile it on Linux system.
GNU ncurses is a programming library providing an application programming interface that allows the programmer to write text-based user interfaces in a terminal-independent manner. You can use the ncurses library to develop “GUI-like” application software that runs under a terminal emulator on Linux system.
- Install Ncurses Library with Default Repositories
- Install Ncurses Library with Source code
- Compile C Porgram that call the ncurses library
- Video: Install ncurses Library From Source
Install Ncurses Library with Default Repository
You can install the Ncurses library with the default repository on the different Unix-like system.
For CentOS/RHEL Linux, you can use the yum command to install the following one package (ncurses-devel) on your system, type:
$ sudo yum install ncurses-devel
Outputs:
[root@osetc ~]# yum install ncurses-devel Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile epel/metalink | 17 kB 00:00 * base: mirror.sjc02.svwh.net * epel: mirror.sjc02.svwh.net * extras: mirror.sfo12.us.leaseweb.net * updates: mirror.sfo12.us.leaseweb.net base | 3.7 kB 00:00 epel | 3.2 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them. The program yum-complete-transaction is found in the yum-utils package. --> Running transaction check ---> Package ncurses-devel.x86_64 0:5.7-4.20090207.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: ncurses-devel x86_64 5.7-4.20090207.el6 base 641 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 641 k Installed size: 1.7 M Is this ok [y/N]:y
For Ubuntu/Debian Linux:
If you are using Ubuntu/Debian Linux, you can use the apt-get command to install the following two packages: libncurses5-dev libncursesw5-dev, type:
$ sudo apt-get install libncurses5-dev libncursesw5-dev
Outputs:
devops@devops-osetc:~/working$ sudo apt-get install libncurses5-dev libncursesw5-dev sudo: unable to resolve host devops-osetc [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libtinfo-dev Suggested packages: ncurses-doc The following NEW packages will be installed: libncurses5-dev libncursesw5-dev libtinfo-dev 0 upgraded, 3 newly installed, 0 to remove and 599 not upgraded. Need to get 450 kB of archives. After this operation, 2,642 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libtinfo-dev amd64 6.0+20160213-1ubuntu1 [77.4 kB] Get:2 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libncurses5-dev amd64 6.0+20160213-1ubuntu1 [175 kB] Get:3 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libncursesw5-dev amd64 6.0+20160213-1ubuntu1 [198 kB] Fetched 450 kB in 4s (104 kB/s) Selecting previously unselected package libtinfo-dev:amd64. (Reading database ... 175926 files and directories currently installed.) Preparing to unpack .../libtinfo-dev_6.0+20160213-1ubuntu1_amd64.deb ... Unpacking libtinfo-dev:amd64 (6.0+20160213-1ubuntu1) ... Selecting previously unselected package libncurses5-dev:amd64. Preparing to unpack .../libncurses5-dev_6.0+20160213-1ubuntu1_amd64.deb ... Unpacking libncurses5-dev:amd64 (6.0+20160213-1ubuntu1) ... Selecting previously unselected package libncursesw5-dev:amd64. Preparing to unpack .../libncursesw5-dev_6.0+20160213-1ubuntu1_amd64.deb ... Unpacking libncursesw5-dev:amd64 (6.0+20160213-1ubuntu1) ... Processing triggers for man-db (2.7.5-1) ... Setting up libtinfo-dev:amd64 (6.0+20160213-1ubuntu1) ... Setting up libncurses5-dev:amd64 (6.0+20160213-1ubuntu1) ... Setting up libncursesw5-dev:amd64 (6.0+20160213-1ubuntu1) ..
For Fedora Linux:
If you are using Fedora 22 Linux or the newer versions, you can use the dnf command to install the ncurses library, type:
$ sudo dnf install ncurses-devel
Outputs:
[root@fedora26-osetc ~]# dnf install ncurses-devel Last metadata expiration check: 1:36:24 ago on Fri 21 Jul 2017 09:01:19 AM EDT. Dependencies resolved. ================================================================================================================================================================================================== Package Arch Version Repository Size ================================================================================================================================================================================================== Installing: ncurses-devel x86_64 6.0-8.20170212.fc26 fedora 505 k Installing dependencies: ncurses-c++-libs x86_64 6.0-8.20170212.fc26 fedora 54 k Transaction Summary ================================================================================================================================================================================================== Install 2 Packages Total download size: 559 k Installed size: 930 k Is this ok [y/N]: y Downloading Packages: [MIRROR] ncurses-c++-libs-6.0-8.20170212.fc26.x86_64.rpm: Curl error (60): Peer certificate cannot be authenticated with given CA certificates for https://fedora-archive.ip-connect.vn.ua/fedora/linux/releases/26/Everything/x86_64/os/Packages/n/ncurses-c++-libs-6.0-8.20170212.fc26.x86_64.rpm [Peer's Certificate has expired.] [MIRROR] ncurses-devel-6.0-8.20170212.fc26.x86_64.rpm: Curl error (60): Peer certificate cannot be authenticated with given CA certificates for https://fedora-archive.ip-connect.vn.ua/fedora/linux/releases/26/Everything/x86_64/os/Packages/n/ncurses-devel-6.0-8.20170212.fc26.x86_64.rpm [Peer's Certificate has expired.] (1/2): ncurses-c++-libs-6.0-8.20170212.fc26.x86_64.rpm 13 kB/s | 54 kB 00:04 (2/2): ncurses-devel-6.0-8.20170212.fc26.x86_64.rpm 59 kB/s | 505 kB 00:08 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 55 kB/s | 559 kB 00:10 warning: /var/cache/dnf/fedora-6dbd63560daef6bf/packages/ncurses-devel-6.0-8.20170212.fc26.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 64dab85d: NOKEY Importing GPG key 0x64DAB85D: Userid : "Fedora 26 Primary (26) <fedora-26-primary@fedoraproject.org>" Fingerprint: E641 850B 77DF 4353 78D1 D7E2 812A 6B4B 64DA B85D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-26-x86_64 Is this ok [y/N]: y Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : ncurses-c++-libs-6.0-8.20170212.fc26.x86_64 1/2 Running scriptlet: ncurses-c++-libs-6.0-8.20170212.fc26.x86_64 1/2 Installing : ncurses-devel-6.0-8.20170212.fc26.x86_64 2/2 Running scriptlet: ncurses-devel-6.0-8.20170212.fc26.x86_64 2/2 Verifying : ncurses-devel-6.0-8.20170212.fc26.x86_64 1/2 Verifying : ncurses-c++-libs-6.0-8.20170212.fc26.x86_64 2/2 Installed: ncurses-devel.x86_64 6.0-8.20170212.fc26 ncurses-c++-libs.x86_64 6.0-8.20170212.fc26 Complete!
For Arch-based Linux:
You can use the following command to install ncurses library, type:
$ sudo pacman -S ncurses
Install Ncurses Library with Source code
You can use the source code to install the latest stable version of ncurses in your Linux system, just do the following steps:
#1 Download the latest source package of ncurses from ftp://ftp.gnu.org/gnu/ncurses/
According to the ncurses release notes, the latest version of nucrses is 6.1. so type the following command to get it to local disk:
$ wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.1.tar.gz
Outputs:
[root@osetc_test working]# wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.1.tar.gz --2018-11-08 07:08:11-- ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.1.tar.gz => “ncurses-6.1.tar.gz” Resolving ftp.gnu.org... 208.118.235.20, 2001:4830:134:3::b Connecting to ftp.gnu.org|208.118.235.20|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /gnu/ncurses ... done. ==> SIZE ncurses-6.1.tar.gz ... 3365395 ==> PASV ... done. ==> RETR ncurses-6.1.tar.gz ... done. Length: 3365395 (3.2M) (unauthoritative) 100%[===============================================================>] 3,365,395 2.64M/s in 1.2s 2018-11-08 07:08:13 (2.64 MB/s) - “ncurses-6.1.tar.gz” saved [3365395] [root@osetc_test working]# ls ncurses-6.1.tar.gz
#2 Extract the tar.gz source package
Type the following command:
$ tar -zxvf ncurses-6.1.tar.gz
Outputs:
[root@osetc_test working]# tar -zxvf ncurses-6.1.tar.gz ncurses-6.1/ ncurses-6.1/panel/ ncurses-6.1/mk-0th.awk ncurses-6.1/Makefile.in ncurses-6.1/c++/ ncurses-6.1/misc/ ncurses-6.1/man/ ncurses-6.1/Makefile.os2 ncurses-6.1/progs/ ncurses-6.1/aclocal.m4 ncurses-6.1/dist.mk ncurses-6.1/config.guess ncurses-6.1/config.sub ncurses-6.1/convert_configure.pl ncurses-6.1/ncurses/ ncurses-6.1/announce.html.in ncurses-6.1/mk-2nd.awk ncurses-6.1/package/ ....
#3 change directory to ncurses-6.1, and then compile and install the source code. type the following command:
$ cd ncurses-6.1 $ ./configure --prefix=/opt/ncurses $ make $ sudo make install
#4 Verify if the ncurses library is install in your system
Type the following command:
$ ls -la /opt/ncurses
Outputs:
[root@osetc_test ncurses-6.1]# ls -la /opt/ncurses total 24 drwxr-xr-x 6 root root 4096 Nov 8 07:18 . drwxr-xr-x. 4 root root 4096 Nov 8 07:17 .. drwxr-xr-x 2 root root 4096 Nov 8 07:18 bin drwxr-xr-x 3 root root 4096 Nov 8 07:18 include drwxr-xr-x 2 root root 4096 Nov 8 07:18 lib drwxr-xr-x 5 root root 4096 Nov 8 07:18 share [root@osetc_test ncurses-6.1]#
So far, the Ncurses library has been installed on your Linux system.
Compile C Porgram that call the ncurses library
Let’s write one C program called fio.c that call the ncurses library via vi/vim text editor under the command line interface in your Linux, and append the following line:
#include <ncurses.h> #include <stdio.h> int main() { initscr(); printw("Hello World!"); refresh(); getch(); endwin(); return 0; }
Compile this C program and then execute it, type:
$ gcc fio.c -o fio -lncurses $ ./fio