cygwin · Linux · Server · Windows

Cygwin walkthrough and beginners guide – Is it Linux for Windows or a POSIX compatible alternative to PowerShell?


November 2017 Update: If you are using Windows 10, I would instead suggest reading my posts Ubuntu Linux on Windows 10 how to or openSUSE Linux on Windows 10 how to which goes through loading Linux on Windows using official Microsoft services.

Linux in Windows?

Cygwin is an awesome tool for agnostic operating system users who regularly use both Windows and Linux but want the power of a Linux shell and its tools running within Windows. It is an excellent replacement for the primitive Windows command prompt without the need to learn a new collection of commands or a new scripting language. A requirement which is needed for the Microsoft developed and recommended Command Prompt alternative PowerShell.

Cygwin is POSIX (Portable Operating System Interface for uniX) compliant which means it shares a standard API with all the other compliant operating systems. It does not say applications compiled for other POSIX systems such as Linux software will automatically work within Cygwin. To add new POSIX software you still need to compile the software source code under Cygwin using its compilers and libraries. This process is a requirement for most POSIX compliant operating systems e.g. software compiled for FreeBDS (Unix) will not work under Debian/Ubuntu (Linux).

Cygwin will work on any x86 32-bit or x86-64 editions of Windows NT from NT 4.0 Service Pack 4 (1998) onwards to Windows 7 including 2000, 2003, 2008, XP and Vista. There is no support for the Windows 16/32-bit hybrids such as 95, 98 or ME.

Installation

So let’s get started. Visit http://cygwin.com/install.html, download then run the setup.exe. This file is a Windows-based package manager for Cygwin. By default, it will download and install the latest minimal install of Cygwin, but on future launches, it will also enable you to add new packages and update your existing Cygwin software. So after you install Cygwin do not delete or lose the location of setup.exe!

Cygwin Net Release Setup Program (setup.exe).
For Choose A Download Source select Install from Internet.
Select Root Install Directory.

By default Cygwin will install to C:\cygwin but you can change this to any directory of your choosing. I recommend that you do not choose a directory path which contains spaces in the name, so both C:\Program Files\cygwin or C:\Program Files (x86)\cygwin are out.

Select Local Package Directory.

Cygwin setup will save all downloaded packages to this folder after it has finished the installation. You can then either delete them, leave them in this directory or back them up to a USB stick for future use on this or other computers.

Select Your Internet Connection.

Unless otherwise needed you should leave the Internet Connected selection with the default selection of Direct Connection.

Choose A Download Site.

Choose A Download Site lets you select a file repository from where Cygwin setup will download the packages and files. It is preferable to pick a download location from a site that is physically close to your own. But if you can not determine this from the country codes in the URL or cannot find a suitable mirror then just select the first item.

Selecting packages

Select Packages.

This Select Packages screen is the most critical part of the Cygwin setup. It is the Cygwin package manager interface, equivalent to Synaptic Package Manager in Debian and Ubuntu or YUM Extender in Fedora Linux. The screen capture has some arrows pointing to features of the interface, but as a first time user you can completely ignore all of these and press Next to accept the default installation.

  • The dark green solid arrow points to the search dialogue enabling you to quickly filer packages by name. Unfortunately, the search does not work on the package descriptions.
  • The solid black arrow has four radio buttons enabling you to mass-apply installation restrictions on all packages.
  • Keep tells Cygwin setup not to update any installed packages.
  • Prev is short for previous which reverts all packages to the previous version.
  • Curr is the default and recommended option as it installs the current version of all selected packages.
  • Exp will install the most current or experimental version of all selected packages.
  • The red arrows point to the manager categories which can be expanded to reveal individual packages.
  • The purple arrow points to version numbers which belong to packages that are already installed onto Cygwin.
  • Orange arrow enables you to manually override the global package version (black arrow radio buttons).
  • Skip means the package has not been installed and setup will ignore it.
  • Keep means the package has been installed but the setup will ignore it.
  • Reinstall will overwrite an existing package installation with the same version of a fresh download.
  • 1.0-1 or some other version number means setup will install this version of the package for the first time.
  • Source means setup will ignore the installed package but will download the source codes.
  • Uninstall removes the installed package from your Cygwin installation.
  • The yellow arrow points to a Binary check-box while the light green points to a Source check-box. These check-boxes only appear only when available in the package New column.
  • The light blue arrow is the package download size in Kilobytes.
  • Dark blue contains the package name and a brief description.
