Setting up a Shiny Development Environment within Linux on Windows 10
While I was getting Ruby on Rails to work nicely under Ubuntu on Windows 10 I took the opportunity to set up my *nix bash environment, which was largely using defaults. Yes, I know I can use zsh or fish or other shells. Yes, I know I can use emacs and screen, but I am using Vim and tmux. Fight me. Anyway, once my post was done, I starting messing around with open source .NET Core on Linux (it runs on Windows, Mac, and Linux, but here I'm running on Linux on Windows. #Inception) and tweeted a pic of my desktop.
By the way, I feel totally vindicated by all the interest in "text mode" given my 2004 blog post "Windows is completely missing the TextMode boat." ;)'
Also, for those of you who are DEEPLY NOT INTERESTED in the command line, that's cool. You can stop reading now. Totally OK. I also use Visual Studio AND Visual Studio Code. Sometimes I click and mouse and sometimes I tap and type. There is room for us all.
WHAT IS ALL THIS LINUX ON WINDOWS STUFF? Here's a FAQ on the Bash/Windows Subsystem for Linux/Ubuntu on Windows/Snowball in Hell and some detailed Release Notes. Yes, it's real, and it's spectacular. Can't read that much text? Here's a video I did on Ubuntu on Windows 10.
A number of people asked me how they could set up their WSL (Windows Subsystem for Linux) installs to be something like this, so here's what I did. Note that will I've been using *nix on and off for 20+ years, I am by no means an expert. I am, and have been, Permanently Intermediate in my skills. I do not dream in RegEx, and I am offended that others can bust out an awk script without googling.
So there's a few things going on in this screenshot.
- Running .NET Core on Linux (on Windows 10)
- Cool VIM theme with >256 colors
NortonMidnight Commander in the corner (thanks Miguel)- Desqview-esque tmux splitter (with mouse support)
- Some hotkey remapping, git prompt, completion
- Ubuntu Mono font
- Nice directory colors (DIRCOLORS/LS_COLORS)
Let's break them down one at a time. And, again, your mileage may vary, no warranty express or implied, any of this may destroy your world, you read this on a blog. Linux is infinitely configurable and the only constant is that my configuration rocks and yours sucks. Until I see something in yours that I can steal.
Running .NET Core on Linux (on Windows 10)
Since Linux on Windows 10 is (today) Ubuntu, you can install .NET Core within it just like any Linux. Here's the Ubuntu instructions for .NET Core's SDK. You may have Ubuntu 14.04 or 16.04 (you can upgrade your Linux on Windows if you like). Make sure you know what you're running by doing a:
~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.2 LTS
Release: 16.04
Codename: xenial
~ $
If you're not on 16.04 you can easily remove and reinstall the whole subsystem with these commands at cmd.exe (note the /full is serious and torches the Linux filesystem):
> lxrun /uninstall /full
> lxrun /install
Or if you want you can run this within bash (will take longer but maintain settings).
NOTE that you'll need Windows 10 Creators Edition build 16163 or greater to run Ubuntu 16.04. Type "winver" to check your build.
sudo do-release-upgrade
Know what Ubuntu your Windows 10 has when you install .NET Core within it. The other thing to remember is that now you have two .NET Cores, one Windows and one Ubuntu, on the same (kinda) machine. Since the file systems are separated it's not a big deal. I do my development work within Ubuntu on /mnt/d/github (which is a Windows drive). It's OK for the Linux subsystem to edit files in Linux or Windows, but don't "reach into" the Linux file system from Windows.
Cool Vim theme with >256 colors
That Vim theme is gruvbox and I installed it like this. Thanks to Rich Turner for turning me on to this theme.
$ cd ~/
$ mkdir .vim
$ cd .vim
$ mkdir colors
$ cd colors
$ curl -O https://raw.githubusercontent.com/morhetz/gruvbox/master/colors/gruvbox.vim
$ cd ~/
$ vim .vimrc
Paste the following (hit āiā for insert and then right click/paste)
set number
syntax enable
set background=dark
colorscheme gruvbox
set mouse=a
if &term =~ '256color'
" disable Background Color Erase (BCE) so that color schemes
" render properly when inside 256-color tmux and GNU screen.
" see also http://snk.tuxfamily.org/log/vim-256color-bce.html
set t_ut=
endif
Then save and exit with Esc, :wq (write and quit). There's a ton of themes out there, so try some for yourself!
Norton Midnight Commander in the corner (thanks Miguel)
Midnight Commander is a wonderful Norton Commander clone that Miguel de Icaza started, that's licensed as part of GNU. I installed it via apt, as I would any Ubuntu software.
$ sudo apt-get install mc
There's mouse support within the Windows conhost (console host) that bash runs within, so you'll even get mouse support within Midnight Commander!
Great stuff.
Desqview-esque tmux splitter (with mouse support)
Tmux is a terminal multiplexer. It's a text-mode windowing environment within which you can run multiple programs. Even better, you can "detach" from a running session and reattached from elsewhere. Because of this, folks love using tmux on servers where they can ssh in, set up an environment, detach, and reattach from elsewhere.
NOTE: The Windows Subsystem for Linux shuts down all background processes when the last console exits. So you can detach and attach tmux sessions happily, but just make sure you don't close every console on your machine.
Here's a nice animated gif of me moving the splitter on tmux on Windows. YES I KNOW YOU CAN USE THE KEYBOARD BUT THIS GIF IS COOL.
Some hotkey remapping, git prompt, completion
I am still learning tmux but here's my .tmux.conf. I've made a few common changes to make the hotkey creation of windows easier.
#remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# split panes using | and -
bind | split-window -h
bind _ split-window -v
unbind '"'
unbind %
bind k confirm kill-window
bind K confirm kill-server
bind < resize-pane -L 1
bind > resize-pane -R 1
bind - resize-pane -D 1
bind + resize-pane -U 1
bind r source-file ~/.tmux.conf
# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# Enable mouse control (clickable windows, panes, resizable panes)
set -g mouse on
set -g default-terminal "screen-256color"
I'm using the default Ubuntu .bashrc that includes a check for dircolors (more on this below) but I added this for git-completion.sh and a git prompt, as well as these two alias. I like being able to type "desktop" to jump to my Windows Desktop. And the -x on Midnight Commander helps the mouse support.
alias desktop="cd /mnt/c/Users/scott/Desktop"
alias mc="mc -x"
export CLICOLOR=1
source ~/.git-completion.sh
PS1='\[\033[37m\]\W\[\033[0m\]$(__git_ps1 " (\[\033[35m\]%s\[\033[0m\])") \$ '
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM="auto"
Git Completion can be installed with:
sudo apt-get install git bash-completion
Ubuntu Mono font
I really like the Ubuntu Mono font, and I like the way it looks when running Ubuntu under Windows. You can download the Ubuntu Font Family free. Right click the downloaded TTF files and right click and "Install," or drag them into C:\windows\fonts. Then click the upper left corner of any Bash console window and change your font to Ubuntu Mono.
Nice directory colors (DIRCOLORS/LS_COLORS)'
If you have a black command prompt background, then default colors for directories will be dark blue on black, which sucks. Fortunately you can get .dircolors files from all over the wep, or set the LS_COLORS (make sure to search for LS_COLORS for Linux, not the other, different LSCOLORS on Mac) environment variable.
I ended up with "dircolors-solarized" from here, downloaded it with wget or curl and put it in ~. Then confirm this is in your .bashrc (it likely is already)
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
Download whatever .dircolors file makes you happy (make sure the filename ends up as ".dircolors," so you may need to cp yoursourcefile ~/.dircolors and then restart your console.
Make a big difference for me, and as I mention, it's totally, gloriously, maddeningly configurable.
Leave YOUR Linux on Windows tips in the comments!
Sponsor: Did you know VSTS can integrate closely with Octopus Deploy? Watch Damian Brady and Brian A. Randell as they show you how to automate deployments from VSTS to Octopus Deploy, and demo the new VSTS Octopus Deploy dashboard widget. Watch now
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
That feeling when developing Controller/Actions in .Net Core on windows feels like you are hacking into the FBI is awesome...
I think even if you're primarily a Windows user WSL has features you might want to use. The great thing about Linux is there's so much built in that's convenient for a developer - for example, contrast setting up SSH on linux - one line "ssh-copy-id" compared with setting things up using Putty which is a bit of a chore.
That said WSL still feels like it's a proof of concept at the moment - I hope it's not and becomes a core part of the developer's arsenal but folks seem curious rather than ecstatic about it. I'd be interested to see any stats on how much it's being taken up. Perhaps you guys could use it a bit more at dev conferences and talks. I noticed on Twitter Charles Nutter has ditched his Mac after 10 years, maybe get someone like him to give a Ruby talk using WSL! Just a thought :-)
I think the whole point Mr Gates made was to surround a user with 'friendly'(or not) windows to make less-keyboard-savvy people comfortable with a computer.
So... maybe we can have a special version of Windows that can be renamed Characters for us keyboard wizards.
However, I like how you can split screens into multiple areas and quickly move windows between them, so I wrote a tool in C# to do exactly that with GUI apps. Literally published it yesterday for the first time.
I'd be proud if you check it out: https://rink.hockeyapp.net/apps/6037e69fa4944acc9d83ef7682e60732
Cool thing is that you define your screen layout in WPF's XAML. Started working on it, because my favorite GridMove is no longer developed anymore.
It would be nice to have a more modern terminal console, a-la Terminator, the current one is a bit rudimentary, but they are getting there.
MS, give-it tabs + split views and my soul is sold!
I currently use ConEmu, but there is a lot of configuration to be done to make-it proper.
I have a problem with connection to DB
My connection string is
Server=(localdb)\mssqllocaldb;Database=dbname;User=login;Password=pass;MultipleActiveResultSets=true
And my ASP.NET Core application, launched form Ubuntu on Windows, throws an exception:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 25 - Connection string is not valid) --->
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
This app launched from Windows works fine.
If I change conn. string like this
Server=[Remote IP address];Database=dbname;User=login;Password=pass;MultipleActiveResultSets=true
It works fine from Ubuntu.
I think, it has some problem with connection string which conatins instance of DB.
I have a problem with connection to DB
My connection string is
Server=(localdb)\mssqllocaldb;Database=dbname;User=login;Password=pass;MultipleActiveResultSets=true
And my ASP.NET Core application, launched form Ubuntu on Windows, throws an exception:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 25 - Connection string is not valid) --->
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
This app launched from Windows works fine.
If I change conn. string like this
Server=[Remote IP address];Database=dbname;User=login;Password=pass;MultipleActiveResultSets=true
It works fine from Ubuntu.
I think, it has some problem with connection string which conatins instance of DB.
(formatting was added)
I tried to upgrade my current 14.04 LTS to 16.04 LTS, but found out that full functionality of 16.04 is really only in the Creators Edition of Windows, so you may want to mention that to folks before they try "sudo do-release-upgrade" and it gets them nowhere (at least that's where it got me).
On git prompt, the step where .git-completion.sh is required, it was complaining (even after running sudo apt-get install git bash-completion) when I ran source .bashrc to reload the bash setting. I had to google up where I can get .git-completion.sh and finally I tried downloading https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash and mv it to ~/.git-completion.sh. That seems to work.
On the Ubuntu Mono font, once you download the files from the repo in Windows (not in bash), just install each font (*.ttf) by opening it one by one by clicking Install in the preview or drag all of them to C:\Windows\Fonts folder to get it installed. Once that is done, open up the property of your Bash console and change the font from there to Ubuntu Mono. That should do it.
On dircolors, going to the GitHub repo, didn't quite help either. No clear instruction on how make this work. I ended up cloning the repo in ~/dircolors-solarized and doing cp dircolors.256dark ~/.dircolors to finally get it to work.
Another caveat... I tried this on trusty (Ubuntu 14.x) and the .tmux.config set -g mouse ... line will scream at you.
Better to do the upgrade to xenial (Ubuntu 16.x) first. And lastly, you should really upgrade to Windows 10 Creator Update before trying this. Because when trying to do lxrun /uninstall /full and lxrun /install keeps installing trusty instead of xenial when I tried. Couldn't get it to upgrade to xenial at all and sudo do-release-upgrade on trusty kept crashing after certain point, not sure why.
Hope that helps for Ubuntu newbies like myself :)
Thanks for the tip about full functionality of 16.04 only being available in Creators Edition. I suspected that would be the case but your comment has saved me a bunch of time confirming that.
If you haven't tried the tools above I'd recommend giving them a shot.
# If not running interactively, don't do anything
case $- in
...
esac
# If tmux isn't already running start it
[[ -z "$TMUX" ]] && exec tmux
Bam! Now, according to Microsoft naming convention, I have "tmux on Bash on Ubuntu on Windows".
LocalDB (as the name implies) is local to your application (ie no TCP, only named pipes). As far as I know, there is no LocalDB for Linux. But, who knows in the future as SQL Server is already running on Linux.
Setup instructions for SQL Server under Ubuntu is here: https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-ubuntu
However, I was not able to install and configure the server under WSL. It failed in point #4 (got core dumped)
In your case I would recomend using SQL Server Express running under Windows.
For more information about LocalDB read her: https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/
// Ronny
Serving files from /mnt/c/ rather than /home/ is significantly slower, and makes it almost not worth it for me.
Any ideas to speed this up? Best I've found is using unison to sync files from /mnt but that's hardly ideal, as it's not instant.
Seems that you found a geek's "Eldorado" (and it is of course) ...now. That "Eldorado" is available from early '90 years...
Trust me, I am an old just retired IT and I worked in many environments, not excluding Windows and Linux course, but this seems to me a bit a forcing.
I really would like hear an answer from you on that, please!
Peace
David
It would be nice to use new Win10/S2016 features (WSL, Docker), but unless you're working at Microsoft or a startup, it seems like there's a booby-trap at every corner (WSL bugs persisting for six months because WSL can't be updated until the OS is updated, Docker not actually supporting any of the features required to use Docker as a service platform [swarm, overlays], etc.)
http://ranger.nongnu.org/
I've been blogging about it as well, currently using WSLtty (MinTTY version for WSL from MinTTY folks themselves) as my terminal emulator combined with tmux.
http://blog.jreypo.io/personal/microsoft/windows%2010/migrating-from-macos-to-windows-10/
http://blog.jreypo.io/containers/microsoft/windows%2010/my-docker-workflow-in-wsl/
If you want your prompt to mirror posh-git you can use the following.
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
In your ~/.bashrc:
source ~/.git-prompt.sh
PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$'
https://github.com/starlight/swan-desktop
But unfortunately while this brings a lot of great tools to the Windows world, anything Windows-subsystem-for-Linux is Windows 10 only, and thus inevitably suffers from the same complete lack of control, privacy and ultimately confidence that finally pushed me to grit my teeth and push through the rough patches to properly 'convert' to Linux in the first place.
Swan Desktop is based off Cygwin, and as such, loses the advantages of the WSL. However, you're missing the point here. We can argue over whether or not your concerns about control and privacy are valid, but the important point here is that the WSL meets the requirements. It lets people have access to the various userland binaries as well as the familiar Windows desktop environment and toolchain people are productive with. If you can give a reason why Swan is better than the WSL, maybe people would switch.
So add the following to .bashrc?
source ~/.git-prompt.sh
source ~/.git-completion.bash
Also, would be helpful to say where to get these:
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
Looking at how much you've tweaked tmux, you might want to check out byobu. It's basically a wrapper around tmux that provides nicer shortcuts, mouse support and a handful of other useful stuff. (And it's available as an Ubuntu package).
The filesystem speed is just not there. Cant work with things 5-10x slower.
Once that is fixed it will be awesome.
Did you mistyped ā_ā(underscore) for "-"(dash) in splitting tmux vertically?
@Pete Smith,
Awesome tip on RANGER. I hadn't seen that before. So much better than `mc` for vi users.
@Scott Hanselman
Thank you for all of the progress you're making on WSL... Still a long ways to go, but many of the basics are there.
I am writing just to thank you for inspiring me not only professionally (technically) but personally as human being as well, with your humbled way of doing this presentation.
This is something that is not learn-able..it's just something one has or not.
Good luck in everything.
Comments are closed.