How to Setup Bash on Windows 10 for Developers – Ultimate Setup

Bash for Windows 10 has been out for almost a year now. It has been incredible for a developer like myself to be able to use Bash natively on a Windows machine!

In this article, I’ll show you how I install my main Bash setup for Windows 10.

 

Requirements

  1. Your Windows 10 PC must be running a 64-bit version of Windows 10 Anniversary Update build 14393 or later.
    To find your PC's CPU architecture and Windows version/build number, open Settings>System>About. Look for the System type and OS Build fields.


    In this screenshot, I have a 64-bit operating system, x64-based processor and OS Build, 15063.138, so I fit the requirements.

 

Steps

  1. Install Bash on Windows 10
  2. Install ConEmu for tabs
  3. Must have Bash customizations
  4. Install VcXsrv Windows X Server for opening GUI applications

 

1. Install Bash on Windows 10

In order to run Bash on Windows, you will need to manually:

  1. Turn on Developer Mode
  2. Enable the “Windows Subsystem for Linux (beta)” feature

Open Settings -> Update and Security -> For developers
Select the Developer Mode radio button.

Developer mode is selected.

From Start, search for “Turn Windows features on or off” (type ‘turn’)
Select Windows Subsystem for Linux (Beta)

Windows Subsystem for Linux (Beta) is checked.

Hit OK. Installation will proceed, and afterwards, you will have to restart your computer.

After you restart your computer, you will be able to finish installing Bash on Windows 10.

Search for Microsoft Store.

From the Microsoft Store, find Ubuntu and download and install it.

From Start, search for “bash” (type ‘bash’) or it might already be open.

When you open bash, you will see a command prompt window.

Type "y" to continue.

The real “Bash on Ubuntu on Windows” shortcut will be installed. Bash on Ubuntu on Windows might already be installed through the Microsoft Store.

Now, you can open “Bash on Ubuntu on Windows” whenever you search for “bash”

Try opening Bash.  The first time you install Bash on Windows, you will be prompted to create a UNIX username and password.

After typing in a UNIX user name and UNIX password, you can use Bash the same way that you would use a terminal on Mac and Linux.

The biggest problem though is there are no tabs! Let’s fix the tab issue.

 

2. Install ConEmu for tabs

ConEmu is an amazing program that gives you terminal tabs, allows you to copy and paste the normal way with CTRL-C and CTRL-V, and run any combinations of shells including Command Prompt, Bash, Powershell, and more.

https://conemu.github.io/

Hi the Download button, which will take you to a redirected list of versions of ConEmu. I like to use the Preview version.

Run the executable. Select the x64 bit version of installation.

Hit Next on the next few prompts, which are the intro window, license and agreement, and settings. I leave everything by default.

Now, you are ready to install. Hit Install.

Hit Finish on the next prompt.

From Start, search for “ConEmu” (type ‘ConEmu’)

When ConEmu first starts, you will be brought to the Startup Settings. You have to select the default console that you want to open.

Find and select {Bash::bash} in the Specified named task section.

Now, whenever you open ConEmu, it will open to Bash by default! ConEmu is easy to read and smooth.

I like to pin ConEmu to the taskbar to be able to open bash quickly. Right click ConEmu’s icon and click Pin to taskbar.

To add a new tab on ConEmu, type WIN+W.

To switch to the right tab, type CTRL+Tab.

To switch to the left tab, type CTRL+Shift+Tab.

All ConEmu’s shortcuts are listed: http://conemu.github.io/en/KeyboardShortcuts.html

 

3. Must have Bash customizations

Bash on Windows 10 is great, but it could use a few customizations to make using it with Windows 10 files and programs easier!

First, we will adjust our ~/.bashrc.

nano ~/.bashrc

Use any text editor. On nano, type ALT+/ to go to the end of the file.

These configurations allow easy access to some of my favorite Windows related directories and programs.

I want to be able to change directory to the Windows C: drive easily and access my desktop, so I use aliases.

alias converts the given word into shortcuts.

I also want to be able to use Sublime, which is my go-to GUI text editor, so that I can type subl . and open the current working directory with Sublime.

