You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法安装R包:非零退出状态(仅sudo运行R时可安装)

Fixing R Package Installation Permission Issues (Without Sudo)

Hey there, let's break down why you're hitting permission errors when installing R packages like mlbench without using sudo to run R. This is a super common snag, and we've got a few straightforward solutions to get you back on track.

Why This Happens

The root issue is that your regular user account doesn't have write access to R's default system-wide library directory. When you run R with sudo, you're using administrator privileges that let you modify that system directory—but this isn't ideal long-term, since it can lead to permission conflicts later on.

This is the safest and most maintainable approach, as it keeps your packages isolated to your user account.

  • First, check your current library paths in R by running:

    .libPaths()
    

    You'll see two types of paths: a system-wide one (you can't write to this without sudo) and possibly a user-specific one (if it exists).

  • If you don't have a user library, create one in your home directory via terminal:

    mkdir -p ~/R/library
    
  • Tell R to prioritize this user library:

    • Temporary fix (for the current R session only):
      .libPaths(c("~/R/library", .libPaths()))
      
    • Permanent fix (applies to every R session):
      Edit your R profile file (~/.Rprofile) and add the line above. If the file doesn't exist, create it with touch ~/.Rprofile in terminal first.
  • Now try installing mlbench again:

    install.packages("mlbench")
    

    The package will install to your user library, no sudo required.

If you really need to install packages to the system library, you can adjust the directory permissions—but be warned, this affects all users on the system and could cause unexpected issues.

  • First, find your system library path using .libPaths() in R (it's usually the first entry, like /usr/lib/R/library).

  • Change ownership of the directory to your user account via terminal:

    sudo chown -R $USER:$USER /usr/lib/R/library
    

    Replace the path with your actual system library path if it's different.

Solution 3: Use Conda to Manage R Environments

If you work with multiple R versions or package sets, using Conda to create isolated environments is a great option.

  • Create a new Conda environment with R:

    conda create -n my_r_env r-base
    
  • Activate the environment:

    conda activate my_r_env
    
  • Now run R within this environment—you can install packages freely without sudo, since everything is contained in your user's Conda directory.

Quick Note

Avoid using sudo to run R and install packages whenever possible. It can lead to permission errors when running scripts later (since your regular user might not have access to the sudo-installed packages) and messes with system-wide R configurations.

内容的提问来源于stack exchange,提问作者bearoplane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:24:54