Progress.

Cygwin setup will now download and install all the default-install packages. Wait until the Progress screen is complete and go to the Next screen.

Create Icons & Install Complete.

Create Icons enable you to add Desktop and Start Menu shortcuts, once finished use either shortcut to launch Cygwin.

Bash terminal

Welcome to Bash.

Welcome to your Bash prompt in Windows! You can test this by running Bash version.

bash --version
Display your version of Bash.

The first thing I’d recommend doing is customising your bash shell to implement some aliases for basic colouring on a few staple Cygwin commands.

dir -A ~

Will reveal some hidden setting files in your home directory which you can edit these using a text editor, but please avoid using the standard Windows Notepad. I use Windows Notepad++ and would edit w:\cygwin\home\Ben\.bashrc, but you would probably need to update c:\cygwin\home\[User name]\.bashrc. Windows may have issues opening a file which it sees as only having an extension but lacking a file name. If this is the case, just use the Open With dialogue but make sure you have the Always use the selected program to open this kind of file check-box left unchecked.

The yellow highlights the settings I would recommend to un-comment.
Bash settings file after the comments have been removed.

Once you have finished your changes, you can apply them by reloading Bash.

bash --login

Test your new alias and option defaults.

cd /
ll

You now see a colourised directory and file listing of the Cygwin root.

Line list of root.

This is the same directory as you chose during the Cygwin setup installation which by default is c:\cygwin. You can use either Windows File Explorer, Windows command prompt or Cygwin to modify the contents of the root. For more details on how Cygwin handles POSIX permissions within Windows and other potential incompatibilities, I’d recommend reading the Using Windows Security in Cygwin chapter in the manual.

Tips

By default, there is no Cygwin clear command (cls in command prompt), but the easiest way to clean your terminal is to press [Ctrl]  l.

Use the [Tab] key on your keyboard to auto-complete commands, file and directory names. This Bash feature is superior to the basic Tab auto-complete used by Windows command prompt.

Did you also know you can chain 2 or more commands together using a semi-column ; character as the separator?

cd /; echo "Root Directory"; ls; cd ~; echo "My Home Directory"; ls

And if you didn’t realise in the command above, change directory forward-slash cd / takes you to the Cygwin root, while change directory tilde cd ~ takes you to your user home directory.

Drill down of the Cygwin root

  • Cygwin.bat is the Windows batch file used to launch a Cygwin Bash prompt from a Windows command prompt.
  • Cygwin.ico is a standard Windows icon file attached to the Cygwin batch file.
  • bin/ is a directory containing binary (execute) applications that have been compiled for use on Windows under Cygwin. In here are all the unique POSIX-like commands and applications that give you all that extra functionality that a bare-Windows install often lacks.
  • cygdrive/ contains a list of mounted Windows drive letters. From here you can access any drive partition that is viewable to Windows. For example cygdrive/c/Windows/ would be the location of most peoples Windows installation.
  • dev/ is short for devices. Traditionally for POSIX, this is where your detected hardware devices are listed, but in Cygwin its usage is mainly there for software compatibility and can be ignored. http://cygwin.com/cygwin-ug-net/using-specialnames.html#pathnames-posixdevices
  • etc/ contains all your Cygwin related configuration files. Unlike the Windows registry, these should all be plain text files that can be edited within any Windows text editor (such as NotePad++) or a Cygwin editor such as VM or Nano.
  • home/ contains the user directories which are not unlike the Windows User directories. It is where your non-Cygwin files such as documents, file downloads should be stored.
  • lib/ is an abbreviation for libraries. These are compiled, shared components used by various Cygwin commands and applications but you can ignore this directory.
  • proc/ is a virtual directory that allows you to display various aspects of your system using the cat command.
  • cat /proc/cpuinfo Displays your computer CPU details.
  • cat /proc/meminfo Displays your Windows memory information.
  • cat /proc/mounts Lists your disk mounts within Cygwin.
  • tmp/ is the Cygwin temporary directory.
  • usr/ is the bread and butter of your Cygwin install.
  • usr/bin/ is identical to bin/ that was listed previously.
  • usr/include/ are programming header files which are somewhat like shared libraries, and you can ignore this.
  • usr/local/ is a location where you can compile applications for yourself, again ignore if this does not interest you.
  • usr/sbin/ contains system binaries and applications tools for Cygwin system administration.
  • usr/share/ are platform independent read-only shared files such as documentation. For example /usr/share/fonts contain a collection of system fonts. /usr/share/doc contains application documents.
  • usr/src/ is a location for storing application source code.
  • usr/ssl/ is a location for storing SSL (Secure Socket Layer) and TLS (Transport Layer Security) certificates which are used for Internet communication encryption and security.
  • usr/tmp/ is the same temporary folder as /tmp.
  • var/ is where variable data files are stored. These are files generated by yourself or applications. For example, all log files would go in /var/log. If you ran an Apache HTTP daemon, the files it hosted would be contained in /var/www.

POSIX and Cygwin commands

Now the usage of a Bash shell or POSIX-like commands is out of the scope of this article. You can run dir /bin to see all the .exe execute commands available to you within Cygwin. Like Windows command prompt you do not need to include the file extension when running an application, dir works the same as dir.exe.

If you need quick help on the usage of a command, use [command] –help e.g. dir –help. Many commands share the same options.

If you think the default Cygwin bash shell is a bit ugly, then you are not alone. Microsoft does not give the default command prompt much love, and unfortunately, this is what Cygwin relies on for its terminal interface. You can change this though by again running Cygwin setup.exe. When you are back in the Select Packages screen, do a search for mintty. Select to install the mintty binary package and once complete go to your Cygwin start menu. There should be a new mintty option in addition to the Cygwin Bash Shell.

Install Mintty Terminal.
Cygwin under command prompt is not that attractive.
Cygwin under Mintty allows for font smoothing.
From now on use Mintty for a cleaner terminal interface to Cygwin.

Services and daemons

Want to implement a service or daemon into your Cygwin install? I will show a simple example using Lighttpd, the light-weight HTTP server that is an alternative to Apache2 HTTP.

Run Cygwin setup.exe again to search and install lighttpd and make sure you say yes to install any resolving dependencies. Once installed you will find the application residing in the usr/sbin/ directory as lighttpd.exe. An example configuration file lighttpd.conf.default can be found in etc/lighttpd/. We will create an empty configuration file using the touch command.

cd /etc/lighttpd
touch lighttpd.conf

Now use your Windows notepad application to edit c:/cygwin/etc/lighttpd/lighttpd.conf and copy the settings listed below.

The GitHub repository of the code entries used in this article https://github.com/bengarrett/devtidbits/tree/master/post_590.

server.document-root = "/var/www/servers/www.example.org/pages/"
server.port = 80

server.username = "www"
server.groupname = "www"

mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)

static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" )
index-file.names = ( "index.html" )

$HTTP["host"] == "www2.example.org" {
server.document-root = "/var/www/servers/www2.example.org/pages/"
}

This config in use is from the Lighttpd Configuration Tutorial.

Now attempt to launch lighttpd with your new configuration file.

/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf

Our attempt will fail because the web server’s document root directory does not exist, so let’s create that and let us also make an index.html document and try it again.

mkdir /var/www ; mkdir /var/www/servers ; mkdir /var/www/servers/www.example.org ; mkdir /var/www/servers/www.example.org/pages
touch /var/www/servers/www.example.org/pages/index.html