I am using Sublime 3https://www.sublimetext.com/3

I want to be able to open any folder with File Explorer with open, so that I can open the current working directory with open .

I have a shortcut called linux to open the Ubuntu’s root folder with File Explorer.

Lastly, I have an alias called reload for reloading the ~/.bashrc to apply any new configurations.

Replace huyle with the username of your computer.

# Custom shortcuts
alias windows="cd /mnt/c"
alias desktop="cd /mnt/c/Users/huyle/Desktop"
alias subl="/mnt/c/Program\ Files/Sublime\ Text\ 3/subl.exe"
alias open="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe /c start -WorkingDirectory C:'\\'Users'\\'huyle'\\'AppData'\\'Local'\\'lxss"
alias linux="open C:/Users/huyle/AppData/Local/lxss"
alias reload="source ~/.bashrc"

These other shortcuts I use to access my favorite directories. I recommend you to add your favorite Windows or Ubuntu directories where you put your development code or work.

# Optional shortcuts
alias repos="cd /mnt/c/Users/huyle/Desktop/Repos"
alias work="cd /mnt/c/Users/huyle/Desktop/Work"
alias youtube="cd /mnt/c/Users/huyle/Desktop/YouTube"

You can save your file with nano by using CTRL+O and then pressing Enter.

Now, we can apply these changes.

source ~/.bashrc

Use the windows alias to easily switch to your Windows files! You can try some of these other aliases!

open .

The open alias does not work with Ubuntu’s directories because of directory translation errors, but by default, at least, I set the alias to open the Ubuntu root directory.

Every now and then, I prefer using Sublime, so it’s very convenient to be able to type subl . in order to open current working directories with Sublime.

Here are some extra utilities that I like to install on Bash. These utilities are optional.

I like to use tmux, which is a terminal multiplexer. It lets you switch easily between several programs in one terminal.

I like tabs, but every now and then, I like everything in one terminal for a bird’s eye view.

I also like to use zip and unzip often to create zip files and extract them.

sudo apt-get install -y tmux zip unzip

Nice! Bash on Windows 10 with tabs and tmux feels so good. Your development is now super-charged!

 

4. Install VcXsrv Windows X Server for opening GUI applications

If you have ever wanted to open GUI applications through SSH connections, this step is for you.

By default, Windows 10 does not come with an X11 server by default. I’ve been using VcXsrv Windows X Server, but Xming also works.

You can download VcXsrv Windows X Server here:

https://sourceforge.net/projects/vcxsrv

Hit the Download green button.

I leave the settings by default.

You can open VcXsrv by searching for “vcx” on Windows 10 search.

I Allow access for VcXsrv Public networks.

Now that we have our X11 server open, we should adjust our ~/.bashrc a little bit to make sure that the DISPLAY value will be correct.

vim ~/.bashrc
# X11 server display value
export DISPLAY=localhost:0.0

Let’s test if our X11 server works! I like to install x11-apps for testing.

x11-apps contains xclock, which is great for testing GUI applications and X11.

sudo apt-get install -y x11-apps

Now, let’s run xclock.

xclock

Last but not least, let’s make sure that X11 will work on top of SSH connections.

mkdir ~/.ssh
vim ~/.ssh/config
Host *
    ForwardAgent yes
    ForwardX11 yes

Now, save the ssh config file and change its permissions correctly.

chmod 600 ~/.ssh/config

Afterwards, you should be able to use -X with ssh in order to enable the forwarding of X11 connections to VcXsrv.

ssh -X [email protected]_address

Great job! Good luck with your development. I’ve been finding this setup very comfortable as a developer!

C++ Libraries: Linking and Compiling

What is the -I, the -L, and the –(little L)? Why am I getting so many errors? What’s going on underneath?

C and C++ libraries can be thought of as a conglomeration of functions and new keywords that you are able to use in your code.

To truly understand what libraries are made of, we should first refresh on header and source files.

 

Header and Source File Example

Header files contain declarations of functions and variables and often have the file extension .h.

