Getting C# on Friggin’ Linux Working

If you like C#, and want to program using it across all platforms–including Linux–it’s easy using the Mono runtime! The details of the Mono project I won’t go into detail here, but rather only show how to set up a “Hello world!” project that will build and run on Ubuntu. Note: MonoProject is an IDE for Mono projects. I highly recommend you install that.

Prerequisites:

There are three options you can use to get the source project onto the virtual machine: (1) Use Visual Studio to create a Hello World program, then set up a mapped folder in VirtualBox that contains the project. (2) Use Visual Studio to create a Hello World program, then use Git to clone the repository onto the guest machine. If you’d like, you can skip this step, and use the example at https://bitbucket.org/ken_domino/hw/overview. (3) Use MonoProject to create a Hello World program from scratch on Ubuntu.

Method:

  1. In your Windows host machine, start Visual Studio.
  2. In Visual Studio:
    1. Create a new C# console application within the mapped directory for the virtual machine.
    2. Add System.Console.WriteLine(“Hello world!”); to the main method.
    3. Compile and run to verify that it works.
  3. Let’s set up Mono on the guest machine. In the virtual machine:
    1. Open a Terminal.
    2. cd <mapped-directory>/<project-directory>
      1. Make sure the directory is mapped and you see the new project.
    3. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
      1. You are creating a package repository for Mono.
      2. These instructions are from the Mono Project install for Ubuntu.
    4. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
    5. sudo apt-get update
    6. sudo apt-get install mono-complete
  4. Let’s now build the Hello World program. Use one of the three methods I mentioned above. Then…
    1. xbuild
      1. xbuild” is Mono’s version of “msbuild“.
    2. mono bin/Debug/<project-executable>
      1. You should see the “Hello world!” in the terminal.
  5. Install the MonoDevelop application if you want to have an IDE for C# on Linux. It is similar to Xamarin Studio. You can then open the project in MonoDevelop.
    1. sudo apt-get install monodevelop
  6. Install the XSP4 server if you want to write ASP.NET projects.
    1. sudo apt-get install mono-xsp4

 

