如何通过Terminal在SSH环境下安装Visual Studio for Mac?
Hey there, let's work through this to get your C# compiling up and running via SSH on your Mac. Since you're working entirely in the terminal (no GUI), we'll need to handle the .dmg installation and setup through command-line tools. Here's a step-by-step breakdown:
First, we need to mount the downloaded Visual Studio for Mac .dmg in terminal-only mode:
- Create a mount point directory (we'll use this to access the dmg contents):
mkdir -p /Volumes/VSforMac - Mount the dmg file to this directory (replace
/path/to/your/VisualStudio.dmgwith the actual path to your file):
Thehdiutil attach /path/to/your/VisualStudio.dmg -mountpoint /Volumes/VSforMac -nobrowse-nobrowseflag prevents Finder from popping up (which we don't need in SSH). - Verify the mount worked by listing the contents:
You should see something likels /Volumes/VSforMacVisual Studio Installer.appin the output.
Visual Studio for Mac's installer supports command-line arguments for headless (GUI-less) installation. We'll specify the core components needed for C# compilation:
- Navigate to the installer's executable directory:
cd /Volumes/VSforMac/Visual\ Studio\ Installer.app/Contents/MacOS - Run the installer with the required components (this includes Mono, which provides the
mcscompiler):sudo ./Installer --install --components MonoFramework-MDK,MonoFramework-MDK.mcs,VisualStudioMac-Core,VisualStudioMac-CSharpMonoFramework-MDK: The core Mono runtime and SDK (includesmcs)VisualStudioMac-Core: Basic VS for Mac core functionalityVisualStudioMac-CSharp: C# language support
- Let the installer run—this can take a while, so make sure your SSH session stays active (use
screenortmuxif you're worried about disconnecting).
Once installation finishes, we need to make sure mcs is available in your terminal's PATH:
- Unmount the
.dmgfile:hdiutil detach /Volumes/VSforMac - Check the Mono installation path (it's usually at
/Library/Frameworks/Mono.framework/Versions/Current/bin). Add this to your PATH temporarily to test:export PATH="/Library/Frameworks/Mono.framework/Versions/Current/bin:$PATH" - To make this permanent, edit your shell configuration file (
.bash_profilefor Bash,.zshrcfor Zsh):
Add the samenano ~/.zshrc # or ~/.bash_profileexport PATH=...line at the bottom, save (Ctrl+O) and exit (Ctrl+X). Then reload the config:source ~/.zshrc # or source ~/.bash_profile - Test if
mcsworks:
If you see version output, you're good to go!mcs --version
If you only need to compile C# code and don't want the full IDE, you can skip VS for Mac entirely:
Option 1: Install .NET SDK (modern C# support)
Download the .NET SDK.pkgand install via terminal:# Replace with the latest .NET SDK URL for macOS curl -O https://download.visualstudio.microsoft.com/download/pr/abc123/dotnet-sdk-7.0.400-osx-x64.pkg sudo installer -pkg dotnet-sdk-7.0.400-osx-x64.pkg -target /Then add .NET to your PATH:
export PATH="$HOME/.dotnet:$PATH"Use
dotnet buildordotnet runto compile/run C# code instead ofmcs.Option 2: Install Mono only (for
mcsspecifically)
If you already have Homebrew installed, this is quick:brew install monoThis will set up
mcsin your PATH automatically.
- Permission errors: Make sure you use
sudowhen running the installer or modifying system paths. mcsnot found: Double-check the Mono path is added to your PATH, and restart your terminal if needed.- Headless installation fails: Check the installer logs at
~/Library/Logs/VisualStudioInstaller/to find specific error messages.
内容的提问来源于stack exchange,提问作者Phannakan