Source files contain the definitions of these functions and often have the file extension .cpp or .cc.

As a brief refresher the example below contains a header, source file, and main file for displaying a Birthday.

SP Libraries 1

 

Birthday.h

Declares a class called birthday.

 

 

 

 

SP Libraries 2

 

Birthday.cpp

Defines the two Birthday functions specified above.

 

 

 

SP Libraries 3

 

main.cpp

Creates an instance of the birthday class, and calls the function printDate.

 

 

 

SP Libraries 4

 

The file structure consists of a main directory containing the .cpp files and the Makefile, and then a separate subdirectory containing the header file as shown:

 

 

Here is the Makefile to compile and run the code:

SP Libraries 5

make
./executable

Output:

SP Libraries 6

The Makefile is splitting up the compilation process into two stages.

First the .cpp files are being turned into object files with a .o extension, then there is a linking stage where the main.o and the Birthday.o files are linked together to create an executable.

But why was the $(INC) there? When is the $(INC) necessary?

In C++, the #include macro at the beginning of programs tells the preprocessor to replace the instance of #include with the contents of the file.

For example, the main.cpp file includes the Birthday.h header file so that it can reference the declarations of the functions Birthday and printDate.

These functions are not fully defined by the header file, but they are prototyped and able to be referenced without throwing syntax errors.

The new file main.cpp file after being preprocessed is pictured below:

SP Libraries 7

Understanding this, we realize that the preprocessor must be able to access the included file in the compilation.

The file is referenced as Birthday.h, and if the pre-processor were to search the current directory for the file, it would be unable to find it because relative to the main directory the Birthday.h file has the path Headers/Birthday.h.

Therefore, the -I flag tells the preprocessor where it should search for files when compiling.

In this case the flag is including the Headers folder, resulting in the discovery of the file and the replacement of the #include.

The Birthday.o file is created the same way with the same -I, as it must also reference the Birthday.h file for the function declarations it is defining.

Next, linking occurs. After the two object files are created, the Birthday.o object contains functions that the main.o file calls.

The Birthday.o object file now contains both the declaration and the definitions after the preprocessor inserted the contents of the .h file.

Now, we must “link” the two object files together so that the functions called by main.o can be executed from the definitions in Birthday.o.

There is no flag for this linking. They are simply linked together by listing the name of the files.

g++ main.o Birthday.o -o executable

 

Back to Libraries

How does this relate to libraries? Libraries are just a grouped together set of .o files.

This means that when compiling they require inclusion of library header files when you create a .o file, as well as a linking stage when creating an executable.

Let’s take a look at how we can link to a library called Google test, which performs modular code testing for files. For more information on Google testing visit our post here.

SP Libraries 8
Example file using #include

This file will simply run the Test to check to see if two plus two equals 4. This can be compiled by:

g++ -I(path to googletest)/include/ -c Test.cc

(Creates object file. The full path to the gtest.h header file is (path to googletest)/include/gtest/gtest.h)

g++ -pthread Test.o (path to libgtest.a)/libgtest.a

(Links to the library the same way as before, it is just a precompiled set of object files)

*Google testing uses pthreading underneath and the -pthread flag is unrelated to this tutorial

./a.out

An alternate way to link to the library is with the -L and -l commands.

g++ -I(path to googletest)/include/ -c Test.c

(Same as before.)

g++ -pthread Test.o -L(path to libgtest.a) -lgtest

(-L path to library *not including the library name* -l library name *excluding the prefix, lib, and suffix, .a*)

SP Libraries 9

WARNING:
The creation of an object file and linking must occur in two different steps.

The first step includes the preprocessor replacing all header files and checking for syntax/function references (the -I flag).

The next step is the actual linking to the definitions of the function references(the -L and -l flag).

There are ways that you can try to do these steps together, and sometimes it will work with flags like -WL, and -Wl, but this practice is considered unscalable, unprofessional, and tenuous, potentially outputting many cryptic errors.

 

Some libraries do not require explicit inclusion or linking becuase the compiler g++ already knows where to search.

