install-rubyisascriptthatreliablyconfiguresyourMacsoyoucaninstallRubygems(likeBundler,Jekyll,Rails),andswitchbetweenmultipleversionsofRuby.
Itcanberunmultipletimesonthesamemachinesafely.Itinstalls,upgrades,orskipspackagesbasedonwhatisalreadyinstalledonthemachine.
ImportantIfyoucameherefrommyansweronStackOverflow,youmostlikelywantmylaptopscriptinstead,unlessyouknowforsurethatallyouneedisRuby.
Afterrunningeitherthisscriptorthelaptopscript,makesuretorestartyourterminalforthechangestotakeeffect.
MoregoodiesJointhe1300+peopleonmylistwhoarebecomingconfidentcodersthroughmyfreeweeklycodingguidesandexclusivetutorialsandcourses.Checkoutmycustomizablelaptopscriptthatinstallsmoreessentialdevelopmenttools,aswellastheJekyllandRailsgems.Irecommendthelaptopscriptformostpeople.What'ssupportedSupportedchips:
AppleSiliconM1IntelSupportedoperatingsystems:
BigSurCatalinaMojaveUnsupportedoperatingsystems.Giveitashot,butIcan'tguaranteeitwillwork.
macOSHighSierra(10.13.x)macOSSierra(10.12.x)OSXElCapitan(10.11.x)OSXYosemite(10.10.x)OSXMavericks(10.9.x)Supportedshells:
bashfishzshWhatitsetsupBundlerformanagingRubygemschrubyformanagingRubyversions(recommendedoverRVMandrbenv)Homebrewformanagingoperatingsystemlibraries(whichalsoinstallstheprerequisiteApplecommandlinetools)ruby-installforinstallingdifferentversionsofRubyInstallIMPORTANT!CHECKALLOFTHEITEMSBELOWBEFOREANDAFTERRUNNINGTHESCRIPT!
CheckprerequisitesMakesureyourcomputermeetsallprerequisitesfirst.
IfyouareonanM1Mac,donotuseRosettaHomebrewworksnativelyonM1Macs.MakesurethatwhateverterminalappyouuseisnotinRosettamode.ReadmyguideoninstallingRubyonAppleSiliconformoredetails.
QuitandrelaunchTerminalafterrunningmyscriptImentionthisseveraltimesinthisREADME,aswellaswhenthescriptfinishessuccessfully,butI'llsayitagain.Forthechangestotakeeffect,youhaveto"refresh"yourterminal.Thebestwayistoquitandrelaunchit.
NowontotheinstallationBeginbyopeningtheTerminaloriTermapplicationonyourMac.TheeasiestwaytoopenanapplicationinmacOSistosearchforitviaSpotlight.ThedefaultkeyboardshortcutforinvokingSpotlightiscommand-Space.OnceSpotlightisup,starttypingthefirstfewlettersoftheappyouarelookingfor,andonceitappears,pressreturntolaunchit.
InyourTerminalwindow,runthefollowingcommands:
cd~curl--remote-namehttps://raw.githubusercontent.com/monfresh/install-ruby-on-macos/master/install-ruby/usr/bin/envbashinstall-ruby2>&1|tee~/laptop.logThescriptitselfisavailableinthisrepoforyoutoreviewifyouwanttoseewhatitdoesandhowitworks.
NotethatthescriptmightaskyoutoenteryourmacOSpasswordatvariouspoints.ThisisthesamepasswordthatyouusetologintoyourMac.ThepromptcomesfromHomebrew,becauseitneedspermissionstowritetothe/usr/local(or/opt/homebrewonM1Macs)directory.
Oncethescriptisdone,quitandrelaunchTerminal.
DebuggingYourlastrunofthescriptwillbesavedtoafilecalledlaptop.loginyourhomefolder.Readthroughittoseeifyoucandebugtheissueyourself,withthehelpoftheTroubleshootingErrorsWikiarticle.Ifnot,copytheentirecontentsoflaptop.logintoanewGitHubIssue(orattachthewholelogfiletotheissue)formeandI'llbegladtohelpyou.
HowtotellifthescriptworkedIfthelastthingthescriptdisplayedwas"Alldone!",theneverythingthescriptwasmeanttodoworked.Nowmakesureyouquitandrestartyourterminal.
ToverifythattheRubyenvironmentisproperlyconfigured,runoneormoreofthesecommands:
ruby-vThisshouldshowruby2.7.2orruby3.0.0.Ifnot,tryquittingandrelaunchingTerminal.Thentryswitchingmanuallyto2.7.2:
chruby2.7.2andchecktheversiontodoublecheck:
ruby-vThencheckwhereRubyisinstalled:
whichrubyThisshouldpointtothe.rubiesdirectoryinyourhomefolder.Forexample:
/Users/monfresh/.rubies/ruby-2.7.2/bin/rubyHowtoswitchbetweenRubyversionsandinstalldifferentversionsBydefault,thescriptinstallsRuby2.7.2.Ifyourunitagain,itwillalsoinstallRuby3.0.0.Fornow,ifyou'renotanexperiencedRubyist,IrecommendusingRuby2.7.2becausesomegemslikeJekyllaren'tyetfullycompatiblewithRuby3.0.0.
Toinstallanolderversion,runruby-installfollowedbyruby-andthedesiredversion.Forexample:
ruby-installruby-2.7.2Toswitchtothisnewly-installedversion,runchrubyfollowedbytheversion.Forexample:
chruby2.7.2Ifthisdoesn'twork,tryquittingandrestartingyourterminal.
Anotherwaytoautomaticallyswitchbetweenversionsistoadda.ruby-versionfileinyourRubyprojectwiththeversionnumberprefixedwithruby-,suchasruby-2.7.2.Totestthatthisworks:
cdintoafolderoutsideofyourprojectRunchruby3.0.0(orsomeotherversionthatisnottheonespecifiedinyour.ruby-version)Verifythatyouareusing3.0.0withruby-vcdintoyourprojectVerifythatyouareusingthespecifiedversionwithruby-vNotethatgemsonlygetinstalledinaspecificversionofRuby.Ifyouinstalledjekyllin3.0.0,andthenyouinstall2.7.2forexample,you'llhavetoinstalljekyllagainin2.7.2.
WhyInstallingRubyand/orgemsisacommonsourceofconfusionandfrustration.SearchforYoudon'thavewritepermissionsforthe/Library/Ruby/Gems/2.3.0directoryor"commandnotfound"inyourfavoritesearchengine,andyouwillseepagesandpagesofresults.
Tomakemattersworse,thevastmajorityofsuggestionsarebadadviceandincomplete.ThereasonfortheerrormessageaboveisbecausepeoplearetryingtoinstallgemsusingtheversionofRubythatcomespre-installedbyApple.Thaterrormessageisthereforareason:youshouldnotmodifymacOSsystemfiles.Acommonsuggestionistobypassthatsecurityprotectionbyusingsudo,whichisnotsafeandcancauseissuesdownthelinethatarehardtoundo.
TherecommendedwayofusingRubyonaMacistoinstallanewer(themacOSversionisoftenoutdatedandisonlyupdatedduringamajorrelease),separateversioninadifferentfolderthantheonethatcomesbydefaultonmacOS.ThebestandmostflexiblewaytodothatiswithaRubymanager.Themostpopularonesare:RVM,rbenv,andchruby,andasdf.Ihavechosenchrubyinthisscript.Seebelowformyreasons.Therearedifferentwaystoinstallthesetools,andtheyallrequireadditionalconfigurationinyourshellstartupfile,suchas.bash_profileor.zshrc.
WhenattemptingtoinstallandconfigureaRubymanagermanually,it'seasytomissorfumbleastepduetohumanerrororincompleteoroutdatedinstructions.Sinceallofthestepsareautomatable,thebestandmostreliablewaytosetupRubyonaMacistorunascriptliketheoneI'vewritten.ItestitregularlyonmysparelaptopwhereIdeletetheharddriveandinstallfreshversionsofmacOS.Ifyou'vealreadyattemptedtosetupadevelopmentenvironmentonyourMac,andyourunintoissueswithmyscript,pleasereadthroughtheTroubleshootingErrorsarticle.Ifthatdoesn'thelp,feelfreetoopenanissue,andIwilldomybesttohelpyou.
ReadmoreinmydefinitiveguidetoinstallingRubygemsonaMac.
WhychrubyandnotRVMorrbenv?Itisthesmallest,mostreliable,andeasiesttounderstand.Ilikethatitdoesnotdosomeofthethingsthatothertoolsdo:
Doesnothookcd.Doesnotinstallexecutableshims.DoesnotrequireRubiestobeinstalledintoyourhomedirectory.DoesnotautomaticallyswitchRubiesbydefault.Doesnotrequirewrite-accesstotheRubydirectoryinordertoinstallgems.Otherfolkswhopreferchruby:
https://kgrz.io/programmers-guide-to-choosing-ruby-version-manager.htmlhttps://stevemarshall.com/journal/why-i-use-chruby/https://linhmtran168.github.io/blog/2014/02/27/moving-from-rbenv-to-chruby/
评论