Interactive C# on Linux
dotnet linux

How to use dotnet tool to run C# commands interactively on a Linux (Ubuntu) machine
February 6, 2019

C# interactive is a handy tool inside Visual Studio (powered by Roslyn), which allows you to experiment with blocks of C# code without the need to load up whole new solution. This post is about dotnet-script, a global dotnet CLI tool, which works everywhere where .NET Core runs and provides the same functionality as C# interactive.

Why? If you do cross-platform development with .NET Core (which is perfecly feasible and you should totally do it), you might get into situations where you need to test how the framework behaves on different platforms. Typical are filesystem paths, where .NET constants return different values on Windows, Mac and Linux. In my case I wanted to see where the active user’s profile is on each operating system, by using the Environment.SpecialFolder enumeration.

Install .NET Core SDK

Follow instructions for your repo. I used Ubuntu 18.04, so my command sequence was:

wget -q
sudo dpkg -i packages-microsoft-prod.deb

sudo add-apt-repository universe
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.2

dotnet --version
> 2.2.103

Install dotnet-script

With dotnet SDK installed, it’s time to install the dotnet-script global tool:

dotnet tool install -g dotnet-script
> You can invoke the tool using the following command: dotnet-script
> Tool 'dotnet-script' (version '0.28.0') was successfully installed.

Verify path

It might happen (aka “happened to me”) that the installer doesn’t update PATH properly. Dotnet global tools are installed to the $HOME/.dotnet/tools directory, so this should be added to PATH:

echo $PATH
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/martin/.dotnet/tools

Not there? Just run:

export PATH=$PATH:$HOME/.dotnet/tools



Finally, it should be possible to run:


And start scripting:

> 2+2
> Console.WriteLine("Hello");
> Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));


I didn’t need it for my case, but there are some advanced features to the REPL interface too:

NuGet packages

> #r "nuget: Newtonsoft.Json"
> using Newtonsoft.Json;
> ....

Reset to initial state


And more…

comments powered by Disqus