For example, iostream is actually a header file which operates the same way as any other library.

It’s placement is computer specific, but mine is stored in /usr/include/c++/4.8/iostream along with other standard c++ header files.

The library itself is stored elsewhere, for me it is in usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a or libstdc++.so.

There are system variables in place which predefine search paths and linking paths for the compiler to follow by default.

In the case of iostream and the c++ library, the compiler already knows to include the header files and link automatically so that you don’t have to explicitly link for each program.

These default paths can be output to the console and the default paths can be adjusted for making it easier to link to a library for a project.

cpp -v

SP Libraries 11

(The default include path for your system)

ld –verbose | grep SEARCH

SP Libraries 10

(The default library search path *Takes the place of -L*)

One final important concept in this tutorial is that of static and dynamic libraries. As shown, some libraries can be linked to without using the -L and -l flags, simply by specifying the path to the library.

A static library can be compiled either way specified above, but a shared library cannot.

The major difference is that shared object libraries are loaded into a program at run time whereas static libraries are preloaded into the executable itself.

This make an executable compiled with a .a library self-contained whereas a program compiled with a .so library must be able to access the .so at runtime.

A .so library ALWAYS requires the -L and -l syntax because of the compilers need to know where the library is located and to link to it.

Because .a libraries can also function using the -L and -l flags, it is recommended that you stick with them for every link to a library.

Learning how to deal with libraries is crucial to figuring out how IDE’s are working underneath and can be an invaluable skill for any Linux based software development job.

I hope you enjoyed the tutorial and if you have further questions please post them below!

How to Get Multiple Terminals on Ubuntu Server

Let’s say that you want multiple terminals on an Ubuntu server. If you have ever used the Ubuntu server version of the OS, you should know that it is maneuvered only through the terminal. We had a situation where we didn’t have access to the Internet, so we couldn’t install something like tmux for multi-tasking, and we thought screen was hard to use.

We needed multiple terminals, so that we could run multiple commands at the same time!

If you are using a keyboard and monitor with Ubuntu server, it so happens that you can use ALT + F1ALT + F6 to spawn new terminals to do your work. The keyboard shortcuts switch you to a new terminal window. You can have 6 of these terminals, which are basically the equivalent of terminal tabs on Ubuntu with the GUI.

In my case, I was using Ubuntu 14.04, but it should work with the other versions as well.

ubuntu-1404-server

ALT + F2 to spawn terminal #2 and so on

How to Use Sublime Text 2 From Terminal on Mac OS

Sublime Text 2? The editor that developers, developers, developers like to use. At the very least, Sublime is a common text editor for the everyday programmer. How can you use Sublime on your terminal? If you’re any programmer, you’re maneuvering through the terminal left and right, all day everyday, so you need to have an easy way to use sublime from the terminal!

By default, for Mac OS X, you don’t have Sublime binded to a command on the terminal! Let’s do show you how to do that.

Installing Sublime

You can install the stable version of Sublime on their website.

http://www.sublimetext.com/2

 

How to Set-up

We want to be able to type in:

subl

and open files.

Open a new terminal. First, we will bind the newly installed Sublime 2 (you can also do this for Sublime 3) as the subl command. We’re using a symbolic link from the binary within the Application to what we will make as globally accessible.

sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

OR you can bind sublime to a sublime command for clarity instead of the abbreviation.

sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime

The symbolic link will create an accessible subl command to open sublime and use it to open files or folders.

We will open ~/.bash_profile with a text editor and make sure that this new sublime command will be ready whenever we open the terminal.

open -a TextEdit ~/.bash_profile

With .bash_profile, we will set the PATH variable to look inside the /usr/local/bin folder where we created the sublime link. Type the following if it is not in your ~/.bash_profile.

By exporting the path, ~/.bash_profile will refresh and export the folder for accessible binary commands whenever we open a new terminal.

Now save and exit. The first time we do this, we will not have access to the sublime command immediately. We first need to source, which means to reload the ~/.bash_profile in order to have access to our sublime command!

source ~/.bash_profile

How to Use

Open a terminal and use any of the variations of the command:

