remrin

remrin

github
email

Setting Up an Independent Linux Development Environment on MacOS

Preface#

Using a Linux development environment on Mac OS is not difficult, as Mac OS itself is a Unix-like system. With HomeBrew, most Linux software packages can be installed directly. Even without using a virtual machine, a relatively good development experience can be achieved. However, there may be special requirements, or one may not want to pollute the host machine's development environment. In that case, one can use tools like VMware Fusion, Parallels Desktop, or VirtualBox to install a complete Linux distribution in a virtual machine. However, these types of virtual machines are relatively heavy, slow to start, and consume a lot of memory, so they are not within my consideration. Of course, one can also use Docker Desktop to directly use various Linux distribution images or create a Dev Container to create a fully equipped development image. However, currently, there is no one-size-fits-all solution for the issues with Docker image sources, so that is also not within my consideration. Ultimately, I decided to use Mechines in Orbstack to set up a development environment.

Installing Orbstack#

You can directly download the installation package from the official website or install it using HomeBrew.

brew install orbstack

Choosing a Suitable Distribution#

I am using Fedora here.
orbstack software
Fedora

Improving the Development Environment#

  1. The first step is of course to change to the
    Tsinghua source. First, back up the default source.
sudo cp -r /etc/yum.repos.d /etc/yum.repos.d.backup

Then directly replace the default source.

sudo sed -e 's|^metalink=|#metalink=|g' \
  -e 's|^#baseurl=http://download.example/pub/fedora/linux|baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora|g' \
  -i.bak \
  /etc/yum.repos.d/fedora.repo \
  /etc/yum.repos.d/fedora-modular.repo \
  /etc/yum.repos.d/fedora-updates.repo \
  /etc/yum.repos.d/fedora-updates-modular.repo

If you prefer to change it manually:

  • Fedora repository (/etc/yum.repos.d/fedora.repo)

    [fedora]
    name=Fedora $releasever - $basearch
    failovermethod=priority
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/
    metadata_expire=28d
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
    skip_if_unavailable=False
    
  • Updates repository (/etc/yum.repos.d/fedora-updates.repo)

[updates]
name=Fedora $releasever - $basearch - Updates
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/Everything/$basearch/
enabled=1
gpgcheck=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
  • Fedora-modular repository (/etc/yum.repos.d/fedora-modular.repo)

    [fedora-modular]
    name=Fedora Modular $releasever - $basearch
    failovermethod=priority
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/releases/$releasever/Modular/$basearch/os/
    enabled=1
    metadata_expire=7d
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
    skip_if_unavailable=False
    
  • Updates-modular repository (/etc/yum.repos.d/fedora-updates-modular.repo)

[updates-modular]
name=Fedora Modular $releasever - $basearch - Updates
failovermethod=priority
baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/updates/$releasever/Modular/$basearch/
enabled=1
gpgcheck=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-$basearch
skip_if_unavailable=False
```

  • Clean the cache.

    sudo dnf clean all
    sudo dnf makecache
    
  • Update packages.

    sudo dnf update
    

  1. Use zsh to replace the default bash.
  • Install git and zsh.
sudo dnf install git zsh
  • Install oh my zsh.
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • Install commonly used oh my zsh plugins.

    • Auto-suggestions.
    git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
    
    • Syntax highlighting.
    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting
    

git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/fast-syntax-highlighting
```
- Auto-completion.

```bash

git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git $ZSH_CUSTOM/plugins/zsh-autocomplete
```

  • Modify the zsh configuration file to change the plugins to:

    vim ~/.zshrc
    
     plugins=(
       git
       zsh-autosuggestions
       zsh-syntax-highlighting
       fast-syntax-highlighting
       zsh-autocomplete
    

)

- Apply the changes.

```bash
source ~/.zshrc
  1. Install NVM to manage NodeJS versions.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  • How to use:

    $ nvm use 16
    Now using node v16.9.1 (npm v7.21.1)
    $ node -v
    v16.9.1
    $ nvm use 14
    Now using node v14.18.0 (npm v6.14.15)
    $ node -v
    v14.18.0
    $ nvm install 12
    Now using node v12.22.6 (npm v6.14.5)
    $ node -v
    v12.22.6
    

    For advanced usage, please refer to the documentation.

  1. Use VS Code to connect to the virtual machine just created.
  • Install the Remote Development extension.
  • Add a remote connection. In the left menu, select Remote Explorer, click the + on SSH, or use the shortcut Shift+Command+P and type remote add to open the window for adding SSH connections.
    image
    Enter ssh orb to connect to the virtual machine just created.
    ::: warning
    If there are multiple virtual machines, you need to specify which host to connect to when using SSH. If not specified, it will connect to the default host. For example, ssh debain@orb connects to the debain host using the default user. Examples include
    ssh machine@orb,
    ssh user@orb,
    ssh user@machine@orb.
    :::
    image
    image

This way, you have a development environment isolated from the host system. No matter how much you tinker, you don't have to worry. It's somewhat similar to WSL in Windows, but it's simple and convenient, without needing to enable certain features of the system. You can use it directly after downloading Orbstack.

This article is synchronized and updated to xLog by Mix Space. The original link is https://remrin.dev/posts/linux/dev-env

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.