In a Windows notepad application edit, c:/cgywin/var/www/servers/www.example.org/pages/index.html and copy/paste the following HTML code.

index.html content.

Let’s retry running Lighttpd again.

/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf

Once Lighttpd has loaded you may need to press the Enter key to restore your Bash prompt, but Lighttpd will continue to run in the background. Now point your local Windows web browser to 127.0.0.1 or localhost.

Chrome browser viewing our primitive HTML file.

When you want to shutdown Lighttpd, find the PID (Process Identifier) and kill it.

ps -s
kill [PID number]
Discover and shutdown of the Lighttpd service.

Congratulations! You have successfully implemented a Bash shell environment on your Windows operating system that also has a robust but simple HTTP server.

Popular Cygwin applications

Other popular Cygwin packages that may be of interest.

Servers services such as Apache, BIND, PostgreSQL, Pure-FTPd, OpenSSH.
Programming languages such as C, C++, Perl, Python, Ruby, TCL.
Remote log-in tools SSH, RSH, Telnet.
File transfer tools FTP, SCP, rsync, unison, rtorrent.
In addition to an extensive range of network, system and file diagnostics tools.

More information on Cygwin is found in its User’s Guide.

 

16 thoughts on “Cygwin walkthrough and beginners guide – Is it Linux for Windows or a POSIX compatible alternative to PowerShell?

  1. All through the article, the word “complaint” is used for “compliant”.

    On the first occurrence, I read the sentence 3 times, trying to make sense of it, before realizing what you’d done.

    I searched for a means, such as e-mail, to notify you privately, but found none.

  2. Excellent guide! First one I’ve seen and tried where everything actually worked as expected and described. Now if someone could just do the same for installing and compiling gcc as every guide I’ve seen and followed results in a failed make of gcc in the end (Eventually want to be able to compile latest Apache and run it on top of Windows 8).

    1. Hi Thomas, thanks for the comments and I am glad this 2011 article worked for you. Unfortunately I stopped using Cygwin a number of years ago as I was only using it for its ported POSIX programs and not its compiling abilities.

      1. That’s OK, still one of the better articles I’ve seen on Cygwin. Starting to give up on trying to use it though as hardly ANY source will compile for me, guess I’ll have to go full hog with Linux as it’s likely less trouble in the end.

        1. I think it would be the way to go especially if you run open source web servers such as Apache or Nginx. Personally I use Windows as a desktop and ‘headless’ Linux for a server software. You can use a virtualization environment on Windows such as VirtualBox to run a terminal/shell aka server edition of Linux even with its own IP address. That way you get the best of both worlds. Ubuntu Server is probably a good bet if you are not familiar with Linux as its documentation & support is very extensive.

  3. Why are you messin’ with IIS? I got it working fine on Windows by following this guy’s instructions.

  4. Am running an IIS Server with Cygwin installed. I went thru this setup for a subdomain and I’m getting unable to bind to port 80, operation not permitted….. tried it again after shutting down IIS…. same issue.

    1. I haven’t used IIS Server in a very long time but I assume its Windows service has locked down port 80 for its own use, even after the server is shutdown.

      Try changing the default port on Lighttpd or IIS Server.

      For Lighttpd in Cygwin:

      nano -B etc/lighttpd/lighttpd.conf

      Change the line

      server.port = 80

      To something unused say

      server.port = 8080

      Save, exit nano and then try running lightttpd again.

      /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf

  5. Seems to be working now. Didn’t know you had to replace all the contents with the stuff above.

  6. Also, your ” Perl, Python, Ruby” links under “Popular Cygwin Applications” are broken.

  7. Very useful post but when I re-run setup lighttpd isn’t there. Tried downloading it but I’m not sure what to do with it.

      1. The Services and Daemons example is generating a lot of “Duplicate config variable in conditional 0 global:” I’ve commented out several that were already in the file before pasting the example above. Not sure what I did wrong here….

Leave a comment