Open sublime
subl
Open a file
subl file
Open a folder
subl folder/
Open the current directory
subl .

Enjoy your new sublime command! Makes things much more convenient if you enjoy using sublime!

8 Steps to Install Cygwin on Windows

Cygwin provides the Linux feeling on Windows. With Cygwin, you can get a sizable Linux collection of GNU and Open Source tools including a terminal that supports POSIX interface on Windows.

Step 1)

Check whether your System type is 32 bit or 64 bit. If you have Windows 8 or 8.1, right click on This PC on your Desktop, then click on Properties. If you don’t have This PC on your Desktop, you can also use Windows’ search to look for “This PC“, then right click the icon, and click on Properties.

On the Properties of This PC, you can see the System Type underneath System category. For me, System type: 64-bit Operating System, x64-based processor. I have a 64 bit operating system.

Right Click on This PC, Click on Properties, and Look at your System type.

 

Step 2)

Go to https://cygwin.com/install.html

Select the blue linked executable setup based on your system type of Step 1. Download will start automatically after clicking on one of the blue executable setup links.

Click on the executable setup based on your System type. Download starts automatically.

 

Step 3)

Open the executable Cygwin setup file. Run the set-up as normal. Click on Next on the Cygwin Setup screen.

When choosing Installation Type, select Install from Internet (default). Click Next.

When choosing the Installation Directory, you can leave the root directory by default, which will be C:\cygwin64 (64 bit). Install for all users. Click Next.

The Local Package Directory contains the setup installation files for packages that you wish Cygwin to have. Underneath Select Local Package Directory, the default is the directory where you have the setup executable.

You can leave this by default.

For Setup your Internet Connection, use Direct Connection for your Internet Connection if you’re using wi-fi or ethernet. Click next.

Choose a Download Site. Select any of the download sites. Click Next.

Cygwin Setup

 

Step 4)

You’ll now be at Cygwin Setup – Select Packages. From here, you can search for packages that you wish Cygwin to have. The ones that I recommend are:

For each of these packages, they belong to categories. Search for a package. Click on the + next to a category to expand the contents of the search. Click on the circle twirl with the arrows to select the most up-to-date version of that package. Package names are on the right with a short description of that package.

Example of selecting the latest version of nano to install.

 

Step 5)

After selecting a version to install for all your packages, you can click the next button at the bottom right of the setup screen. At the Resolving Dependencies screen, make sure that the checkbox that says Select required packages (RECOMMENDED) is checked. Click Next. The download and installation of your desired packages will commence.

Resolving Dependencies.

 

Step 6)

After you finish the download and installation, you can check Create an icon on Desktop. Click Finish. You’ll find a program called Cygwin64 Terminal on your desktop. Open the program. Cygwin is installed.

Installation of packages has finished. Finish.
When you open Cygwin for the first time, Cygwin initializes.

 

Step 7)

You can also add the Cygwin packages and POSIX interface for the Windows command prompt. In other words, you can make the command prompt act like the terminal in Linux.

Right click on “This PC” and click Properties if it’s on the Desktop. Also, you can use Windows’ search for “This PC” and right click, then click on Properties.

Click on Advanced System Settings in this Properties window.

In the System Properties that pops up, click on Environment Variables, which will be within the Advanced tab. Inside Environment Variables, look underneath the System variables section. Scroll through and look for the Variable with the name Path. Click on that line. Click on the Edit button underneath. For 64 bit, add a “;C:\cygwin64\bin;” to the end of the Variable value line. This allows the command prompt to work with Cygwin’s directory path to the Cygwin’s bin folder and utilize Cygwin’s packages and POSIX interface. Click OK for each of the three windows to save this setting.

Cygwin’s path variable to work with the command prompt.

 

Step 8)

Open a command prompt. You can use Windows’ search and search for command prompt or cmd. Click on command prompt, and a command prompt black box will open. You will have all the functionality of Cygwin within the command prompt. You can choose to use either Cygwin or a command prompt to use the linux tools or POSIX interface.

Displaying SSH on a Windows command prompt.