dokku@kukku:/vagrant/hw$ which mono
/usr/bin/mono
dokku@kukku:/vagrant/hw$ mono --version
Mono JIT compiler version 4.2.2 (Stable 4.2.2.30/996df3c Mon Feb 15 17:30:30 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen
dokku@kukku:/vagrant/hw$ which mono
/usr/bin/mono
dokku@kukku:/vagrant/hw$ mono --version
Mono JIT compiler version 4.2.2 (Stable 4.2.2.30/996df3c Mon Feb 15 17:30:30 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen
dokku@kukku:/vagrant/hw$ xbuild /target:Clean
XBuild Engine Version 12.0
Mono, Version 4.2.2.0
Copyright (C) 2005-2013 Various Mono authors

Build started 2/24/2016 9:11:49 AM.
__________________________________________________
Project "/vagrant/hw/hw.sln" (Clean target(s)):
	Target ValidateSolutionConfiguration:
		Building solution configuration "Debug|Any CPU".
	Target Clean:
		Project "/vagrant/hw/hw.csproj" (Clean target(s)):
/vagrant/hw/hw.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.
			Target CoreClean:
				Deleting file '/vagrant/hw/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs'
				Deleting file '/vagrant/hw/bin/Debug/hw.exe.config'
				Deleting file '/vagrant/hw/bin/Debug/hw.exe.mdb'
				Deleting file '/vagrant/hw/bin/Debug/hw.exe'
				Deleting file '/vagrant/hw/obj/Debug/hw.exe'
				Deleting file '/vagrant/hw/obj/Debug/hw.exe.mdb'
				Deleting file '/vagrant/hw/obj/Debug/hw.csproj.FilesWrittenAbsolute.txt'
		Done building project "/vagrant/hw/hw.csproj".
Done building project "/vagrant/hw/hw.sln".

Build succeeded.

Warnings:

/vagrant/hw/hw.sln (Clean) ->
(Clean target) ->
/vagrant/hw/hw.csproj (Clean) ->

	/vagrant/hw/hw.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.

	 1 Warning(s)
	 0 Error(s)

Time Elapsed 00:00:00.5173490
dokku@kukku:/vagrant/hw$ ls -R
.:
App.config  bin  hw.csproj  hw.sln  obj  Program.cs  Properties

./bin:
Debug

./bin/Debug:
hw.pdb  hw.vshost.exe  hw.vshost.exe.config  hw.vshost.exe.manifest

./obj:
Debug

./obj/Debug:
DesignTimeResolveAssemblyReferencesInput.cache  TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs  TempPE
hw.csproj.FileListAbsolute.txt                  TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
hw.csprojResolveAssemblyReference.cache         TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

./obj/Debug/TempPE:

./Properties:
AssemblyInfo.cs
dokku@kukku:/vagrant/hw$ xbuild /target:Rebuild
XBuild Engine Version 12.0
Mono, Version 4.2.2.0
Copyright (C) 2005-2013 Various Mono authors

Build started 2/24/2016 9:12:19 AM.
__________________________________________________
Project "/vagrant/hw/hw.sln" (Rebuild target(s)):
	Target ValidateSolutionConfiguration:
		Building solution configuration "Debug|Any CPU".
	Target Rebuild:
		Project "/vagrant/hw/hw.csproj" (Rebuild target(s)):
/vagrant/hw/hw.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.
			Target PrepareForBuild:
				Configuration: Debug Platform: AnyCPU
			Target GenerateSatelliteAssemblies:
			No input files were specified for target GenerateSatelliteAssemblies, skipping.
			Target CoreCompile:
				Tool /usr/lib/mono/4.5/mcs.exe execution started with arguments: /noconfig /debug:full /debug+ /optimize- /out:obj/Debug/hw.exe Program.cs Properties/AssemblyInfo.cs obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs /target:exe /define:"DEBUG;TRACE" /nostdlib /platform:AnyCPU /reference:/usr/lib/mono/4.5/System.dll /reference:/usr/lib/mono/4.5/System.Xml.Linq.dll /reference:/usr/lib/mono/4.5/System.Data.DataSetExtensions.dll /reference:/usr/lib/mono/4.5/Microsoft.CSharp.dll /reference:/usr/lib/mono/4.5/System.Data.dll /reference:/usr/lib/mono/4.5/System.Net.Http.dll /reference:/usr/lib/mono/4.5/System.Xml.dll /reference:/usr/lib/mono/4.5/System.Core.dll /reference:/usr/lib/mono/4.5/mscorlib.dll /warn:4
			Target _CopyAppConfigFile:
				Copying file from '/vagrant/hw/App.config' to '/vagrant/hw/bin/Debug/hw.exe.config'
			Target DeployOutputFiles:
				Copying file from '/vagrant/hw/obj/Debug/hw.exe.mdb' to '/vagrant/hw/bin/Debug/hw.exe.mdb'
				Copying file from '/vagrant/hw/obj/Debug/hw.exe' to '/vagrant/hw/bin/Debug/hw.exe'
		Done building project "/vagrant/hw/hw.csproj".
Done building project "/vagrant/hw/hw.sln".

Build succeeded.

Warnings:

/vagrant/hw/hw.sln (Rebuild) ->
(Rebuild target) ->
/vagrant/hw/hw.csproj (Rebuild) ->

	/vagrant/hw/hw.csproj:  warning : Project has unknown ToolsVersion '14.0'. Using the default tools version '4.0' instead.

	 1 Warning(s)
	 0 Error(s)

Time Elapsed 00:00:01.1014250
dokku@kukku:/vagrant/hw$ ls -l bin/Debug/hw.exe
-rwxrwxrwx 1 dokku dokku 4096 Feb 24 09:12 bin/Debug/hw.exe
dokku@kukku:/vagrant/hw$ mono !$
mono bin/Debug/hw.exe
Hello world!
dokku@kukku:/vagrant/hw$ 

 

For more information: